app/soc/logic/helper/timeline.py
author Lennard de Rijk <ljvderijk@gmail.com>
Mon, 23 Feb 2009 16:05:30 +0000
changeset 1472 27c9ae3f8d8b
parent 1307 091a21cf3627
child 1652 83903fb16b76
permissions -rw-r--r--
Textual change in cleaing.py. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1220
diff changeset
     3
# Copyright 2009 the Melange authors.
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Functions that are useful when dealing with timelines.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
import datetime
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
def isBeforePeriod(entity, period):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
  """Returns true iff the current DateTime is before the given period_start.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
    period: The name of a period (without start or end).
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
  return isBeforeEvent(entity, '%s_start' % period)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    38
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
def isBeforeEvent(entity, event):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
  """Returns true iff the current DateTime is before the given event.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    event: The name of property in the timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
  event_time = getDateTimeByName(entity, event)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
  if event_time:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
    return datetime.datetime.utcnow() < event_time
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
  else:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
    return False
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    53
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
def isActivePeriod(entity, period):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
  """Returns true iff the current DateTime is between period_start and period_end.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
    period: The name of a period (without start or end).
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
  period_start = '%s_start' % period
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
  period_end = '%s_end' % period
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
  period_started = isAfterEvent(entity, period_start)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
  period_not_ended = isBeforeEvent(entity, period_end)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
  return period_started and period_not_ended
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    70
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
def isAfterPeriod(entity, period):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
  """Returns true iff the current DateTime is after the given period_end.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
    period: The name of a period (without start or end).
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
  return isAfterEvent(entity, '%s_end' % period)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    81
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    82
def isAfterEvent(entity, event):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
  """Returns true iff the current DateTime is after the given event.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    85
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    86
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
    event: The name of property in the timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    88
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    89
  event_time = getDateTimeByName(entity, event)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    90
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
  if event_time:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    92
    return event_time < datetime.datetime.utcnow()
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    93
  else:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
    return False
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    95
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    96
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    97
def getDateTimeByName(entity, name):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
  """Returns the DateTime property with the given name. 
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    99
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   102
    name: The name of a specific property in the given timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   103
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   104
  Returns:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   105
    The requested DateTime property, or None if there is no such property set.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   106
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   107
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
  if hasattr(entity, name):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
    return getattr(entity, name)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
  else:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
    return None