1.1 --- a/imiptools/handlers/scheduling/quota.py Tue Feb 09 14:17:52 2016 +0100
1.2 +++ b/imiptools/handlers/scheduling/quota.py Tue Feb 09 15:01:40 2016 +0100
1.3 @@ -19,8 +19,7 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from imiptools.dates import format_datetime, format_duration, get_datetime, \
1.8 - get_duration, to_utc_datetime
1.9 +from imiptools.dates import 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 @@ -80,13 +79,12 @@
1.14 if total == Endless() or not expiry:
1.15 return
1.16
1.17 - # Obtain the journal entries and limits.
1.18 + # Update the journal entries.
1.19
1.20 journal = handler.get_journal()
1.21 entries = journal.get_entries(quota, group)
1.22 -
1.23 - if _add_to_entries(entries, handler.uid, handler.recurrenceid, format_duration(total), format_datetime(expiry)):
1.24 - journal.set_entries(quota, group, entries)
1.25 + handler.update_freebusy(entries, group, False)
1.26 + journal.set_entries(quota, group, entries)
1.27
1.28 def remove_from_quota(handler, args):
1.29
1.30 @@ -104,13 +102,12 @@
1.31 if total == Endless():
1.32 total = None
1.33
1.34 - # Obtain the journal entries and limits.
1.35 + # Update the journal entries.
1.36
1.37 journal = handler.get_journal()
1.38 entries = journal.get_entries(quota, group)
1.39 -
1.40 - if _remove_from_entries(entries, handler.uid, handler.recurrenceid, format_duration(total)):
1.41 - journal.set_entries(quota, group, entries)
1.42 + handler.remove_from_freebusy(entries)
1.43 + journal.set_entries(quota, group, entries)
1.44
1.45 def _get_quota_and_group(handler, args):
1.46
1.47 @@ -178,73 +175,10 @@
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, found_expiry in entries:
1.53 - retraction = found_duration.startswith("-")
1.54 - multiplier = retraction and -1 or 1
1.55 - total += multiplier * get_duration(found_duration[retraction and 1 or 0:])
1.56 -
1.57 + for period in entries:
1.58 + total += period.get_duration()
1.59 return total
1.60
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' and 'expiry' time.
1.66 - """
1.67 -
1.68 - confirmed = _find_applicable_entry(entries, uid, recurrenceid)
1.69 -
1.70 - # Where a previous entry still applies, retract it if different.
1.71 -
1.72 - if confirmed:
1.73 - found_uid, found_recurrenceid, found_duration, found_expiry = confirmed
1.74 - if found_duration != duration:
1.75 - entries.append((found_uid, found_recurrenceid, "-%s" % found_duration, found_expiry))
1.76 - else:
1.77 - return False
1.78 -
1.79 - # Without an applicable previous entry, add a new entry.
1.80 -
1.81 - entries.append((uid, recurrenceid, duration, expiry))
1.82 - return True
1.83 -
1.84 -def _remove_from_entries(entries, uid, recurrenceid, duration):
1.85 -
1.86 - """
1.87 - Remove from the given 'entries' any entry for the event having the given
1.88 - 'uid' and 'recurrenceid' with the given 'duration'.
1.89 - """
1.90 -
1.91 - confirmed = _find_applicable_entry(entries, uid, recurrenceid)
1.92 -
1.93 - # Where a previous entry still applies, retract it.
1.94 -
1.95 - if confirmed:
1.96 - found_uid, found_recurrenceid, found_duration, found_expiry = confirmed
1.97 - entries.append((found_uid, found_recurrenceid, "-%s" % found_duration, found_expiry))
1.98 - return True
1.99 -
1.100 - return False
1.101 -
1.102 -def _find_applicable_entry(entries, uid, recurrenceid):
1.103 -
1.104 - """
1.105 - Within 'entries', find any applicable previous entry for this event,
1.106 - using the 'uid' and 'recurrenceid'.
1.107 - """
1.108 -
1.109 - confirmed = None
1.110 -
1.111 - for found_uid, found_recurrenceid, found_duration, found_expiry in entries:
1.112 - if uid == found_uid and recurrenceid == found_recurrenceid:
1.113 - if found_duration.startswith("-"):
1.114 - confirmed = None
1.115 - else:
1.116 - confirmed = found_uid, found_recurrenceid, found_duration, found_expiry
1.117 -
1.118 - return confirmed
1.119 -
1.120 # Collective free/busy maintenance.
1.121
1.122 def schedule_across_quota(handler, args):