aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSlava Bacherikov <slava@bacher09.org>2012-09-14 20:12:53 +0300
committerSlava Bacherikov <slava@bacher09.org>2012-09-14 20:12:53 +0300
commit81dbdfc8901de049ee866f6733554416404102c2 (patch)
treeac5cf29457c328af348522e6c3153b6cd9d32ada
parentAdd TODO (diff)
downloadgentoo-packages-81dbdfc8901de049ee866f6733554416404102c2.tar.gz
gentoo-packages-81dbdfc8901de049ee866f6733554416404102c2.tar.bz2
gentoo-packages-81dbdfc8901de049ee866f6733554416404102c2.zip
Sorting maintainers by many attributes
-rw-r--r--gpackages/apps/generic/views.py69
-rw-r--r--gpackages/apps/packages/views.py3
-rw-r--r--gpackages/templates/maintainers.html4
3 files changed, 69 insertions, 7 deletions
diff --git a/gpackages/apps/generic/views.py b/gpackages/apps/generic/views.py
index da0048e..910d3a5 100644
--- a/gpackages/apps/generic/views.py
+++ b/gpackages/apps/generic/views.py
@@ -1,11 +1,14 @@
from django.views.generic import TemplateView, ListView
-from string import Template
-import re
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed, rfc3339_date
from django.utils import tzinfo
from django.utils.timezone import is_naive
from django.http import Http404
+from django.core.urlresolvers import reverse, NoReverseMatch
+from django.utils.html import mark_safe
+from string import Template
+from collections import namedtuple
+import re
class ContextView(object):
"Mixin to add additional data to context"
@@ -71,6 +74,23 @@ def dynamic_order(order_attr, allowed_list, reverse = None):
order = order[1:]
return order
+OrderClass = namedtuple('OrderClass', ['active', 'link', 'reverse'])
+
+class OrderClass(object):
+
+ __slots__ = ('active', 'link', 'reverse')
+
+ def __init__(self, active, link, reverse = False):
+ self.active = active
+ self.link = link
+ self.reverse = reverse
+
+ def arr(self):
+ if self.reverse:
+ return mark_safe('&darr;')
+ else:
+ return mark_safe('&uarr;')
+
class MultipleFilterListViewMixin(object):
"""Mixin for dynamical objects filtering
"""
@@ -95,9 +115,17 @@ class MultipleFilterListViewMixin(object):
"""Indicates that this attribute is m2m fields and filtering by it should done
with ``distinct`` attribute.
"""
+ view_name = str()
+ """Name of url view"""
@classmethod
def get_url_kwargs(cls, filters, order = None, rev = False):
+ """Args:
+ filters -- dict, search filters.
+ order -- order name.
+ rev -- bolean, reverse order
+ Return: dict used in reverse
+ """
kwargs = {}
for name, value in filters.iteritems():
if not value or name not in cls.allowed_filter:
@@ -114,22 +142,51 @@ class MultipleFilterListViewMixin(object):
kwargs[name] = value
- if order is not None and order in self.allowed_order:
+ if order is not None and order in cls.allowed_order:
kwargs['order'] = order
if rev:
- kwargs['rev'] = 'rev'
+ kwargs['rev'] = 'rev/'
return kwargs
+ @classmethod
+ def get_url(cls, kwargs):
+ try:
+ url = reverse(cls.view_name, kwargs = kwargs)
+ except NoReverseMatch:
+ url = '#'
+
+ return url
+
def get_context_data(self, **kwargs):
"""In addition to default context value will return all filters as
:py:class:`.dict`.
"""
cd = super(MultipleFilterListViewMixin, self).get_context_data(**kwargs)
cd['filters_dict'] = self.queries_dict
+ cd['orders'] = self.get_orders_context()
return cd
+ def get_orders_context(self):
+ dct = {}
+ for name in self.allowed_order:
+ if name == self.order:
+ active = True
+ else:
+ active = False
+
+ is_reverse = not self.reverse
+
+ link = self.get_url_kwargs(self.queries_dict, name, is_reverse)
+ link = self.get_url(link)
+ obj = OrderClass(active = active, link = link, reverse = is_reverse)
+ if name is None:
+ dct['default'] = obj
+ else:
+ dct[name] = obj
+ return dct
+
def get_base_filters(self):
qs = filter_req(self.request.GET, self.allowed_filter)
qs.update(filter_req(self.kwargs, self.allowed_filter))
@@ -176,6 +233,10 @@ class MultipleFilterListViewMixin(object):
if order_attr not in self.allowed_order:
raise Http404('no such order')
+
+ self.order = order_attr
+ self.reverse = reverse
+
order = dynamic_order(order_attr, self.allowed_order, reverse)
return order
diff --git a/gpackages/apps/packages/views.py b/gpackages/apps/packages/views.py
index b597a12..2ceb965 100644
--- a/gpackages/apps/packages/views.py
+++ b/gpackages/apps/packages/views.py
@@ -114,7 +114,7 @@ class HerdsListView(ArchesCacheViewMixin, ContextListView):
class MaintainersListView(CacheFilterListView, ContextListView):
cache_time = 560
allowed_filter = { 'dev' : 'is_dev',
- 'herd' : 'herdsmodel__name'}
+ 'herd' : 'herdsmodel__name' }
allowed_order = { None: 'name', 'dev' : 'is_dev'}
boolean_filters = ('dev',)
paginate_by = 40
@@ -122,6 +122,7 @@ class MaintainersListView(CacheFilterListView, ContextListView):
template_name = 'maintainers.html'
queryset = MaintainerModel.objects.only('name', 'email' ).all()
context_object_name = 'maintainers'
+ view_name = 'maintainers'
class RepositoriesListView(ArchesCacheViewMixin, ContextListView):
extra_context = {'page_name': 'Repsitories',}
diff --git a/gpackages/templates/maintainers.html b/gpackages/templates/maintainers.html
index b337f18..4303c9d 100644
--- a/gpackages/templates/maintainers.html
+++ b/gpackages/templates/maintainers.html
@@ -14,9 +14,9 @@
</colgroup>
<thead>
<tr>
- <th>{% trans 'Name' %}</th>
+ <th><a href="{{ orders.default.link }}">{% trans 'Name' %} {{ orders.default.arr }}</a></th>
<th>{% trans 'Email' %}</th>
- <th>{% trans 'Gentoo dev' %}</th>
+ <th><a href="{{ orders.dev.link }}">{% trans 'Gentoo dev' %} {{ orders.dev.arr }}</a></th>
</tr>
</thead>
<tbody>