Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iCal Feed Served Through Django Not Recognized

I'm having difficulty generating a valid iCal stream using Django.

The problem I'm having is that my iCal file and stream is valid. I can visit the stream URL to download the .ics file and validate it only, import it to iCalendar or Google Calendar just fine, etc. In fact, if I host the same .ics file as a static file on my Django server and subscribe to that static URL from Google Calendar, it also works just fine. However, when giving Google Calendar the feed URL, no luck.

I've seen several similar questions on Stack Overflow and when searching Google, but most of them have the exact opposite problem (file doesn't work, stream does, or stream only works sometimes). Best I can tell from the example code they offer, I'm doing something very similar, but my feed doesn't work, so I must be missing something.

Here is the contents of the .ics file.

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Tester//Version 0.0.1//EN
BEGIN:VEVENT
SUMMARY:iCal Timed
DTSTART:20140408T202000Z
DTEND:20140408T202000Z
DTSTAMP:20141107T153835Z
UID:event_1
DESCRIPTION:iCal Comment
END:VEVENT
BEGIN:VEVENT
SUMMARY:iCal All Day
DTSTART;VALUE=DATE:20140408
DTEND;VALUE=DATE:20140409
DTSTAMP:20141107T153835Z
UID:1
DESCRIPTION:iCal Comment
END:VEVENT
END:VCALENDAR

I am using Django 1.7 with icalendar 3.8.3, and below is the code used to generated the feed.

def ical_feed(request):
    cal = icalendar.Calendar()
    cal.add('prodid', '-//Tester//Version 0.1.1//EN')
    cal.add('version', '2.0')

    for e in Event.events.filter(user_id=request.user.pk).iterator():
        event = icalendar.Event()
        event['uid'] = unicode(e.pk)
        ...
        cal.add_component(event)

    stream = cal.to_ical()

    response = HttpResponse(stream, content_type='text/calendar; charset=utf-8')
    response['Filename'] = request.user.username + '.ics'
    response['Content-Disposition'] = 'attachment; filename=' + request.user.username + '.ics'

    return response

This shouldn't matter, but Django is served through Apache 2.4.7 (Ubuntu). Aside from setting the Content-Type in my HttpResponse, is there something I need to be doing in Apache as well to make readers see this as a stream? I've heard of django-ical, and it seems like an unnecessary amount of overhead for what I'm doing, and looking at its code it sets the response with headers the same way I'm already doing.

Any insight would be greatly appreciated.

UPDATE

I misunderstood the UID, thinking it was simple an identifier for items within my feed. However, that did not resolve the issue. To further clarify the question, when trying to access my calendar from an iCal feed validator, iCalendar, or Google Calendar, I was met with an "failed iCal validation" error, and the size of the calendar always resulted in 0 bytes.

Jerry's suggestion to CURL the URL was right on. What I found was a bit embarassing, but I was so focused on the code implentation that I never walked all the way up to the Django view-level function. CURLing the URL showed that 0 bytes were returned because the user was being redirected to the /login page. I had the @login_required decorator on the view.

Thanks very much for your help, I'm marking your answer as correct, Jerry, as it provided the debugging I needed to find the solution.

like image 621
alexdlaird Avatar asked Mar 19 '26 09:03

alexdlaird


1 Answers

First, try looking at your headers from a client to make sure that the headers aren’t being mangled or lost somewhere. You can use curl --head and then the URL to see the full headers that a client on the same computer will receive. (If you are changing behavior based on cookies or agent information, you may want to use a plug-in or the developer tools for that particular client to see the header information.)

Second, your UIDs are not unique across the universe. Those UIDs could be duplicated somewhere else. One common practice is to append your own unique hostname to the UID to ensure that they are unique.

Finally, you didn’t mention how the feed fails in Google Calendar and in iCal. Is the feed simply ignored as if it were empty? Is an error generated in either case? Or are the items showing up but not showing up correctly?

like image 119
Jerry Stratton Avatar answered Mar 21 '26 23:03

Jerry Stratton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!