event_rsvp.forms: 64 total statements, 100.0% covered

Generated: Tue 2013-04-16 10:12 CEST

Source file: /home/tobi/Projects/event-rsvp/src/event_rsvp/forms.py

Stats: 61 executed, 0 missed, 3 excluded, 56 ignored

  1. """Forms for the ``event_rsvp`` app."""
  2. from django import forms
  3. from django.utils.translation import ugettext_lazy as _
  4. from event_rsvp.models import Event, Guest
  5. class EventForm(forms.ModelForm):
  6. """Form to handle specific validations of the Event model."""
  7. required_css_class = 'requiredField'
  8. def __init__(self, created_by, create_from_template=False, *args,
  9. **kwargs):
  10. """
  11. :param created_by: Django-Auth User model, which is calling this view
  12. :param create_from_template: Boolean to know, if someone is using a
  13. template as draft.
  14. """
  15. self.instance = kwargs.get('instance')
  16. self.create_from_template = create_from_template
  17. if hasattr(self.instance, 'id') and not self.create_from_template:
  18. # Ignore the current user if it's the update view or if the
  19. # instance is a template
  20. self.created_by = self.instance.created_by
  21. else:
  22. # Add the current user as owner
  23. self.created_by = created_by
  24. if self.create_from_template:
  25. # If we use a template, the template_name is the only attribute,
  26. # which should not be preset
  27. self.instance.template_name = ''
  28. super(EventForm, self).__init__(*args, **kwargs)
  29. if self.instance.id and self.instance.template_name:
  30. self.this_is_a_template = True
  31. else:
  32. self.this_is_a_template = False
  33. def save(self, commit=True):
  34. """Saves twice if a template should be created."""
  35. if self.create_from_template:
  36. # Save a new event
  37. return forms.models.save_instance(
  38. self, Event(created_by=self.created_by), self._meta.fields,
  39. 'created', commit, False)
  40. self.instance.created_by = self.created_by
  41. self.instance = super(EventForm, self).save(commit=True)
  42. if self.instance.template_name and not self.this_is_a_template:
  43. """
  44. If someone wants to save this instance as a template, create a
  45. second event as the template and keep the current instance in a
  46. 'normal' condition. This is necessary, 'cause otherwise the
  47. template would keep the 'good' slug like 'foo-bar', while the new
  48. event got a slug like '_foo-bar'.
  49. """
  50. template = Event(template_name=self.instance.template_name,
  51. created_by=self.created_by)
  52. self.instance.template_name = ''
  53. self.instance.save()
  54. forms.models.save_instance(
  55. self, template, self._meta.fields, 'created', commit, False)
  56. return self.instance
  57. class Meta:
  58. model = Event
  59. exclude = ('created_by', 'slug')
  60. class GuestForm(forms.ModelForm):
  61. """Form to handle specific validations of the Guest model."""
  62. required_css_class = 'requiredField'
  63. def __init__(self, event, user, *args, **kwargs):
  64. """
  65. :param event: Event to participate
  66. :param user: Current user or anonymous.
  67. """
  68. self.event = event
  69. if user and user.is_authenticated():
  70. self.user = user
  71. else:
  72. self.user = None
  73. super(GuestForm, self).__init__(*args, **kwargs)
  74. if self.event.id:
  75. for field in self.event.required_fields:
  76. if field:
  77. self.fields[field].required = True
  78. def clean_number_of_seats(self):
  79. data = self.cleaned_data['number_of_seats'] or 1
  80. free_seats = self.event.get_free_seats()
  81. if self.event.available_seats and free_seats < data:
  82. if free_seats == 1:
  83. msg = _('Sorry. There is only 1 seat left.')
  84. else:
  85. msg = _('Sorry. There are only %(amount)s seats left.') % {
  86. 'amount': free_seats}
  87. raise forms.ValidationError(msg)
  88. if (self.event.max_seats_per_guest > 0
  89. and data > self.event.max_seats_per_guest):
  90. if self.event.max_seats_per_guest == 1:
  91. msg = _('Pardon. There is only 1 seat per person reservable.')
  92. else:
  93. msg = _('Pardon. There are only %(amount)s seats per person'
  94. ' reservable.') % {
  95. 'amount': self.event.max_seats_per_guest}
  96. raise forms.ValidationError(msg)
  97. return data
  98. def save(self, *args, **kwargs):
  99. self.instance.user = self.user
  100. self.instance.event = self.event
  101. super(GuestForm, self).save(*args, **kwargs)
  102. class Meta:
  103. model = Guest
  104. fields = ('name', 'email', 'phone', 'number_of_seats', 'message')