homePIX gets a Calendar View
By: Elliott Bignell
Tags: Web Design, Django, Stock Photos, Web Development, Photography

The Technical Bit

This seems to be a good place to start explaining how a user cloning the project off GitHub could go about adding their own functionality, as the Calendar view covers most of it. So here goes:

New entries in urls.py

In the urls.py file, you’ll need a mapping to the view. As you can see, I’m using a class-based view called CalendarView, cleaving as closely as possible to the rule of least astonishment. That view is defined in views.py. Here’s what it looks like, and bear in mind that it is a DERIVED class, based on a lot of other code that will be explained another time:

The ellipses on lines 740 and 759 are to paper over some fairly ugly code with several nested loops and trivial lists. Not only is this an offence against the developer’s aesthetic, it also takes up too much space. A refactoring is planned… Python Django makes it deliberately hard to do complex conditional logic in a template, so it’s best to do it in the view. What I am basically doing behind the ellipses is packing the photo URLs and their corresponding dates into a nested dictionary of groups of years, each year containing groups of months, each month containing groups of seven days. These last DO NOT yet line up with calendar weeks, as mentioned above. As for the individual methods:

build_calendar is a utility to build the nested structure described above.

get_context_data marshals other information that the HTML template will need to render the page, such as the day names for a particular day index. This will be important later for localisation.

get_queryset is a standard method for a class-based View in Django, and calls up that nested structure for the view to iterate through in rendering the page.

form_valid validates any arguments defining a date range, which will become important in looking up a calendar section from other pages.

In filters.py, in the templatetags directory, there are a couple of utilities to get formatted date parts for the template to render:

New filters in filters.py

Lastly, there’s the HTML template itself.

Part of calendar_inc.html

You’ll see that this contains a mixture of HTML and Django template markup. It takes some getting used to, but is not that complex. There are also some styles in there which I have not yet taken out into a CSS static file.

So, that is a very broad outline of how generally to add a view using Python Django, and specifically how to add your own to homePIX. I’ve left out the CSS, and I can promise you that this is where the devil hides in the details. Getting the calendar to look just the way you want it is much harder than just adding a new view. (Or perhaps you’re all talented web designers and it’s trivial for you, but the Python code is pure Klingon.)

The list of TODOs is prodigious. I still have to clean up the code, add documentation markup, deal with localisation, finish off the formatting and alignment. Then I have to make it possible for a user following up with their own site to personalise it as easily as possible. That involves nailing down the customisation of Bootstrap, among other things.

So I’d like to start a conversation. Let me know what you think of the calendar view and what you want to have added. You can say, “But I wanted a zoomable map,” and I’ll say, yes, but that’s on another list. What I’d really like now is for someone to start using homePIX to build their own site. Then we’ll see if we can take back the stock photography market for photographers.