|
| 1 | +from celery import shared_task |
| 2 | +from django.utils import timezone |
| 3 | +from datetime import timedelta |
| 4 | +from ..models.certifying_organisation import CertifyingOrganisation |
| 5 | +from ..models.status import Status |
| 6 | +from ..views import send_rejection_email |
| 7 | + |
| 8 | + |
| 9 | +@shared_task |
| 10 | +def reject_pending_organisations(days=365): |
| 11 | + """Celery task to reject pending certifying organisations |
| 12 | + that were created more than one year ago. |
| 13 | + """ |
| 14 | + print('Begin process....') |
| 15 | + print(f'Number of days: {days}') |
| 16 | + one_year_ago = timezone.now() - timedelta(days=days) |
| 17 | + old_organisations = CertifyingOrganisation.unapproved_objects.all() |
| 18 | + |
| 19 | + print('Begin process to reject old certifying organisations.') |
| 20 | + |
| 21 | + count = 0 |
| 22 | + for organisation in old_organisations: |
| 23 | + if organisation.update_date < one_year_ago: |
| 24 | + rejected_status, created = Status.objects.get_or_create( |
| 25 | + name='Rejected', |
| 26 | + project=organisation.project |
| 27 | + ) |
| 28 | + organisation.status = rejected_status |
| 29 | + organisation.rejected = True |
| 30 | + organisation.save() |
| 31 | + |
| 32 | + send_rejection_email( |
| 33 | + organisation, |
| 34 | + 'certification.qgis.org', |
| 35 | + 'https' |
| 36 | + ) |
| 37 | + count += 1 |
| 38 | + print(f'{organisation.name} has been set to Rejected') |
| 39 | + |
| 40 | + result = f'{count} certifying organisations have been set to Rejected' |
| 41 | + print('------------------------------------------------------------') |
| 42 | + print('Process finished.') |
| 43 | + return result |
0 commit comments