Skip to content

Commit 0fb7f79

Browse files
committed
Extend cronjob to handle additional collection
1 parent 7df8ca9 commit 0fb7f79

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

src/onegov/org/cronjobs.py

+35-31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from onegov.core.orm import find_models, Base
1515
from onegov.core.orm.mixins.publication import UTCPublicationMixin
1616
from onegov.core.templates import render_template
17+
from onegov.directory.collections.directory import EntryRecipientCollection
1718
from onegov.event import Occurrence, Event
1819
from onegov.file import FileCollection
1920
from onegov.form import FormSubmission, parse_form, Form
@@ -774,37 +775,40 @@ def get_postmark_token() -> str:
774775

775776
token = get_postmark_token()
776777

777-
recipients = RecipientCollection(request.session)
778-
yesterday = utcnow() - timedelta(days=1)
779-
780-
try:
781-
r = requests.get(
782-
'https://api.postmarkapp.com/bounces?count=500&offset=0',
783-
f'fromDate={yesterday.date()}&toDate='
784-
f'{yesterday.date()}&inactive=true',
785-
headers={
786-
'Accept': 'application/json',
787-
'X-Postmark-Server-Token': token
788-
},
789-
timeout=30
790-
)
791-
r.raise_for_status()
792-
bounces = r.json().get('Bounces', [])
793-
except requests.exceptions.HTTPError as http_err:
794-
if r.status_code == 401:
795-
raise RuntimeWarning(
796-
f'Postmark API token is not set or invalid: {http_err}'
797-
) from None
798-
else:
799-
raise
800-
801-
for bounce in bounces:
802-
email = bounce.get('Email', '')
803-
inactive = bounce.get('Inactive', False)
804-
recipient = recipients.by_address(email)
805-
806-
if recipient and inactive:
807-
recipient.mark_inactive()
778+
collections = (RecipientCollection, EntryRecipientCollection)
779+
for collection in collections:
780+
recipients = collection(request.session)
781+
yesterday = utcnow() - timedelta(days=1)
782+
783+
try:
784+
r = requests.get(
785+
'https://api.postmarkapp.com/bounces?count=500&offset=0',
786+
f'fromDate={yesterday.date()}&toDate='
787+
f'{yesterday.date()}&inactive=true',
788+
headers={
789+
'Accept': 'application/json',
790+
'X-Postmark-Server-Token': token
791+
},
792+
timeout=30
793+
)
794+
r.raise_for_status()
795+
bounces = r.json().get('Bounces', [])
796+
except requests.exceptions.HTTPError as http_err:
797+
if r.status_code == 401:
798+
raise RuntimeWarning(
799+
f'Postmark API token is not set or invalid: {http_err}'
800+
) from None
801+
else:
802+
raise
803+
804+
for bounce in bounces:
805+
email = bounce.get('Email', '')
806+
inactive = bounce.get('Inactive', False)
807+
recipient = recipients.by_address(email)
808+
809+
if recipient and inactive:
810+
print(f'Mark recipient {recipient.address} as inactive')
811+
recipient.mark_inactive()
808812

809813

810814
@OrgApp.cronjob(hour=4, minute=30, timezone='Europe/Zurich')

0 commit comments

Comments
 (0)