Login

URL Query Parameter Menu

Author:
drg006
Posted:
May 24, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

Helps create menus that modify URL query parameters. Create the Menu object in your view and put it in the template context.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Menu:
    """A class for generating a renderable menu that modifies URL parameters."""
    def __init__(self, request, key, items, default=None, exclude=['page']):
        """Constructs a new Menu.
        request: the request object for the current view.
        key: the name of the URL parameter for this Menu.
        items: a sequence of MenuItems.
        default: the key of the MenuItem to be selected by default.
        exclude: a list of URL parameters to remove when constructing the links for each MenuItem.
        """
        self.key = key
        self.items = items
        value = request.GET.get(self.key, default)
        self.selected = value
        for item in self.items:
            item.selected = (value == item.key)
            copy = request.GET.copy()
            copy[self.key] = item.key
            for key2 in exclude:
                if key2 in copy:
                    del copy[key2]
            item.link = u'%s?%s' % (request.path, copy.urlencode())
    def as_list(self):
        """Renders the Menu as an unordered list."""
        fragments = ['<ul>']
        for item in self.items:
            fragments.append('<li>')
            fragments.append(str(item))
            fragments.append('</li>')
        fragments.append('</ul>')
        return ''.join(fragments)
    def __str__(self):
        """Renders the Menu using the default method."""
        return self.as_list()

class MenuItem:
    """A class representing an item in a Menu."""
    def __init__(self, key, label):
        """Constructs a MenuItem.
        key: the value of the Menu URL parameter when this item is selected.
        label: the name of the menu item as it will appear on the page.
        """
        self.key = key
        self.label = label
    def __str__(self):
        """Renders the MenuItem.  
        The item appears as a link only if not selected.
        """
        if self.selected:
            return u'%s' % (self.label)
        else:
            return u'<a href="%s">%s</a>' % (self.link, self.label)
        
def sample():
    """Returns a rendered sample menu.
    >>> sample()
    '<ul><li>Hot!</li><li><a href="/home/?topic=new&search=blah">New!</a></li></ul>'
    """
    from django.http import HttpRequest, QueryDict
    items = [MenuItem('hot', "Hot!"),
             MenuItem('new', "New!"), ]
    request = HttpRequest()
    request.path = '/home/'
    request.GET = QueryDict("topic=hot&search=blah&page=3")
    menu = Menu(request, 'topic', items)
    return menu.as_list()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.