Browse Source

Allow different ordering in /browse/

pull/1/head
Hidden Z 9 years ago
parent
commit
75de76cf4c
  1. 6
      pyi2phosts/latest/views.py
  2. 20
      pyi2phosts/lib/generic.py
  3. 8
      pyi2phosts/templates/browse.html
  4. 4
      pyi2phosts/templates/latest.html

6
pyi2phosts/latest/views.py

@ -3,7 +3,7 @@ import datetime @@ -3,7 +3,7 @@ import datetime
from django.conf import settings
from pyi2phosts.postkey.models import i2phost
from pyi2phosts.lib.generic import HostsListsView
from pyi2phosts.lib.generic import LocalObjectList
def get_latest():
now_date = datetime.datetime.utcnow()
@ -12,7 +12,7 @@ def get_latest(): @@ -12,7 +12,7 @@ def get_latest():
date_added__range=(start_date, now_date)).order_by("-date_added")[:settings.LATEST_HOSTS_COUNT]
return qs
class LatestHostsListsView(HostsListsView):
class LatestHostsListsView(LocalObjectList):
""" Renders list of latest active hosts added """
def get_context_data(self, **kwargs):
@ -26,5 +26,5 @@ class LatestHostsListsView(HostsListsView): @@ -26,5 +26,5 @@ class LatestHostsListsView(HostsListsView):
queryset = get_latest()
template_name = 'latest.html'
template_object_name = 'host_list'
context_object_name = 'host_list'
paginate_by = 40

20
pyi2phosts/lib/generic.py

@ -44,7 +44,23 @@ class FaqView(LocalObjectList): @@ -44,7 +44,23 @@ class FaqView(LocalObjectList):
class HostsListsView(LocalObjectList):
""" Renders list of active hosts """
queryset = i2phost.objects.filter(activated=True).order_by("name")
template_name = 'browse.html'
def get_queryset(self):
allowed_orders = ['name', 'last_seen', 'date_added']
self.order_by = self.request.GET.get('order', 'name')
if self.order_by not in allowed_orders:
self.order_by = 'name'
qs = super(HostsListsView, self).get_queryset()
return qs.order_by(self.order_by)
def get_context_data(self, **kwargs):
""" we should pass order_by to template to not lose it while paginating """
context = super(LocalObjectList, self).get_context_data(**kwargs)
context.update({
'order': self.order_by,
})
return context
queryset = i2phost.objects.filter(activated=True)
template_name = 'browse.html'
context_object_name = 'host_list'
paginate_by = 40

8
pyi2phosts/templates/browse.html

@ -7,8 +7,10 @@ @@ -7,8 +7,10 @@
{% endblock %}
{% block content %}
{% block table_header %}
<table>
<tr><td>{% trans "Host" %}</td><td>{% trans "Last seen" %}</td><td>{% trans "Date added" %}</td><td>{% trans "Description" %}</td></tr>
<tr><td><a href="?order=name">{% trans "Host" %}</a></td><td><a href="?order=last_seen">{% trans "Last seen" %}</a></td><td><a href="?order=date_added">{% trans "Date added" %}</a></td><td>{% trans "Description" %}</td></tr>
{% endblock table_header %}
{% for host in host_list %}
<tr><td><a href="http://{{ host.name }}/?i2paddresshelper={{host.b64hash}}">{{ host.name }}</a></td><td>{{ host.last_seen }}</td>
<td>{{ host.date_added }}</td><td>{{ host.description }}</td></tr>
@ -19,14 +21,14 @@ @@ -19,14 +21,14 @@
<div class="pager">
{% if page_obj.has_previous %}
<span class="page">
<a href="?page={{ page_obj.previous_page_number }}">&lt; Prev</a>
<a href="?page={{ page_obj.previous_page_number }}&order={{ order }}">&lt; Prev</a>
</span>
{% endif %}
<span class="current">Page {{ page_obj.number }} of {{ paginator.num_pages }}</span>
{% if page_obj.has_next %}
<span class="page"><a href="?page={{ page_obj.next_page_number }}">Next &gt;</a></span>
<span class="page"><a href="?page={{ page_obj.next_page_number }}&order={{ order }}">Next &gt;</a></span>
{% endif %}
{{ paginator.count }} {% trans "hosts total" %}

4
pyi2phosts/templates/latest.html

@ -7,3 +7,7 @@ @@ -7,3 +7,7 @@
{% endblocktrans %}
<a href={% url 'latest-rss' %}><img src="/static/rss-grey-18.png" border="0" width="18" height="18" alt="rss"></a></h3>
{% endblock %}
{% block table_header %}
<table>
<tr><td>{% trans "Host" %}</td><td>{% trans "Last seen" %}</td><td>{% trans "Date added" %}</td><td>{% trans "Description" %}</td></tr>
{% endblock table_header %}

Loading…
Cancel
Save