Newer
Older
import datetime
import logging
from django import forms
from django.forms import ModelForm
from django.utils import timezone
from web.algorithm import VerhoeffAlgorithm
from web.forms.forms import DATEPICKER_DATE_ATTRS
from web.models import VoucherType, VoucherTypePrice, Voucher, Worker
from web.models.constants import VOUCHER_STATUS_NEW, VOUCHER_STATUS_USED
from web.models.worker_study_role import WORKER_VOUCHER_PARTNER
logger = logging.getLogger(__name__)
class VoucherTypeForm(ModelForm):
class Meta:
model = VoucherType
exclude = ['study']
class VoucherTypePriceForm(ModelForm):
start_date = forms.DateField(label="Start date",
widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d")
)
end_date = forms.DateField(label="End date",
widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d")
)
class Meta:
model = VoucherTypePrice
exclude = ['voucher_type']
class VoucherForm(ModelForm):
class Meta:
model = Voucher
fields = '__all__'
def __init__(self, *args, **kwargs):
voucher_types = kwargs.pop('voucher_types', VoucherType.objects.all())
super(VoucherForm, self).__init__(*args, **kwargs)
self.fields['voucher_type'].queryset = voucher_types
self.fields['usage_partner'].queryset = Worker.get_workers_by_worker_type(WORKER_VOUCHER_PARTNER)
self.fields['number'].widget.attrs['readonly'] = True
self.fields['number'].required = False
self.fields['issue_date'].widget.attrs['readonly'] = True
self.fields['issue_date'].required = False
self.fields['expiry_date'].widget.attrs['readonly'] = True
self.fields['expiry_date'].required = False
self.fields['use_date'].widget.attrs['readonly'] = True
instance = getattr(self, 'instance', None)
if instance and instance.pk:
self.fields['voucher_type'].widget.attrs['readonly'] = True
self.fields['usage_partner'].queryset = Worker.get_workers_by_worker_type(WORKER_VOUCHER_PARTNER).filter(
voucher_types = instance.voucher_type)
if instance.status != VOUCHER_STATUS_NEW:
self.fields['status'].widget.attrs['readonly'] = True
self.fields['feedback'].widget.attrs['readonly'] = True
self.fields['usage_partner'].widget.attrs['readonly'] = True
def clean(self):
if self.cleaned_data["status"] == VOUCHER_STATUS_USED and not self.cleaned_data["usage_partner"]:
self.add_error('usage_partner', "Partner must be defined for used voucher")
if self.cleaned_data["status"] != VOUCHER_STATUS_USED and self.cleaned_data["usage_partner"]:
self.add_error('status', "Status must be used for voucher with defined partner")
def save(self, commit=True):
instance = super(VoucherForm, self).save(commit=False)
if not instance.id:
instance.issue_date = timezone.now()
instance.expiry_date = instance.issue_date + datetime.timedelta(days=92)
max_id = str(0).zfill(5)
if Voucher.objects.all().count() > 0:
max_id = str(Voucher.objects.latest('id').id).zfill(5)
instance.number = max_id + VerhoeffAlgorithm.calculate_verhoeff_check_sum(max_id)
if instance.status == VOUCHER_STATUS_USED and not instance.use_date:
instance.use_date = timezone.now()
if commit:
instance.save()