Test coverage report: calendarium.forms

# calendarium.forms: 70 total statements, 79.7% covered

Generated: Thu 2013-05-02 12:21 SGT

Source file: /Users/martin/Repos/django-calendarium/calendarium/forms.py

Stats: 51 executed, 13 missed, 6 excluded, 58 ignored

1. `"""Forms for the ``calendarium`` app."""`
2. `from django import forms`
3. `from django.contrib.auth.models import User`
4. `from django.forms.models import model_to_dict`
5. `from django.utils.timezone import datetime, timedelta`
6. ``
7. `from calendarium.constants import (`
8. `OCCURRENCE_DECISION_CHOICESS,`
9. `OCCURRENCE_DECISIONS,`
10. `)`
11. `from calendarium.models import Event, Occurrence`
12. ``
13. ``
14. `class OccurrenceForm(forms.ModelForm):`
15. `"""A form for the ``Occurrence`` model."""`
16. `decision = forms.CharField(`
17. `widget=forms.Select(choices=OCCURRENCE_DECISION_CHOICESS),`
18. `)`
19. ``
20. `cancelled = forms.BooleanField(`
21. `widget=forms.HiddenInput,`
22. `required=False,`
23. `)`
24. ``
25. `original_start = forms.DateTimeField(`
26. `widget=forms.HiddenInput,`
27. `)`
28. ``
29. `original_end = forms.DateTimeField(`
30. `widget=forms.HiddenInput,`
31. `)`
32. ``
33. `event = forms.ModelChoiceField(`
34. `widget=forms.HiddenInput,`
35. `queryset=Event.objects.all(),`
36. `)`
37. ``
38. `class Meta:`
39. `model = Occurrence`
40. ``
41. `def save(self):`
42. `cleaned_data = self.cleaned_data`
43. `if cleaned_data['decision'] == OCCURRENCE_DECISIONS['all']:`
44. `changes = dict(`
45. `(key, value) for key, value in cleaned_data.iteritems()`
46. `if value != self.initial.get(key) and self.initial.get(key))`
47. `event = self.instance.event`
48. `# for each field on the event, check for new data in cleaned_data`
49. `for field_name in [field.name for field in event._meta.fields]:`
50. `value = changes.get(field_name)`
51. `if value:`
52. `setattr(event, field_name, value)`
53. `event.save()`
54. ``
55. `# repeat for persistent occurrences`
56. `for occ in event.occurrences.all():`
57. `for field_name in [field.name for field in occ._meta.fields]:`
58. `value = changes.get(field_name)`
59. `if value:`
60. `# since we can't just set a new datetime, we have to`
61. `# adjust the datetime fields according to the changes`
62. `# on the occurrence form instance`
63. `if type(value) != datetime:`
64. `setattr(occ, field_name, value)`
65. `else:`
66. `initial_time = self.initial.get(field_name)`
67. `occ_time = getattr(occ, field_name)`
68. `delta = value - initial_time`
69. `new_time = occ_time + delta`
70. `setattr(occ, field_name, new_time)`
71. `occ.save()`
72. ``
73. `# get everything from initial and compare to cleaned_data to`
74. `# retrieve what has been changed`
75. `# apply those changes to the persistent occurrences (and the main`
76. `# event)`
77. `elif cleaned_data['decision'] == OCCURRENCE_DECISIONS['this one']:`
78. `self.instance.save()`
79. `elif cleaned_data['decision'] == OCCURRENCE_DECISIONS['following']:`
80. `# get the changes`
81. `changes = dict(`
82. `(key, value) for key, value in cleaned_data.iteritems()`
83. `if value != self.initial.get(key) and self.initial.get(key))`
84. ``
85. `# change the old event`
86. `old_event = self.instance.event`
87. `end_recurring_period = self.instance.event.end_recurring_period`
88. `old_event.end_recurring_period = self.instance.start - timedelta(`
89. `days=1)`
90. `old_event.save()`
91. ``
92. `# the instance occurrence holds the info for the new event, that we`
93. `# use to update the old event's fields`
94. `new_event = old_event`
95. `new_event.end_recurring_period = end_recurring_period`
96. `new_event.id = None`
97. `event_kwargs = model_to_dict(self.instance)`
98. `for field_name in [field.name for field in new_event._meta.fields]:`
99. `if (field_name == 'created_by'`
100. `and event_kwargs.get('created_by')):`
101. `value = User.objects.get(pk=event_kwargs.get(field_name))`
102. `elif field_name in ['rule', 'category']:`
103. `continue`
104. `else:`
105. `value = event_kwargs.get(field_name)`
106. `if value:`
107. `setattr(new_event, field_name, value)`
108. `new_event.save()`
109. ``
110. `# update the new event and update all of the corresponding occs`
111. `for occ in self.instance.event.occurrences.filter(`
112. `start__gte=self.instance.start):`
113. `occ.event = new_event`
114. `for field_name in [field.name for field in occ._meta.fields]:`
115. `value = changes.get(field_name)`
116. `if value:`
117. `# since we can't just set a new datetime, we have to`
118. `# adjust the datetime fields according to the changes`
119. `# on the occurrence form instance`
120. `if type(value) != datetime:`
121. `setattr(occ, field_name, value)`
122. `else:`
123. `initial_time = self.initial.get(field_name)`
124. `occ_time = getattr(occ, field_name)`
125. `delta = value - initial_time`
126. `new_time = occ_time + delta`
127. `setattr(occ, field_name, new_time)`
128. `occ.save()`