Test coverage report: calendarium.utils

# calendarium.utils: 20 total statements, 100.0% covered

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

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

Stats: 18 executed, 0 missed, 2 excluded, 55 ignored

1. `"""`
2. `Utils for the ``calendarium`` app.`
3. ``
4. `The code of these utils is highly influenced by or taken from the utils of`
5. `django-schedule:`
6. ``
7. `https://github.com/thauber/django-schedule/blob/master/schedule/utils.py`
8. ``
9. ``
10. `"""`
11. `import time`
12. `from django.utils import timezone`
13. ``
14. ``
15. `def now(**kwargs):`
16. `"""`
17. `Utility function to zero microseconds to avoid inaccuracy.`
18. ``
19. `I replaced the microseconds, because there is some slightly varying`
20. `difference that occurs out of unknown reason. Since we probably never`
21. `schedule events on microsecond basis, seconds and microseconds will be`
22. `zeroed everywhere.`
23. ``
24. `"""`
25. `return timezone.now(**kwargs).replace(second=0, microsecond=0)`
26. ``
27. ``
28. `def monday_of_week(year, week):`
29. `"""`
30. `Returns a datetime for the monday of the given week of the given year.`
31. ``
32. `"""`
33. `str_time = time.strptime('{0} {1} 1'.format(year, week), '%Y %W %w')`
34. `date = timezone.datetime(year=str_time.tm_year, month=str_time.tm_mon,`
35. `day=str_time.tm_mday, tzinfo=timezone.utc)`
36. `if timezone.datetime(year, 1, 4).isoweekday() > 4:`
37. `# ISO 8601 where week 1 is the first week that has at least 4 days in`
38. `# the current year`
39. `date -= timezone.timedelta(days=7)`
40. `return date`
41. ``
42. ``
43. `class OccurrenceReplacer(object):`
44. `"""`
45. `When getting a list of occurrences, the last thing that needs to be done`
46. `before passing it forward is to make sure all of the occurrences that`
47. `have been stored in the datebase replace, in the list you are returning,`
48. `the generated ones that are equivalent. This class makes this easier.`
49. ``
50. `"""`
51. `def __init__(self, persisted_occurrences):`
52. `lookup = [`
53. `((occ.event, occ.original_start, occ.original_end), occ) for`
54. `occ in persisted_occurrences]`
55. `self.lookup = dict(lookup)`
56. ``
57. `def get_occurrence(self, occ):`
58. `"""`
59. `Return a persisted occurrences matching the occ and remove it from`
60. `lookup since it has already been matched`
61. `"""`
62. `return self.lookup.pop(`
63. `(occ.event, occ.original_start, occ.original_end),`
64. `occ)`
65. ``
66. `def has_occurrence(self, occ):`
67. `return (occ.event, occ.original_start, occ.original_end) in self.lookup`
68. ``
69. `def get_additional_occurrences(self, start, end):`
70. `"""`
71. `Return persisted occurrences which are now in the period`
72. `"""`
73. `return [occ for key, occ in self.lookup.items() if (`
74. `(end and occ.start < end)`
75. `and occ.end >= start and not occ.cancelled)]`