|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Functions that are useful when dealing with timelines. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 import datetime |
|
26 |
|
27 |
|
28 def isBeforePeriod(entity, period): |
|
29 """Returns true iff the current DateTime is before the given period_start. |
|
30 |
|
31 Args: |
|
32 entity: Instance of a timeline model. |
|
33 period: The name of a period (without start or end). |
|
34 """ |
|
35 |
|
36 return isBeforeEvent(entity, '%s_start' % period) |
|
37 |
|
38 def isBeforeEvent(entity, event): |
|
39 """Returns true iff the current DateTime is before the given event. |
|
40 |
|
41 Args: |
|
42 entity: Instance of a timeline model. |
|
43 event: The name of property in the timeline model. |
|
44 """ |
|
45 event_time = getDateTimeByName(entity, event) |
|
46 |
|
47 if event_time: |
|
48 return datetime.datetime.utcnow() < event_time |
|
49 else: |
|
50 return False |
|
51 |
|
52 def isActivePeriod(entity, period): |
|
53 """Returns true iff the current DateTime is between period_start and period_end. |
|
54 |
|
55 Args: |
|
56 entity: Instance of a timeline model. |
|
57 period: The name of a period (without start or end). |
|
58 """ |
|
59 |
|
60 period_start = '%s_start' % period |
|
61 period_end = '%s_end' % period |
|
62 |
|
63 period_started = isAfterEvent(entity, period_start) |
|
64 period_not_ended = isBeforeEvent(entity, period_end) |
|
65 |
|
66 return period_started and period_not_ended |
|
67 |
|
68 def isAfterPeriod(entity, period): |
|
69 """Returns true iff the current DateTime is after the given period_end. |
|
70 |
|
71 Args: |
|
72 entity: Instance of a timeline model. |
|
73 period: The name of a period (without start or end). |
|
74 """ |
|
75 |
|
76 return isAfterEvent(entity, '%s_end' % period) |
|
77 |
|
78 def isAfterEvent(entity, event): |
|
79 """Returns true iff the current DateTime is after the given event. |
|
80 |
|
81 Args: |
|
82 entity: Instance of a timeline model. |
|
83 event: The name of property in the timeline model. |
|
84 """ |
|
85 event_time = getDateTimeByName(entity, event) |
|
86 |
|
87 if event_time: |
|
88 return event_time < datetime.datetime.utcnow() |
|
89 else: |
|
90 return False |
|
91 |
|
92 def getDateTimeByName(entity, name): |
|
93 """Returns the DateTime property with the given name. |
|
94 |
|
95 Args: |
|
96 entity: Instance of a timeline model. |
|
97 name: The name of a specific property in the given timeline model. |
|
98 |
|
99 Returns: |
|
100 The requested DateTime property, or None if there is no such property set. |
|
101 """ |
|
102 |
|
103 if hasattr(entity, name): |
|
104 return getattr(entity, name) |
|
105 else: |
|
106 return None |