app/soc/logic/helper/timeline.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 30 Jun 2009 14:56:48 +0200
changeset 2464 64b241a237b2
parent 1652 83903fb16b76
permissions -rw-r--r--
Merge params from inheriting Survey views so functionality can be reused.
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):
1652
83903fb16b76 Fix too long lines in soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    55
  """Returns true iff the current DateTime is between period_start 
83903fb16b76 Fix too long lines in soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    56
     and period_end.
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
    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
    61
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
  period_start = '%s_start' % period
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
  period_end = '%s_end' % period
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
  period_started = isAfterEvent(entity, period_start)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
  period_not_ended = isBeforeEvent(entity, period_end)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
  return period_started and period_not_ended
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    71
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
def isAfterPeriod(entity, period):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
  """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
    74
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
    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
    78
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
  return isAfterEvent(entity, '%s_end' % period)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    82
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
def isAfterEvent(entity, event):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
  """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
    85
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    86
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    88
    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
    89
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    90
  event_time = getDateTimeByName(entity, event)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    92
  if event_time:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    93
    return event_time < datetime.datetime.utcnow()
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
  else:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    95
    return False
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    96
1220
1adb4d1cce64 Add missing blank line to soc.logic.helper.timeline module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1208
diff changeset
    97
1208
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
def getDateTimeByName(entity, name):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    99
  """Returns the DateTime property with the given name. 
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
  Args:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   102
    entity: Instance of a timeline model.
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   103
    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
   104
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   105
  Returns:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   106
    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
   107
  """
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
  if hasattr(entity, name):
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
    return getattr(entity, name)
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
  else:
10cef80ad917 Added new helper module for timelines.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   112
    return None