1.1 --- a/tests/test_handle.py Sat Oct 03 01:04:41 2015 +0200
1.2 +++ b/tests/test_handle.py Sat Oct 03 01:05:28 2015 +0200
1.3 @@ -21,7 +21,9 @@
1.4
1.5 from imiptools.client import ClientForObject
1.6 from imiptools.data import Object, get_address, parse_object
1.7 +from imiptools.dates import get_datetime, to_timezone
1.8 from imiptools.mail import Messenger
1.9 +from imiptools.period import RecurringPeriod
1.10 import imip_store
1.11 import sys
1.12
1.13 @@ -34,16 +36,38 @@
1.14
1.15 # Action methods.
1.16
1.17 - def handle_request(self, accept):
1.18 + def handle_request(self, action, start=None, end=None):
1.19
1.20 """
1.21 - Process the current request for the current user. Return whether any
1.22 - action was taken.
1.23 + Process the current request for the current user. Return whether the
1.24 + given 'action' was taken.
1.25 +
1.26 + If 'start' and 'end' are specified, they will be used in any
1.27 + counter-proposal.
1.28 """
1.29
1.30 # Reply only on behalf of this user.
1.31
1.32 - attendee_attr = self.update_participation(self.obj, accept and "ACCEPTED" or "DECLINED")
1.33 + if action in ("accept", "decline"):
1.34 + attendee_attr = self.update_participation(self.obj, action == "accept" and "ACCEPTED" or "DECLINED")
1.35 + method = "REPLY"
1.36 +
1.37 + # For counter-proposals, set a new main period for the event.
1.38 +
1.39 + elif action == "counter":
1.40 + attendee_attr = self.obj.get_value_map("ATTENDEE").get(self.user)
1.41 + period = self.obj.get_main_period(self.get_tzid())
1.42 +
1.43 + # Use the existing or configured time zone for the specified
1.44 + # datetimes.
1.45 +
1.46 + start = to_timezone(get_datetime(start), period.tzid)
1.47 + end = to_timezone(get_datetime(end), period.tzid)
1.48 + period = RecurringPeriod(start, end, period.tzid, period.origin, period.get_start_attr(), period.get_end_attr())
1.49 + self.obj.set_period(period)
1.50 + method = "COUNTER"
1.51 + else:
1.52 + return None
1.53
1.54 if not attendee_attr:
1.55 return None
1.56 @@ -57,7 +81,7 @@
1.57 self.set_sequence(False)
1.58
1.59 message = self.messenger.make_outgoing_message(
1.60 - [self.obj.to_part("REPLY")],
1.61 + [self.obj.to_part(method)],
1.62 [organiser],
1.63 outgoing_bcc=get_address(self.user)
1.64 )
1.65 @@ -69,14 +93,30 @@
1.66
1.67 if __name__ == "__main__":
1.68 try:
1.69 - minargs = 3; maxargs = 5
1.70 - accept, store_dir, user, uid, recurrenceid = (sys.argv[1:maxargs+1] + ([None] * (maxargs - minargs)))[:maxargs]
1.71 + action, store_dir, user = sys.argv[1:4]
1.72 + if action == "counter":
1.73 + start, end = sys.argv[4:6]
1.74 + i = 6
1.75 + else:
1.76 + start, end = None, None
1.77 + i = 4
1.78 + uid, recurrenceid = (sys.argv[i:i+2] + [None] * 2)[:2]
1.79 except ValueError:
1.80 - print >>sys.stderr, "Need 'accept' or 'decline', a store directory, user URI, event UID and optional RECURRENCE-ID."
1.81 - print >>sys.stderr, "The RECURRENCE-ID must be in the form employed by the store."
1.82 - print >>sys.stderr
1.83 - print >>sys.stderr, "Alternatively, omit the UID and RECURRENCE-ID and provide event-only details on standard input"
1.84 - print >>sys.stderr, "to force the script to handle an event not already present in the store."
1.85 + print >>sys.stderr, """\
1.86 +Need 'accept', 'counter' or 'decline', a store directory, user URI, any
1.87 +counter-proposal datetimes (see below), plus the appropriate event UID and
1.88 +RECURRENCE-ID (if a recurrence is involved).
1.89 +
1.90 +The RECURRENCE-ID must be in exactly the form employed by the store, not a
1.91 +different but equivalent representation.
1.92 +
1.93 +Alternatively, omit the UID and RECURRENCE-ID and provide event-only details on
1.94 +standard input to force the script to handle an event not already present in the
1.95 +store.
1.96 +
1.97 +If 'counter' has been indicated, alternative start and end datetimes are also
1.98 +required.
1.99 +"""
1.100 sys.exit(1)
1.101
1.102 store = imip_store.FileStore(store_dir)
1.103 @@ -92,7 +132,7 @@
1.104
1.105 obj = Object(fragment)
1.106 handler = TestClient(obj, user, Messenger(), store)
1.107 - response = handler.handle_request(accept == "accept")
1.108 + response = handler.handle_request(action, start, end)
1.109
1.110 if response:
1.111 if uid is not None: