Browse Source

Migrate all function-based generic views to a class-based views

pull/1/head
Hidden Z 13 years ago
parent
commit
76ee7dbd0e
  1. 5
      pyi2phosts/latest/urls.py
  2. 32
      pyi2phosts/latest/views.py
  3. 37
      pyi2phosts/lib/generic.py
  4. 5
      pyi2phosts/search/urls.py
  5. 29
      pyi2phosts/search/views.py
  6. 28
      pyi2phosts/urls.py

5
pyi2phosts/latest/urls.py

@ -1,8 +1,9 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from pyi2phosts.lib.rss import LatestHostsFeed from pyi2phosts.lib.rss import LatestHostsFeed
from pyi2phosts.latest.views import LatestHostsListsView
urlpatterns = patterns('pyi2phosts.latest.views', urlpatterns = patterns('',
url(r'^$', 'latest', name='latest'), url(r'^$', LatestHostsListsView.as_view(), name='latest'),
url(r'^rss/$', LatestHostsFeed(), name='latest-rss'), url(r'^rss/$', LatestHostsFeed(), name='latest-rss'),
) )

32
pyi2phosts/latest/views.py

@ -1,9 +1,8 @@
import datetime import datetime
from django.views.generic import list_detail
import settings import settings
from pyi2phosts.postkey.models import i2phost from pyi2phosts.postkey.models import i2phost
from pyi2phosts.lib.generic import HostsListsView
def get_latest(): def get_latest():
now_date = datetime.datetime.utcnow() 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] date_added__range=(start_date, now_date)).order_by("-date_added")[:settings.LATEST_HOSTS_COUNT]
return qs return qs
def latest(request): class LatestHostsListsView(HostsListsView):
return list_detail.object_list( """ Renders list of latest active hosts added """
request = request,
queryset = get_latest(), def get_context_data(self, **kwargs):
template_name = 'latest.html', context = super(LatestHostsListsView, self).get_context_data(**kwargs)
template_object_name = 'host', context.update({
paginate_by = 40, 'title': settings.SITE_NAME,
extra_context = { 'day_count': settings.LATEST_DAY_COUNT,
'title': settings.SITE_NAME, 'hosts_count': settings.LATEST_HOSTS_COUNT
'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

37
pyi2phosts/lib/generic.py

@ -1,8 +1,17 @@
from django.views.generic.base import TemplateView 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.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 import settings
class LocalTemplateView(TemplateView): class LocalTemplateView(TemplateView):
""" Renders some template with passing some local config variables """
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(LocalTemplateView, self).get_context_data(**kwargs) context = super(LocalTemplateView, self).get_context_data(**kwargs)
context.update({ context.update({
@ -12,3 +21,31 @@ class LocalTemplateView(TemplateView):
'b32': get_b32(settings.MY_B64) 'b32': get_b32(settings.MY_B64)
}) })
return context 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

5
pyi2phosts/search/urls.py

@ -1,5 +1,6 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from pyi2phosts.search.views import SearchedHostsListsView
urlpatterns = patterns('pyi2phosts.search.views', urlpatterns = patterns('',
(r'^$', 'search'), (r'^$', SearchedHostsListsView.as_view()),
) )

29
pyi2phosts/search/views.py

@ -1,21 +1,18 @@
from django.db.models import Q from django.db.models import Q
from django.views.generic import list_detail
import settings
from pyi2phosts.postkey.models import i2phost from pyi2phosts.postkey.models import i2phost
from pyi2phosts.lib.generic import HostsListsView
def search(request): class SearchedHostsListsView(HostsListsView):
q = request.GET.get('q', '') """ Renders list of hosts matching search request """
fil = Q(name__icontains=q) | Q(b64hash__contains=q)
qs = i2phost.objects.filter(fil) def get_queryset(self):
return list_detail.object_list( q = self.request.GET.get('q', '')
request = request, fil = Q(name__icontains=q) | Q(b64hash__contains=q)
queryset = qs, queryset = i2phost.objects.filter(fil)
template_name = 'search_results.html', return queryset
template_object_name = 'host',
paginate_by = 40, template_name = 'search_results.html'
extra_context = { template_object_name = 'host_list'
'title': settings.SITE_NAME, paginate_by = 40
}
)

28
pyi2phosts/urls.py

@ -1,40 +1,20 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from django.views.generic.list_detail import object_list
# Uncomment the next two lines to enable the admin: # Uncomment the next two lines to enable the admin:
from django.contrib import admin from django.contrib import admin
admin.autodiscover() admin.autodiscover()
from pyi2phosts.lib.rss import AliveHostsFeed from pyi2phosts.lib.rss import AliveHostsFeed
from pyi2phosts.lib.utils import get_b32
from pyi2phosts.lib.generic import LocalTemplateView from pyi2phosts.lib.generic import LocalTemplateView
from pyi2phosts.extsources.models import ExternalSource from pyi2phosts.lib.generic import FaqView
from pyi2phosts.postkey.models import i2phost from pyi2phosts.lib.generic import HostsListsView
import settings 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('', urlpatterns = patterns('',
url(r'^$', LocalTemplateView.as_view(template_name='index.html'), name='index'), url(r'^$', LocalTemplateView.as_view(template_name='index.html'), name='index'),
url(r'^faq/$', object_list, extsources, name='faq'), url(r'^faq/$', FaqView.as_view(), name='faq'),
url(r'^browse/$', object_list, browse_hosts, name='browse'), url(r'^browse/$', HostsListsView.as_view(), name='browse'),
url(r'^browse/rss/$', AliveHostsFeed(), name='browse-rss'), url(r'^browse/rss/$', AliveHostsFeed(), name='browse-rss'),
(r'^latest/', include('pyi2phosts.latest.urls')), (r'^latest/', include('pyi2phosts.latest.urls')),

Loading…
Cancel
Save