Guest User

Untitled

a guest
Sep 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. def clean(self):
  2. act_date = start_date
  3. exp_date = end_date
  4.  
  5.  
  6. act_date = self.cleaned_data.get('activated')
  7. exp_date = self.cleaned_data.get('expiry')
  8.  
  9.  
  10.  
  11. if act_date and exp_date:
  12. if act_date >= exp_date:
  13. raise forms.ValidationError("Activation date could not be on or after Expiry date ")
  14. if exp_date < datetime.today().date():
  15. raise forms.ValidationError("Expiry date must be a future date")
  16.  
  17. """
  18. CREATING A SET OF DATE OBJECTS WITH THE NEW TIME SLOT
  19. """
  20. set_current = set([act_date + timedelta(days=day) for day in range((exp_date-act_date).days)])
  21.  
  22.  
  23. filter_conditions = Q(expiry__gt=act_date) & Q(governing_body=self.formkwargs.get('pk'))
  24.  
  25. if self.instance:
  26. filter_conditions &= ~Q(id=self.instance.id)
  27.  
  28. """
  29. SUBSCRIPTION PLAN MODEL CONTAINS OBJECTS WITH START_DATE as `activated` AND END_DATE as `expiry` SLOT.
  30.  
  31. """
  32.  
  33. for plan in SubscriptionPlan.objects.filter(filter_conditions):
  34. temp_date = set([plan.activated + timedelta(days=day)
  35. for day in range((plan.expiry-plan.activated).days)])
  36. if set_current.intersection(temp_date):
  37. raise forms.ValidationError("You have another plan active within this time slot. "
  38. "[ %s to %s ]" % (plan.activated, plan.expiry))
  39.  
  40. return super(SubscriptionForm, self).clean()
Add Comment
Please, Sign In to add comment