1.1 --- a/imiptools/handlers/scheduling/quota.py Mon Feb 08 17:40:20 2016 +0100
1.2 +++ b/imiptools/handlers/scheduling/quota.py Mon Feb 08 18:46:31 2016 +0100
1.3 @@ -19,7 +19,8 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from imiptools.dates import format_duration, get_duration
1.8 +from imiptools.dates import format_datetime, format_duration, get_datetime, \
1.9 + get_duration, to_utc_datetime
1.10 from imiptools.data import get_uri
1.11 from imiptools.period import Endless
1.12 from datetime import timedelta
1.13 @@ -72,13 +73,14 @@
1.14 quota, group = _get_quota_and_group(handler, args)
1.15
1.16 total = _get_duration(handler)
1.17 + expiry = _get_expiry_time(handler)
1.18
1.19 # Obtain the journal entries and limits.
1.20
1.21 journal = handler.get_journal()
1.22 entries = journal.get_entries(quota, group)
1.23
1.24 - if _add_to_entries(entries, handler.uid, handler.recurrenceid, format_duration(total)):
1.25 + if _add_to_entries(entries, handler.uid, handler.recurrenceid, format_duration(total), format_datetime(expiry)):
1.26 journal.set_entries(quota, group, entries)
1.27
1.28 def remove_from_quota(handler, args):
1.29 @@ -142,24 +144,37 @@
1.30
1.31 return total
1.32
1.33 +def _get_expiry_time(handler):
1.34 +
1.35 + """
1.36 + Return the expiry time for quota purposes of the current object provided by
1.37 + the 'handler'.
1.38 + """
1.39 +
1.40 + # Count only explicit periods.
1.41 + # NOTE: Should reject indefinitely recurring events.
1.42 +
1.43 + periods = handler.get_periods(handler.obj, explicit_only=True)
1.44 + return periods and to_utc_datetime(periods[-1].get_end_point()) or None
1.45 +
1.46 def _get_usage(entries):
1.47
1.48 "Return the usage total according to the given 'entries'."
1.49
1.50 total = timedelta(0)
1.51
1.52 - for found_uid, found_recurrenceid, found_duration in entries:
1.53 + for found_uid, found_recurrenceid, found_duration, found_expiry in entries:
1.54 retraction = found_duration.startswith("-")
1.55 multiplier = retraction and -1 or 1
1.56 total += multiplier * get_duration(found_duration[retraction and 1 or 0:])
1.57
1.58 return total
1.59
1.60 -def _add_to_entries(entries, uid, recurrenceid, duration):
1.61 +def _add_to_entries(entries, uid, recurrenceid, duration, expiry):
1.62
1.63 """
1.64 Add to 'entries' an entry for the event having the given 'uid' and
1.65 - 'recurrenceid' with the given 'duration'.
1.66 + 'recurrenceid' with the given 'duration' and 'expiry' time.
1.67 """
1.68
1.69 confirmed = _find_applicable_entry(entries, uid, recurrenceid, duration)
1.70 @@ -167,15 +182,15 @@
1.71 # Where a previous entry still applies, retract it if different.
1.72
1.73 if confirmed:
1.74 - found_uid, found_recurrenceid, found_duration = confirmed
1.75 + found_uid, found_recurrenceid, found_duration, found_expiry = confirmed
1.76 if found_duration != duration:
1.77 - entries.append((found_uid, found_recurrenceid, "-%s" % found_duration))
1.78 + entries.append((found_uid, found_recurrenceid, "-%s" % found_duration, found_expiry))
1.79 else:
1.80 return False
1.81
1.82 # Without an applicable previous entry, add a new entry.
1.83
1.84 - entries.append((uid, recurrenceid, duration))
1.85 + entries.append((uid, recurrenceid, duration, expiry))
1.86 return True
1.87
1.88 def _remove_from_entries(entries, uid, recurrenceid, duration):
1.89 @@ -190,8 +205,8 @@
1.90 # Where a previous entry still applies, retract it.
1.91
1.92 if confirmed:
1.93 - found_uid, found_recurrenceid, found_duration = confirmed
1.94 - entries.append((found_uid, found_recurrenceid, "-%s" % found_duration))
1.95 + found_uid, found_recurrenceid, found_duration, found_expiry = confirmed
1.96 + entries.append((found_uid, found_recurrenceid, "-%s" % found_duration, found_expiry))
1.97 return found_duration == duration
1.98
1.99 return False
1.100 @@ -205,12 +220,12 @@
1.101
1.102 confirmed = None
1.103
1.104 - for found_uid, found_recurrenceid, found_duration in entries:
1.105 + for found_uid, found_recurrenceid, found_duration, found_expiry in entries:
1.106 if uid == found_uid and recurrenceid == found_recurrenceid:
1.107 if found_duration.startswith("-"):
1.108 confirmed = None
1.109 else:
1.110 - confirmed = found_uid, found_recurrenceid, found_duration
1.111 + confirmed = found_uid, found_recurrenceid, found_duration, found_expiry
1.112
1.113 return confirmed
1.114