2011-10-08 18 views
2

我嘗試使用Google API for python刪除日曆中的所有事件。 如果我逐個刪除事件,這工作正常,但它很長,沒有未優化,所以我嘗試使用ExecuteBatch做同樣的事情,我用它插入日曆中的很多條目(並且這在插入之前工作正常) 。Google Cal API Python:使用ExecuteBatch失敗進行多次刪除

def DeleteAllEntryOfCalendar(calendar_client, name_calendar): 
    #Request_feed for doing the delete 
    request_feed = gdata.calendar.CalendarEventFeed() 
    #Get the Uri of the cal we've to delete all entries 
    uri = GetUri(calendar_client, name_calendar) 
    feed = calendar_client.GetAllCalendarsFeed() 
    for a_cal in feed.entry: 
     if a_cal.title.text == name_calendar: 
      calendar = a_cal  
    #For each entry in the cal, do a delete request entry in request_feed 
    query = gdata.calendar.client.CalendarEventQuery() 
    query.max_results = 1000 
    feed = calendar_client.GetCalendarEventFeed(uri=uri, q=query) 
    for an_entry in feed.entry: 
     an_entry.batch_id = gdata.BatchId(text='delete-request') 
     request_feed.AddDelete(entry=an_entry)  
     entry = '' 

    # submit the batch request to the server 
    response_feed = calendar_client.ExecuteBatch(request_feed, uri+u'/batch') 

而且我得到了錯誤:

Traceback (most recent call last): 
    File "c.py", line 253, in <module> 
    DeleteAllEntryOfCalendar(client, name_calendar) 
    File "c.py", line 187, in DeleteAllEntryOfCalendar 
    response_feed = calendar_client.ExecuteBatch(request_feed, uri+u'/batch') 
    File "/usr/local/lib/python2.6/dist-packages/gdata/calendar/client.py", line 432, in  execute_batch 
    return self.Post(batch_feed, url, desired_class=desired_class) 
    File "/usr/local/lib/python2.6/dist-packages/gdata/client.py", line 681, in post 
    entry.to_string(get_xml_version(self.api_version)), 
AttributeError: 'CalendarEventFeed' object has no attribute 'to_string' 

我再說一遍,當我做同樣的插入事件能正常工作。 我搜索了很多,並嘗試了很多的可能性,我無法做到這一點。 如果你已經一個想法......

回答

1

使用

gdata.calendar.data.CalendarEventFeed 

在GDATA Python庫的一些對象有兩個版本,你可以在一起不能混用。