Wednesday, December 24, 2014

I love computers (for at least another five minutes)

In my last post about Whatnext, I wrote that
Sometimes something that is supposed to be very simple turns out to two minutes of work, IF AND ONLY IF you already know [blah blah blah technical thing]
A big problem with using a software platform as a starting point for development is it locks you into its way of doing things, including using all of the platforms it in turn is built on.  However, sometimes it just works fine (or, I suppose, it's possible that I learning something), and something that should take two minutes not only takes only two minutes, but also saves you from something else that could have taken hours.

Example: In Whatnext, you can filter your task list by a word in the task title or notes.  Here's an example, all of my tasks regarding "windows":

Naturally, I wondered what would happen if I used spaces in the filter text.  Computer programming is generally unkind to spaces, because programmers never reached consensus on whether a space is just another letter like X or 8, or if spaces are magical indications of the difference between each us, or at least our words.  So the safe way to transmit spaces inside of URLs is to change them into something that clearly indicates that a space is just another letter like any other.  That something is often %20, for obvious reasons (that was sarcasm). I anticipated a headache around fixing my code to handle spaces properly.  First, I tried filtering by "wash windows" and was mildly stunned that it worked perfectly:
This is thanks to the Django platform. Since I already wrote this code:

    def get_context_data(self, **kwargs):
        context = super(TaskQueryMixin, self).get_context_data(**kwargs)
        state_params=QueryDict(query_string=None, mutable=True)
        state_params.setlist('tags',self.request.GET.getlist('tags'))
        state_params.__setitem__('show_closed', self.request.GET.get('show_closed', ''))
        state_params.__setitem__('show_snoozed', self.request.GET.get('show_snoozed', ''))
        state_params.__setitem__('page', self.request.GET.get('page', ''))
        context['state_url_fragment']=state_params.urlencode()
        return context

When I added this new code in the middle:

        state_params.__setitem__('name', self.request.GET.get('name', ''))
        state_params.__setitem__('notes', self.request.GET.get('notes', ''))

it all got a free ride thanks to the .urlencode() function at the end, and it changes "wash windows" into wash+windows (+ instead of %20 because reasons, I guess), and then changes it back at the appropriate time, and it all works fine. I'm nervous to search for a + only to have it fail and break the fragile feeling that I picked a decent platform.

UPDATE: + works fine. It gets encoded to %2B, obviously.

No comments :

Post a Comment