|
14 | 14 | from onegov.core.orm import find_models, Base
|
15 | 15 | from onegov.core.orm.mixins.publication import UTCPublicationMixin
|
16 | 16 | from onegov.core.templates import render_template
|
| 17 | +from onegov.directory.collections.directory import EntryRecipientCollection |
17 | 18 | from onegov.event import Occurrence, Event
|
18 | 19 | from onegov.file import FileCollection
|
19 | 20 | from onegov.form import FormSubmission, parse_form, Form
|
@@ -774,37 +775,40 @@ def get_postmark_token() -> str:
|
774 | 775 |
|
775 | 776 | token = get_postmark_token()
|
776 | 777 |
|
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() |
808 | 812 |
|
809 | 813 |
|
810 | 814 | @OrgApp.cronjob(hour=4, minute=30, timezone='Europe/Zurich')
|
|
0 commit comments