Skip to content

Commit 152e2a7

Browse files
author
api.jscudder
committed
Added batch execution method to the calendar service so it can now perform batch operations.
1 parent 70281b6 commit 152e2a7

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/gdata/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ def AddQuery(self, url_string=None, entry=None, batch_id_string=None):
680680
entry = self.AddBatchEntry(entry=entry, id_url_string=url_string,
681681
batch_id_string=batch_id_string,
682682
operation_string=BATCH_QUERY)
683+
684+
def GetBatchLink(self):
685+
for link in self.link:
686+
if link.rel == 'http://schemas.google.com/g/2005#batch':
687+
return link
688+
return None
683689

684690

685691
def BatchFeedFromString(xml_string):

src/gdata/calendar/service.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
import atom
4242

4343

44+
DEFAULT_BATCH_URL = ('http://www.google.com/calendar/feeds/default/private'
45+
'/full/batch')
46+
47+
4448
class Error(Exception):
4549
pass
4650

@@ -424,6 +428,32 @@ def UpdateAclEntry(self, edit_uri, updated_rule, url_params=None,
424428
else:
425429
return response
426430

431+
def ExecuteBatch(self, batch_feed, url,
432+
converter=gdata.calendar.CalendarEventFeedFromString):
433+
"""Sends a batch request feed to the server.
434+
435+
The batch request needs to be sent to the batch URL for a particular
436+
calendar. You can find the URL by calling GetBatchLink().href on the
437+
CalendarEventFeed.
438+
439+
Args:
440+
batch_feed: gdata.calendar.CalendarEventFeed A feed containing batch
441+
request entries. Each entry contains the operation to be performed
442+
on the data contained in the entry. For example an entry with an
443+
operation type of insert will be used as if the individual entry
444+
had been inserted.
445+
url: str The batch URL for the Calendar to which these operations should
446+
be applied.
447+
converter: Function (optional) The function used to convert the server's
448+
response to an object. The default value is
449+
CalendarEventFeedFromString.
450+
451+
Returns:
452+
The results of the batch request's execution on the server. If the
453+
default converter is used, this is stored in a CalendarEventFeed.
454+
"""
455+
return self.Post(batch_feed, url, converter=converter)
456+
427457

428458
class CalendarEventQuery(gdata.service.Query):
429459

tests/gdata_tests/calendar/service_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,46 @@ def testPostQueryUpdateAndDeleteEvents(self):
279279
self.assertEquals(after_delete_query_result.entry[0].event_status.value,
280280
'CANCELED')
281281

282+
def testCreateAndDeleteEventUsingBatch(self):
283+
# Get random data for creating event
284+
r = random.Random()
285+
r.seed()
286+
random_event_number = str(r.randint(100000,1000000))
287+
random_event_title = 'My Random Comments Test Event %s' % (
288+
random_event_number)
289+
290+
# Set event data
291+
event = gdata.calendar.CalendarEventEntry()
292+
event.author.append(atom.Author(name=atom.Name(text='GData Test user')))
293+
event.title = atom.Title(text=random_event_title)
294+
event.content = atom.Content(text='Picnic with some lunch')
295+
296+
# Form a batch request
297+
batch_request = gdata.calendar.CalendarEventFeed()
298+
batch_request.AddInsert(entry=event)
299+
300+
# Execute the batch request to insert the event.
301+
self.cal_client.ProgrammaticLogin()
302+
batch_result = self.cal_client.ExecuteBatch(batch_request,
303+
gdata.calendar.service.DEFAULT_BATCH_URL)
304+
305+
self.assertEquals(len(batch_result.entry), 1)
306+
self.assertEquals(batch_result.entry[0].title.text, random_event_title)
307+
self.assertEquals(batch_result.entry[0].batch_operation.type,
308+
gdata.BATCH_INSERT)
309+
self.assertEquals(batch_result.GetBatchLink().href,
310+
gdata.calendar.service.DEFAULT_BATCH_URL)
311+
312+
# Create a batch request to delete the newly created entry.
313+
batch_delete_request = gdata.calendar.CalendarEventFeed()
314+
batch_delete_request.AddDelete(entry=batch_result.entry[0])
315+
316+
batch_delete_result = self.cal_client.ExecuteBatch(batch_delete_request,
317+
batch_result.GetBatchLink().href)
318+
self.assertEquals(len(batch_delete_result.entry), 1)
319+
self.assertEquals(batch_delete_result.entry[0].batch_operation.type,
320+
gdata.BATCH_DELETE)
321+
282322
def testCorrectReturnTypesForGetMethods(self):
283323
self.cal_client.ProgrammaticLogin()
284324

0 commit comments

Comments
 (0)