diff --git a/pyi2phosts/latest/urls.py b/pyi2phosts/latest/urls.py index 19b2db1..9798474 100644 --- a/pyi2phosts/latest/urls.py +++ b/pyi2phosts/latest/urls.py @@ -1,8 +1,9 @@ from django.conf.urls.defaults import * from pyi2phosts.lib.rss import LatestHostsFeed +from pyi2phosts.latest.views import LatestHostsListsView -urlpatterns = patterns('pyi2phosts.latest.views', - url(r'^$', 'latest', name='latest'), +urlpatterns = patterns('', + url(r'^$', LatestHostsListsView.as_view(), name='latest'), url(r'^rss/$', LatestHostsFeed(), name='latest-rss'), ) diff --git a/pyi2phosts/latest/views.py b/pyi2phosts/latest/views.py index b04ba14..01de056 100644 --- a/pyi2phosts/latest/views.py +++ b/pyi2phosts/latest/views.py @@ -1,9 +1,8 @@ import datetime -from django.views.generic import list_detail - import settings from pyi2phosts.postkey.models import i2phost +from pyi2phosts.lib.generic import HostsListsView def get_latest(): now_date = datetime.datetime.utcnow() @@ -12,16 +11,19 @@ def get_latest(): date_added__range=(start_date, now_date)).order_by("-date_added")[:settings.LATEST_HOSTS_COUNT] return qs -def latest(request): - return list_detail.object_list( - request = request, - queryset = get_latest(), - template_name = 'latest.html', - template_object_name = 'host', - paginate_by = 40, - extra_context = { - 'title': settings.SITE_NAME, - 'day_count': settings.LATEST_DAY_COUNT, - 'hosts_count': settings.LATEST_HOSTS_COUNT, - } - ) +class LatestHostsListsView(HostsListsView): + """ Renders list of latest active hosts added """ + + def get_context_data(self, **kwargs): + context = super(LatestHostsListsView, self).get_context_data(**kwargs) + context.update({ + 'title': settings.SITE_NAME, + 'day_count': settings.LATEST_DAY_COUNT, + 'hosts_count': settings.LATEST_HOSTS_COUNT + }) + return context + + queryset = get_latest() + template_name = 'latest.html' + template_object_name = 'host_list' + paginate_by = 40 diff --git a/pyi2phosts/lib/generic.py b/pyi2phosts/lib/generic.py index 68945a1..92a0572 100755 --- a/pyi2phosts/lib/generic.py +++ b/pyi2phosts/lib/generic.py @@ -1,8 +1,17 @@ from django.views.generic.base import TemplateView +from django.views.generic.list import ListView +from django.core.paginator import Paginator + from pyi2phosts.lib.utils import get_b32 +from pyi2phosts.extsources.models import ExternalSource +from pyi2phosts.postkey.models import i2phost +from pyi2phosts.postkey.templatetags import paginator + import settings class LocalTemplateView(TemplateView): + """ Renders some template with passing some local config variables """ + def get_context_data(self, **kwargs): context = super(LocalTemplateView, self).get_context_data(**kwargs) context.update({ @@ -12,3 +21,31 @@ class LocalTemplateView(TemplateView): 'b32': get_b32(settings.MY_B64) }) return context + + +class LocalObjectList(ListView): + """ Renders some list of objects """ + + def get_context_data(self, **kwargs): + context = super(LocalObjectList, self).get_context_data(**kwargs) + context.update({ + 'title': settings.SITE_NAME, + }) + return context + + +class FaqView(LocalObjectList): + """ Renders list of external sources for hosts.txt """ + + queryset = ExternalSource.objects.filter(active=True) + template_name = 'faq.html' + context_object_name = 'sources_list' + + +class HostsListsView(LocalObjectList): + """ Renders list of active hosts """ + + queryset = i2phost.objects.filter(activated=True).order_by("-last_seen") + template_name = 'browse.html' + context_object_name = 'host_list' + paginate_by = 40 diff --git a/pyi2phosts/search/urls.py b/pyi2phosts/search/urls.py index a4042eb..e9fb950 100644 --- a/pyi2phosts/search/urls.py +++ b/pyi2phosts/search/urls.py @@ -1,5 +1,6 @@ from django.conf.urls.defaults import * +from pyi2phosts.search.views import SearchedHostsListsView -urlpatterns = patterns('pyi2phosts.search.views', - (r'^$', 'search'), +urlpatterns = patterns('', + (r'^$', SearchedHostsListsView.as_view()), ) diff --git a/pyi2phosts/search/views.py b/pyi2phosts/search/views.py index a3db035..343362c 100644 --- a/pyi2phosts/search/views.py +++ b/pyi2phosts/search/views.py @@ -1,21 +1,18 @@ from django.db.models import Q -from django.views.generic import list_detail -import settings from pyi2phosts.postkey.models import i2phost +from pyi2phosts.lib.generic import HostsListsView -def search(request): - q = request.GET.get('q', '') - fil = Q(name__icontains=q) | Q(b64hash__contains=q) - qs = i2phost.objects.filter(fil) - return list_detail.object_list( - request = request, - queryset = qs, - template_name = 'search_results.html', - template_object_name = 'host', - paginate_by = 40, - extra_context = { - 'title': settings.SITE_NAME, - } - ) +class SearchedHostsListsView(HostsListsView): + """ Renders list of hosts matching search request """ + + def get_queryset(self): + q = self.request.GET.get('q', '') + fil = Q(name__icontains=q) | Q(b64hash__contains=q) + queryset = i2phost.objects.filter(fil) + return queryset + + template_name = 'search_results.html' + template_object_name = 'host_list' + paginate_by = 40 diff --git a/pyi2phosts/urls.py b/pyi2phosts/urls.py index 155a13a..39045f6 100644 --- a/pyi2phosts/urls.py +++ b/pyi2phosts/urls.py @@ -1,40 +1,20 @@ from django.conf.urls.defaults import * -from django.views.generic.list_detail import object_list # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() from pyi2phosts.lib.rss import AliveHostsFeed -from pyi2phosts.lib.utils import get_b32 from pyi2phosts.lib.generic import LocalTemplateView -from pyi2phosts.extsources.models import ExternalSource -from pyi2phosts.postkey.models import i2phost +from pyi2phosts.lib.generic import FaqView +from pyi2phosts.lib.generic import HostsListsView import settings -extsources = { - 'queryset': ExternalSource.objects.filter(active=True), - 'template_name': 'faq.html', - 'template_object_name': 'sources', - 'extra_context': { - 'title': settings.SITE_NAME, - } - } - -browse_hosts = { - 'queryset': i2phost.objects.filter(activated=True).order_by("-last_seen"), - 'template_name': 'browse.html', - 'template_object_name': 'host', - 'paginate_by': 40, - 'extra_context': { - 'title': settings.SITE_NAME, - } - } urlpatterns = patterns('', url(r'^$', LocalTemplateView.as_view(template_name='index.html'), name='index'), - url(r'^faq/$', object_list, extsources, name='faq'), - url(r'^browse/$', object_list, browse_hosts, name='browse'), + url(r'^faq/$', FaqView.as_view(), name='faq'), + url(r'^browse/$', HostsListsView.as_view(), name='browse'), url(r'^browse/rss/$', AliveHostsFeed(), name='browse-rss'), (r'^latest/', include('pyi2phosts.latest.urls')),