I need to pull events from a Google Calendar in order to present them on a site built with Django. I’m not sure what’s the best way of doing it, but I believe the data should be cached (my site/app doesn’t use any caching today).
As I don’t need an event archive, I’m want to avoid using the db.
This is the approach I’m considering:
- (0) Check age of cached calendar file, if too old:
- Download iCalendar (.ics) file from Google (not sure if I should use ics. Maybe RSS/XML is better?)
- Parse file with iCalendar (Python iCalendar lib)
- Generate / render new html output from calendar which can be included as static file and presented on the Django site
Suggestions for improving the procedure, or parts of it, is appreciated.
UPDATE: I’ve considered using JavaScript, but then if the API is down the data will be unavailable. But if this is considered an acceptable solution, I’ll have to look more into it.
I’d agree with @marr75, in that you can probably achieve what you want to do using JavaScript (see Google’s JavaScript page for the GCal API).
One solution would be to write your own custom management command that populates Django’s low level cache, and then use these variables inside your template. You could then set up this management command to run periodically as a cronjob to keep the data fresh. You don’t have to cache individual variables – you could always cache a whole block of HTML constructed using iCalendar (which I’m assuming is this).
If you gave a little more detail of what it is then I can give an answer that’s slightly more helpful than that. As an aside, you might also want to have a look at Django’s per-view cache decorator.
EDIT: An example might look like this:
And then in your view, you can just pass that cached HTML back to it like this: