summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-07-25 04:01:31 +0200
committerDan McGee <dan@archlinux.org>2012-07-25 04:01:31 +0200
commit24b28a504cabcf077882aa95cfa0edbc6a8d4569 (patch)
treebff618fd8c245340e368971372e672fe65c78df1
parentd68e00f19556be169f303da256c79431e15dd448 (diff)
downloadarchweb-24b28a504cabcf077882aa95cfa0edbc6a8d4569.tar.gz
archweb-24b28a504cabcf077882aa95cfa0edbc6a8d4569.tar.xz
Replace deprecated list_detail usage in search with class-based view
We can convert the entire search view to a generic class-based ListView. This is still one of the more disgusting views in the application and has a ton of logic scattered buckshot across several methods, but this commit is not meant to address all of that in one go. This is the last of the deprecated pieces I know of we are still using in the codebase, so we should be relatively safe in the long run now for an upgrade to the eventual next major Django release. Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--packages/urls.py8
-rw-r--r--packages/views/__init__.py2
-rw-r--r--packages/views/search.py77
3 files changed, 45 insertions, 42 deletions
diff --git a/packages/urls.py b/packages/urls.py
index 6eddc5f..9a151b4 100644
--- a/packages/urls.py
+++ b/packages/urls.py
@@ -1,5 +1,7 @@
from django.conf.urls import include, patterns
+from .views.search import SearchListView
+
package_patterns = patterns('packages.views',
(r'^$', 'details'),
(r'^json/$', 'details_json'),
@@ -21,9 +23,9 @@ urlpatterns = patterns('packages.views',
(r'^signoffs/json/$', 'signoffs_json', {}, 'package-signoffs-json'),
(r'^update/$', 'update'),
- (r'^$', 'search', {}, 'packages-search'),
- (r'^search/json/$', 'search_json'),
- (r'^(?P<page>\d+)/$', 'search'),
+ (r'^$', SearchListView.as_view(), {}, 'packages-search'),
+ (r'^(?P<page>\d+)/$', SearchListView.as_view()),
+ (r'^search/json/$', 'search_json'),
(r'^differences/$', 'arch_differences', {}, 'packages-differences'),
(r'^stale_relations/$', 'stale_relations'),
diff --git a/packages/views/__init__.py b/packages/views/__init__.py
index 19ea910..038d40a 100644
--- a/packages/views/__init__.py
+++ b/packages/views/__init__.py
@@ -19,7 +19,7 @@ from ..utils import (get_differences_info,
from .display import (details, groups, group_details, files, details_json,
files_json, download)
from .flag import flaghelp, flag, flag_confirmed, unflag, unflag_all
-from .search import search, search_json
+from .search import search_json
from .signoff import signoffs, signoff_package, signoff_options, signoffs_json
diff --git a/packages/views/search.py b/packages/views/search.py
index a89822b..9750894 100644
--- a/packages/views/search.py
+++ b/packages/views/search.py
@@ -6,7 +6,7 @@ from django.contrib.admin.widgets import AdminDateWidget
from django.contrib.auth.models import User
from django.db.models import Q
from django.http import HttpResponse
-from django.views.generic import list_detail
+from django.views.generic import ListView
from main.models import Package, Arch, Repo
from main.utils import make_choice
@@ -33,6 +33,7 @@ class LimitTypedChoiceField(forms.TypedChoiceField):
except (ValueError, TypeError):
return False
+
class PackageSearchForm(forms.Form):
repo = forms.MultipleChoiceField(required=False)
arch = forms.MultipleChoiceField(required=False)
@@ -69,6 +70,7 @@ class PackageSearchForm(forms.Form):
[('', 'All'), ('unknown', 'Unknown')] + \
[(m.username, m.get_full_name()) for m in maints]
+
def parse_form(form, packages):
if form.cleaned_data['repo']:
packages = packages.filter(
@@ -117,48 +119,47 @@ def parse_form(form, packages):
return packages
-def search(request, page=None):
- limit = 50
- sort = None
- packages = Package.objects.normal()
- if request.GET:
- form = PackageSearchForm(data=request.GET)
- if form.is_valid():
- packages = parse_form(form, packages)
- asked_limit = form.cleaned_data['limit']
+class SearchListView(ListView):
+ template_name = "packages/search.html"
+
+ sort_fields = ("arch", "repo", "pkgname", "pkgbase", "compressed_size",
+ "installed_size", "build_date", "last_update", "flag_date")
+ allowed_sort = list(sort_fields) + ["-" + s for s in sort_fields]
+
+ def get(self, request, *args, **kwargs):
+ self.form = PackageSearchForm(data=request.GET)
+ return super(SearchListView, self).get(request, *args, **kwargs)
+
+ def get_queryset(self):
+ packages = Package.objects.normal()
+ if self.form.is_valid():
+ packages = parse_form(self.form, packages)
+ sort = self.form.cleaned_data['sort']
+ if sort in self.allowed_sort:
+ packages = packages.order_by(sort)
+ else:
+ packages = packages.order_by('pkgname')
+ return packages
+
+ # Form had errors so don't return any results
+ return Package.objects.none()
+
+ def get_paginate_by(self, queryset):
+ limit = 50
+ if self.form.is_valid():
+ asked_limit = self.form.cleaned_data['limit']
if asked_limit and asked_limit < 0:
limit = None
elif asked_limit:
limit = asked_limit
- sort = form.cleaned_data['sort']
- else:
- # Form had errors, don't return any results, just the busted form
- packages = Package.objects.none()
- else:
- form = PackageSearchForm()
-
- current_query = request.GET.urlencode()
- page_dict = {
- 'search_form': form,
- 'current_query': current_query
- }
- allowed_sort = ["arch", "repo", "pkgname", "pkgbase",
- "compressed_size", "installed_size",
- "build_date", "last_update", "flag_date"]
- allowed_sort += ["-" + s for s in allowed_sort]
- if sort in allowed_sort:
- packages = packages.order_by(sort)
- page_dict['sort'] = sort
- else:
- packages = packages.order_by('pkgname')
-
- return list_detail.object_list(request, packages,
- template_name="packages/search.html",
- page=page,
- paginate_by=limit,
- template_object_name="package",
- extra_context=page_dict)
+ return limit
+
+ def get_context_data(self, **kwargs):
+ context = super(SearchListView, self).get_context_data(**kwargs)
+ context['current_query'] = self.request.GET.urlencode()
+ context['search_form'] = self.form
+ return context
def search_json(request):