Skip to content
Snippets Groups Projects
Commit 41af46a4 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

cron job for marking vouchers as expired

parent c90f0a96
No related branches found
No related tags found
1 merge request!120Resolve "auto-expire of vouchers"
...@@ -72,7 +72,8 @@ TEMPLATES = [ ...@@ -72,7 +72,8 @@ TEMPLATES = [
CRON_CLASSES = [ CRON_CLASSES = [
'web.views.kit.KitRequestEmailSendJob', 'web.views.kit.KitRequestEmailSendJob',
'web.redcap_connector.RedCapRefreshJob' 'web.redcap_connector.RedCapRefreshJob',
'web.voucher_expiry_job',
] ]
# Password validation # Password validation
......
...@@ -6,10 +6,12 @@ from django.urls import reverse_lazy ...@@ -6,10 +6,12 @@ from django.urls import reverse_lazy
from django.views.generic import CreateView from django.views.generic import CreateView
from django.views.generic import ListView from django.views.generic import ListView
from django.views.generic import UpdateView from django.views.generic import UpdateView
from django_cron import CronJobBase, Schedule
from views.notifications import get_today_midnight_date
from web.forms import VoucherForm from web.forms import VoucherForm
from web.models import Voucher, StudySubject from web.models import Voucher, StudySubject
from web.models.constants import GLOBAL_STUDY_ID from web.models.constants import GLOBAL_STUDY_ID, VOUCHER_STATUS_NEW, VOUCHER_STATUS_EXPIRED
from . import WrappedView from . import WrappedView
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -64,3 +66,18 @@ class VoucherEditView(SuccessMessageMixin, UpdateView, WrappedView): ...@@ -64,3 +66,18 @@ class VoucherEditView(SuccessMessageMixin, UpdateView, WrappedView):
def get_study_subject_id(self): def get_study_subject_id(self):
return Voucher.objects.get(id=self.kwargs['pk']).study_subject.id return Voucher.objects.get(id=self.kwargs['pk']).study_subject.id
class ExpireVouchersJob(CronJobBase):
RUN_EVERY_MINUTES = 120
schedule = Schedule(run_every_mins=RUN_EVERY_MINUTES)
code = 'web.voucher_expiry_job' # a unique code
# noinspection PyMethodMayBeStatic
def do(self):
due_date = get_today_midnight_date()
vouchers = Voucher.objects.filter(status=VOUCHER_STATUS_NEW, expiry_date__lte=due_date)
count = vouchers.count()
if count > 0:
vouchers.update(status=VOUCHER_STATUS_EXPIRED)
return count + " vouchers expired"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment