70922bc34b6c — Oben Sonne 11 years ago
Use dynamic URLs in labels view
3 files changed, 21 insertions(+), 16 deletions(-)

M app/diggie/templates/diggie/labels.html
M app/diggie/urls.py
M app/diggie/views.py
M app/diggie/templates/diggie/labels.html +14 -8
@@ 10,21 10,27 @@ 
 <table class="listing">
   <tr>
   <th class="name">
-    <a href="{{ list }}/{% if sfield == 'name' and sdir == '' %}-{% else %}{% endif%}name"
-       title="Sort {% if sfield == 'name' and sdir == '' %}down{% else %}up{% endif%} by name">
+    {% if sort == "name" and dir == "up" %}
+    <a href="{% url diggie.views.labels %}?sort=name&dir=down" title="Sort down by name">
+    {% else %}
+    <a href="{% url diggie.views.labels %}?sort=name&dir=up" title="Sort up by name">
+    {% endif %}
     Name
     </a>
-    {% if sfield == 'name' %}
-      {% if sdir == '' %}&uarr;{% else %}&darr;{% endif%}
+    {% if sort == 'name' %}
+      {% if dir == 'up' %}&uarr;{% else %}&darr;{% endif%}
     {% endif %}
   </th>
   <th class="pcount">
-    <a href="{{ list }}/{% if sfield == 'pcount' and sdir == '-' %}{% else %}-{% endif%}pcount"
-       title="Sort {% if sfield == 'pcount' and sdir == '-' %}up{% else %}down{% endif%} by number of pages">
+    {% if sort == "pcount" and dir == "down" %}
+    <a href="{% url diggie.views.labels %}?sort=pcount&dir=up" title="Sort up by number of pages">
+    {% else %}
+    <a href="{% url diggie.views.labels %}?sort=pcount&dir=down" title="Sort down by number of pages">
+    {% endif %}
     Pages
     </a>
-    {% if sfield == 'pcount' %}
-      {% if sdir == '' %}&uarr;{% else %}&darr;{% endif%}
+    {% if sort == 'pcount' %}
+      {% if dir == 'up' %}&uarr;{% else %}&darr;{% endif%}
     {% endif %}
   </th>
   </tr>

          
M app/diggie/urls.py +0 -1
@@ 7,7 7,6 @@ urlpatterns = patterns('',
 
     (r'^$', 'diggie.views.main'),
     (r'^labels/?$', 'diggie.views.labels'),
-    (r'^labels/(?P<sort>-?(?:name|pcount))/?$', 'diggie.views.labels'),
     (r'^pages/?$', 'diggie.views.pages'),
     (r'^pages/(?P<label>%s)/?$' % RE_LABEL_NAME, 'diggie.views.pages'),
     (r'^page/(?P<name>%s)/?$' % RE_PAGE_NAME, 'diggie.views.page'),

          
M app/diggie/views.py +7 -7
@@ 93,25 93,25 @@ def main(request):
 
 @never_cache                    # no upstream caching
 @cache_page(A_VERY_LONG_TIME)   # server-side caching
-def labels(request, sort="name"):
+def labels(request):
     _dview("labels", request)
 
     rqc = _init()
 
-    sfield, sdir = sort.lstrip("-"), sort.startswith("-") and "-" or ""
+    dir = _rq_param(request.GET, "dir", ("up", "down"))
+    sort = _rq_param(request.GET, "sort", ("name", "pcount"))
 
-    if sort.lstrip("-") == "pcount":
+    if sort == "pcount":
         skey = lambda l: l.page_set_nd.count()
     else:
         skey = lambda l: l.name
 
     labels = [l for l in Label.objects.all() if l.page_set_nd.exists()]
-    labels.sort(key=skey, reverse=sdir=="-")
+    labels.sort(key=skey, reverse=dir=="down")
 
     rqc['labels'] = labels
-    rqc['list'] = "/labels"
-    rqc['sfield'] = sfield
-    rqc['sdir'] = sdir
+    rqc['dir'] = dir
+    rqc['sort'] = sort
 
     return render_to_response('diggie/labels.html', rqc)