amirpk / gdata-python-client

Automatically exported from code.google.com/p/gdata-python-client
0 stars 0 forks source link

Problem using RecurrenceExceptions #234

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm attempting to use the python gdata API to create recurring events with
exceptions. In the code below google_event is is the result of calling
InsertGoogleEvent, which does result in a recurring event. Now I need to
cancel some of the events stored in palm_event["exceptions"]. Below is my
attempt on creating the exceptions, however it doesn't seem to work. I
don't get any errors, but when I look at my calendar on Google the
particular dates haven't been canceled. Any help would be appreciated. Some
examples of this added to the documentation would be most helpful.

        for exception in palm_event["exceptions"]:
            # create a RecurrenceException with an eventStatus of canceled
            # http://schemas.google.com/g/2005#event.canceled
            print("Exception on %s" % (exception))

            # create a canceled event to represent the exception 
            event_status = gdata.calendar.EventStatus()
            event_status.value = 'CANCELED'
            exception_event =
gdata.calendar.CalendarEventEntry(event_status=event_status)
            exception_google_format = exception.strftime(google_date_format)

exception_event.when.append(gdata.calendar.When(start_time=exception_google_form
at,
end_time=exception_google_format))
            #exception_event = cal_client.InsertEvent(exception_event, uri)

            # add the exception to the google event
            google_exception =
gdata.calendar.RecurrenceException(entry_link=exception_event,
original_event=google_event)
            google_event.recurrence_exception.append(google_exception)

            exception_event = cal_client.InsertEvent(exception_event, uri)

Original issue reported on code.google.com by jpsch...@gmail.com on 29 Mar 2009 at 8:13

GoogleCodeExporter commented 9 years ago
You need to have an OriginalEvent in the exception_event. I used .NET for this 
but it
should be very similar in Python.

e.g. (untested):
exception_event.original_event = OriginalEvent(id = '<short ID of the original 
event
you are canceling>', href = '<self-link URL of the original event you are 
canceling>'
when = <a gdata.calendar.When with the start time of the recurrence you are 
canceling>)

The recurrenceException is not writable, it is only provided in the composite 
feed
which is read-only.

Original comment by william....@sadasystems.com on 27 Apr 2009 at 7:38

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I appreciate the suggestion. I didn't realize that recurrenceException was 
read-only.
Below is my revised attempt at making this work. Still doesn't cancel the event 
though.

        google_cal_id = get_google_cal_id(google_cal) 
        uri = "/calendar/feeds/%s/private/full" % (google_cal_id) 
        google_event = cal_client.InsertEvent(google_event, uri) 

        # FIXME Still not canceling the particular date yet - put in issue 
        # 234 for this 
        for exception in palm_event["exceptions"]: 
            # create a RecurrenceException with an eventStatus of canceled 
            # http://schemas.google.com/g/2005#event.canceled 
            print("Exception on %s" % (exception)) 

            # create a canceled event to represent the exception  
            event_status = gdata.calendar.EventStatus() 
            event_status.value = 'CANCELED' 
            exception_event =
gdata.calendar.CalendarEventEntry(event_status=event_status, original_event =
google_event) 
            exception_google_format = exception.strftime(google_date_format) 

exception_event.when.append(gdata.calendar.When(start_time=exception_google_form
at,
end_time=exception_google_format)) 

            # add the exception to the google event 
            #google_exception =
gdata.calendar.RecurrenceException(entry_link=exception_event,
original_event=google_event) 
            #google_event.recurrence_exception.append(google_exception) 

            exception_event = cal_client.InsertEvent(exception_event, uri) 

Original comment by jpsch...@gmail.com on 28 Apr 2009 at 2:58

GoogleCodeExporter commented 9 years ago
The original_event is *not* the original CalendarEventEntry (google_event in 
your
code), it is another element (gd:originalEvent, Python class
gdata.calendar.OriginalEvent). 

Set the originalEvent's ID to the short id of your google_event, set the href 
to the
self-link of your google_event, and set the when to a gdata.calendar.When with 
the
start_time set to the start_time of the recurrence you are canceling. Also, 
watch out
for DST if that applies to you.

I think this should be better documented :).

Original comment by william....@sadasystems.com on 28 Apr 2009 at 10:16

GoogleCodeExporter commented 9 years ago
OK, that seems to be making some progress. At least I'm getting errors now :)

Now I've got this error:
  File "./jppy_google_sync.py", line 176, in create_google_event
    exception_event = cal_client.InsertEvent(exception_event, uri)
  File "/usr/lib64/python2.6/site-packages/gdata/calendar/service.py", line 149, in
InsertEvent
    converter=gdata.calendar.CalendarEventEntryFromString)
  File "/usr/lib64/python2.6/site-packages/gdata/service.py", line 866, in Post
    media_source=media_source, converter=converter)
  File "/usr/lib64/python2.6/site-packages/gdata/service.py", line 951, in PostOrPut
    headers=extra_headers)
  File "/usr/lib64/python2.6/site-packages/atom/service.py", line 166, in request
    content_length = CalculateDataLength(data)
  File "/usr/lib64/python2.6/site-packages/atom/service.py", line 722, in
CalculateDataLength
    return len(str(data))
  File "/usr/lib64/python2.6/site-packages/atom/__init__.py", line 322, in __str__
    return self.ToString()
  File "/usr/lib64/python2.6/site-packages/atom/__init__.py", line 319, in ToString
    return ElementTree.tostring(self._ToElementTree(), encoding=string_encoding)
  File "/usr/lib64/python2.6/site-packages/atom/__init__.py", line 314, in _ToElementTree
    self._AddMembersToElementTree(new_tree)
  File "/usr/lib64/python2.6/site-packages/atom/__init__.py", line 276, in
_AddMembersToElementTree
    member._BecomeChildElement(tree)
  File "/usr/lib64/python2.6/site-packages/atom/__init__.py", line 302, in
_BecomeChildElement
    self._AddMembersToElementTree(new_child)
  File "/usr/lib64/python2.6/site-packages/atom/__init__.py", line 282, in
_AddMembersToElementTree
    tree.attrib[xml_attribute] = member.decode(MEMBER_STRING_ENCODING)
AttributeError: 'Id' object has no attribute 'decode'

With this code (must be getting the id from the wrong place):

Yes, I've noticed that this isn't documented very well. Perhaps some wiki pages
should be written with examples.

Original comment by jpsch...@gmail.com on 28 Apr 2009 at 12:19

GoogleCodeExporter commented 9 years ago
I finally figured this out by using Java API and checking for the errors that it
gave. The keys were this:

1) You need to create a When object to be the date of the exception and match 
the
time of the original event
2) You need to create an OriginalEvent object with this when object and the 
base ID
of the original event that you're creating an exception for.
3) Create the exception as a regular event with original_event set to the
OriginalEvent object and status set to Canceled.

Note that this base ID is not easily retrieved. If you use the 'id' field of the
object you'll be wrong, what you actually want is just the end of this field. 
This
method does that:
def get_google_base_id(google_base_entry):
    '''Get the ID of the google calendar, this is the last part of the URI'''
    base_id = google_base_entry.id.text.split('/')[ - 1]
    return base_id.replace("%40", "@")

Why this ID isn't available is beyond me. If someone else knows where to get 
this ID
without my extra method, please let me know as this seems rather hackish.

Original comment by jpsch...@gmail.com on 18 Nov 2009 at 1:03

GoogleCodeExporter commented 9 years ago
Closing the issue, please re-open if you are still experiencing it.

Original comment by ala...@google.com on 7 Oct 2011 at 11:13