1 from django.db import models
2 from django.utils.translation import ugettext_lazy as _
5 class Campaign(models.Model):
6 name = models.CharField(_('name'), max_length=255, unique=True)
7 description = models.TextField(_('description'), blank=True)
10 verbose_name = _('campaign')
11 verbose_name_plural = _('campaigns')
17 class Fundraiser(models.Model):
18 name = models.CharField(_('name'), max_length=255, unique=True)
21 verbose_name = _('fundraiser')
22 verbose_name_plural = _('fundraisers')
28 class DirectDebit(models.Model):
29 first_name = models.CharField(_('first name'), max_length=255, blank=True)
30 last_name = models.CharField(_('last name'), max_length=255, blank=True)
31 sex = models.CharField(_('sex'), max_length=1, blank=True, choices=[
35 date_of_birth = models.DateField(_('date of birth'), null=True, blank=True)
36 street = models.CharField(_('street'), max_length=255, blank=True)
37 building = models.CharField(_('building'), max_length=255, blank=True)
38 flat = models.CharField(_('flat'), max_length=255, blank=True)
39 town = models.CharField(_('town'), max_length=255, blank=True)
40 postal_code = models.CharField(_('postal code'), max_length=255, blank=True)
41 phone = models.CharField(_('phone'), max_length=255, blank=True)
42 email = models.CharField(_('e-mail'), max_length=255, blank=True)
43 iban = models.CharField(_('IBAN'), max_length=255, blank=True)
44 iban_valid = models.NullBooleanField(_('IBAN valid'), default=False)
45 is_consumer = models.BooleanField(_('is a consumer'), default=True)
46 payment_id = models.CharField(_('payment identifier'), max_length=255, blank=True, unique=True)
47 agree_fundraising = models.BooleanField(_('agree fundraising'), default=False)
48 agree_newsletter = models.BooleanField(_('agree newsletter'), default=False)
50 acquisition_date = models.DateField(_('acquisition date'), help_text=_('Date from the form'), null=True, blank=True)
51 submission_date = models.DateField(_('submission date'), null=True, blank=True, help_text=_('Date the fundaiser submitted the form'))
52 bank_submission_date = models.DateField(_('bank submission date'), null=True, blank=True, help_text=_('Date when the form data is submitted to the bank'))
53 bank_acceptance_date = models.DateField(_('bank accepted date'), null=True, blank=True, help_text=_('Date when bank accepted the form'))
55 fundraiser = models.ForeignKey(Fundraiser, models.PROTECT, blank=True, null=True, verbose_name=_('fundraiser'))
56 fundraiser_commission = models.IntegerField(_('fundraiser commission'), null=True, blank=True)
57 fundraiser_bill = models.CharField(_('fundaiser bill number'), max_length=255, blank=True)
59 amount = models.IntegerField(_('amount'), null=True, blank=True)
61 notes = models.TextField(_('notes'), blank=True)
63 needs_redo = models.BooleanField(_('needs redo'), default=False)
64 is_cancelled = models.BooleanField(_('is cancelled'), default=False)
65 optout = models.BooleanField(_('optout'), default=False)
67 campaign = models.ForeignKey(Campaign, models.PROTECT, null=True, blank=True, verbose_name=_('campaign'))
70 verbose_name = _('direct debit')
71 verbose_name_plural = _('direct debits')
73 def save(self, **kwargs):
74 self.iban_valid = not self.iban_warning() if self.iban else None
75 super().save(**kwargs)
78 def get_next_payment_id(cls):
79 # Find the last object added.
80 last = cls.objects.order_by('-id').first()
83 match = re.match(r'^(.*?)(\d+)$', last.payment_id)
86 prefix = match.group(1)
87 number = int(match.group(2))
88 number_length = len(match.group(2))
91 payment_id = f'{prefix}{number:0{number_length}}'
92 if not cls.objects.filter(payment_id=payment_id).exists():
96 def iban_warning(self):
99 if len(self.iban) != 26:
100 return 'Bad IBAN length'
101 if int(self.iban[2:] + '2521' + self.iban[:2]) % 97 != 1:
102 return 'This IBAN number looks invalid'
104 iban_warning.short_description = ''