# HG changeset patch # User Paul Boddie # Date 1454953591 -3600 # Node ID a55598b6f053ab244c97a4f8336e3dec240125bf # Parent c06cdeae95a38f6cf5c13c2bd4fa468f2992975e Added expiry times to recorded events so that quotas can be updated. diff -r c06cdeae95a3 -r a55598b6f053 imip_store.py --- a/imip_store.py Mon Feb 08 17:40:20 2016 +0100 +++ b/imip_store.py Mon Feb 08 18:46:31 2016 +0100 @@ -986,7 +986,7 @@ return dict(self._get_table_atomic(quota, filename, tab_separated=False)) - # Free/busy period access. + # Free/busy period access for users within quota groups. def get_freebusy(self, quota, user, get_table=None): @@ -1024,7 +1024,7 @@ if not filename or not isfile(filename): return [] - return self._get_table_atomic(quota, filename, [(1, None)]) + return self._get_table_atomic(quota, filename, [(1, None), (3, None)]) def set_entries(self, quota, group, entries): @@ -1037,7 +1037,7 @@ if not filename: return False - self._set_table_atomic(quota, filename, entries, [(1, "")]) + self._set_table_atomic(quota, filename, entries, [(1, ""), (3, "")]) return True # vim: tabstop=4 expandtab shiftwidth=4 diff -r c06cdeae95a3 -r a55598b6f053 imiptools/handlers/scheduling/quota.py --- a/imiptools/handlers/scheduling/quota.py Mon Feb 08 17:40:20 2016 +0100 +++ b/imiptools/handlers/scheduling/quota.py Mon Feb 08 18:46:31 2016 +0100 @@ -19,7 +19,8 @@ this program. If not, see . """ -from imiptools.dates import format_duration, get_duration +from imiptools.dates import format_datetime, format_duration, get_datetime, \ + get_duration, to_utc_datetime from imiptools.data import get_uri from imiptools.period import Endless from datetime import timedelta @@ -72,13 +73,14 @@ quota, group = _get_quota_and_group(handler, args) total = _get_duration(handler) + expiry = _get_expiry_time(handler) # Obtain the journal entries and limits. journal = handler.get_journal() entries = journal.get_entries(quota, group) - if _add_to_entries(entries, handler.uid, handler.recurrenceid, format_duration(total)): + if _add_to_entries(entries, handler.uid, handler.recurrenceid, format_duration(total), format_datetime(expiry)): journal.set_entries(quota, group, entries) def remove_from_quota(handler, args): @@ -142,24 +144,37 @@ return total +def _get_expiry_time(handler): + + """ + Return the expiry time for quota purposes of the current object provided by + the 'handler'. + """ + + # Count only explicit periods. + # NOTE: Should reject indefinitely recurring events. + + periods = handler.get_periods(handler.obj, explicit_only=True) + return periods and to_utc_datetime(periods[-1].get_end_point()) or None + def _get_usage(entries): "Return the usage total according to the given 'entries'." total = timedelta(0) - for found_uid, found_recurrenceid, found_duration in entries: + for found_uid, found_recurrenceid, found_duration, found_expiry in entries: retraction = found_duration.startswith("-") multiplier = retraction and -1 or 1 total += multiplier * get_duration(found_duration[retraction and 1 or 0:]) return total -def _add_to_entries(entries, uid, recurrenceid, duration): +def _add_to_entries(entries, uid, recurrenceid, duration, expiry): """ Add to 'entries' an entry for the event having the given 'uid' and - 'recurrenceid' with the given 'duration'. + 'recurrenceid' with the given 'duration' and 'expiry' time. """ confirmed = _find_applicable_entry(entries, uid, recurrenceid, duration) @@ -167,15 +182,15 @@ # Where a previous entry still applies, retract it if different. if confirmed: - found_uid, found_recurrenceid, found_duration = confirmed + found_uid, found_recurrenceid, found_duration, found_expiry = confirmed if found_duration != duration: - entries.append((found_uid, found_recurrenceid, "-%s" % found_duration)) + entries.append((found_uid, found_recurrenceid, "-%s" % found_duration, found_expiry)) else: return False # Without an applicable previous entry, add a new entry. - entries.append((uid, recurrenceid, duration)) + entries.append((uid, recurrenceid, duration, expiry)) return True def _remove_from_entries(entries, uid, recurrenceid, duration): @@ -190,8 +205,8 @@ # Where a previous entry still applies, retract it. if confirmed: - found_uid, found_recurrenceid, found_duration = confirmed - entries.append((found_uid, found_recurrenceid, "-%s" % found_duration)) + found_uid, found_recurrenceid, found_duration, found_expiry = confirmed + entries.append((found_uid, found_recurrenceid, "-%s" % found_duration, found_expiry)) return found_duration == duration return False @@ -205,12 +220,12 @@ confirmed = None - for found_uid, found_recurrenceid, found_duration in entries: + for found_uid, found_recurrenceid, found_duration, found_expiry in entries: if uid == found_uid and recurrenceid == found_recurrenceid: if found_duration.startswith("-"): confirmed = None else: - confirmed = found_uid, found_recurrenceid, found_duration + confirmed = found_uid, found_recurrenceid, found_duration, found_expiry return confirmed