From: Stephen Finucane Date: Sun, 6 Dec 2015 18:13:25 +0000 (+0000) Subject: views: Split up 'base' X-Git-Tag: v1.1.0~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=026cf7e3108c9c3c3f877b007289832541334ef0;p=thirdparty%2Fpatchwork.git views: Split up 'base' This is a collection of unrelated functions, many of which belong in their own file. Do this, taking the opportunity to rename some of these functions to better indicate their purpose in the process. Signed-off-by: Stephen Finucane --- diff --git a/patchwork/templates/patchwork/filters.html b/patchwork/templates/patchwork/filters.html index 65fd6bfe..acb9375e 100644 --- a/patchwork/templates/patchwork/filters.html +++ b/patchwork/templates/patchwork/filters.html @@ -70,7 +70,7 @@ $(document).ready(function() { return callback(); req = $.ajax({ - url: '{% url 'patchwork.views.submitter_complete' %}?q=' + + url: '{% url 'patchwork.views.api.submitters' %}?q=' + encodeURIComponent(query) + '&l=10', error: function() { callback(); diff --git a/patchwork/templates/patchwork/help/pwclient.html b/patchwork/templates/patchwork/help/pwclient.html index 7101ec16..793cf737 100644 --- a/patchwork/templates/patchwork/help/pwclient.html +++ b/patchwork/templates/patchwork/help/pwclient.html @@ -11,13 +11,13 @@ and applying patches.

To use pwclient, you will need:

You can create your own .pwclientrc file. Each -patchwork project +patchwork project provides a sample linked from the 'project info' page.

{% endblock %} diff --git a/patchwork/templates/patchwork/project.html b/patchwork/templates/patchwork/project.html index be8cadc1..e1cf3f5f 100644 --- a/patchwork/templates/patchwork/project.html +++ b/patchwork/templates/patchwork/project.html @@ -49,9 +49,9 @@ {% if settings.ENABLE_XMLRPC %} -

Sample patchwork +

Sample patchwork client configuration for this project: .pwclientrc.

{% endif %} diff --git a/patchwork/templatetags/person.py b/patchwork/templatetags/person.py index 6829019f..d35203b5 100644 --- a/patchwork/templatetags/person.py +++ b/patchwork/templatetags/person.py @@ -38,9 +38,9 @@ def personify(person, project): else: linktext = escape(person.email) - url = reverse( - 'patchwork.views.patch.list', kwargs={'project_id': project.linkname}) - str = '%s' % \ - (url, SubmitterFilter.param, escape(person.id), linktext) + url = reverse('patchwork.views.patch.list', + kwargs={'project_id': project.linkname}) + str = '%s' % ( + url, SubmitterFilter.param, escape(person.id), linktext) return mark_safe(str) diff --git a/patchwork/tests/test_confirm.py b/patchwork/tests/test_confirm.py index 4c14e569..7ef922e0 100644 --- a/patchwork/tests/test_confirm.py +++ b/patchwork/tests/test_confirm.py @@ -25,7 +25,8 @@ from patchwork.models import EmailConfirmation, Person def _confirmation_url(conf): - return reverse('patchwork.views.confirm', kwargs={'key': conf.key}) + return reverse('patchwork.views.confirm', + kwargs={'key': conf.key}) class TestUser(object): diff --git a/patchwork/tests/test_mail_settings.py b/patchwork/tests/test_mail_settings.py index b9f1dd91..02205aa4 100644 --- a/patchwork/tests/test_mail_settings.py +++ b/patchwork/tests/test_mail_settings.py @@ -188,7 +188,8 @@ class OptinRequestTest(TestCase): self.assertContains(response, self.email) # check email - url = reverse('patchwork.views.confirm', kwargs={'key': conf.key}) + url = reverse('patchwork.views.confirm', + kwargs={'key': conf.key}) self.assertEqual(len(mail.outbox), 1) msg = mail.outbox[0] self.assertEqual(msg.to, [self.email]) diff --git a/patchwork/tests/test_registration.py b/patchwork/tests/test_registration.py index b5eacef5..b5f5bb44 100644 --- a/patchwork/tests/test_registration.py +++ b/patchwork/tests/test_registration.py @@ -28,7 +28,8 @@ from patchwork.tests.utils import create_user def _confirmation_url(conf): - return reverse('patchwork.views.confirm', kwargs={'key': conf.key}) + return reverse('patchwork.views.confirm', + kwargs={'key': conf.key}) class TestUser(object): diff --git a/patchwork/tests/test_user.py b/patchwork/tests/test_user.py index e9d18227..3df5ffa5 100644 --- a/patchwork/tests/test_user.py +++ b/patchwork/tests/test_user.py @@ -28,7 +28,8 @@ from patchwork.tests.utils import defaults, error_strings def _confirmation_url(conf): - return reverse('patchwork.views.confirm', kwargs={'key': conf.key}) + return reverse('patchwork.views.confirm', + kwargs={'key': conf.key}) class TestUser(object): diff --git a/patchwork/tests/test_xmlrpc.py b/patchwork/tests/test_xmlrpc.py index d8c77f06..d8e1d98c 100644 --- a/patchwork/tests/test_xmlrpc.py +++ b/patchwork/tests/test_xmlrpc.py @@ -42,7 +42,7 @@ class XMLRPCTest(LiveServerTestCase): def testGetRedirect(self): response = self.client.patch(self.url) self.assertRedirects(response, - reverse('patchwork.views.help', + reverse('patchwork.views.help.help', kwargs={'path': 'pwclient/'})) def testList(self): diff --git a/patchwork/urls.py b/patchwork/urls.py index 81a571a1..5be286a8 100644 --- a/patchwork/urls.py +++ b/patchwork/urls.py @@ -29,7 +29,7 @@ urlpatterns = patterns( '', url(r'^admin/', include(admin.site.urls)), - (r'^$', 'patchwork.views.projects'), + (r'^$', 'patchwork.views.project.list'), (r'^project/(?P[^/]+)/list/$', 'patchwork.views.patch.list'), (r'^project/(?P[^/]+)/$', 'patchwork.views.project.project'), @@ -85,7 +85,7 @@ urlpatterns = patterns( (r'^confirm/(?P[0-9a-f]+)/$', 'patchwork.views.confirm'), # submitter autocomplete - (r'^submitter/$', 'patchwork.views.submitter_complete'), + (r'^submitter/$', 'patchwork.views.api.submitters'), # email setup (r'^mail/$', 'patchwork.views.mail.settings'), @@ -93,16 +93,16 @@ urlpatterns = patterns( (r'^mail/optin/$', 'patchwork.views.mail.optin'), # help! - (r'^help/(?P.*)$', 'patchwork.views.help'), + (r'^help/(?P.*)$', 'patchwork.views.help.help'), ) if settings.ENABLE_XMLRPC: urlpatterns += patterns( '', (r'xmlrpc/$', 'patchwork.views.xmlrpc.xmlrpc'), - (r'^pwclient/$', 'patchwork.views.pwclient'), + (r'^pwclient/$', 'patchwork.views.pwclient.pwclient'), (r'^project/(?P[^/]+)/pwclientrc/$', - 'patchwork.views.pwclientrc'), + 'patchwork.views.pwclient.pwclientrc'), ) # redirect from old urls diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py index 0a12b41c..4cfbc2cf 100644 --- a/patchwork/views/__init__.py +++ b/patchwork/views/__init__.py @@ -27,11 +27,14 @@ from email.parser import HeaderParser import email.utils import re -from .base import * # noqa -from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle -from patchwork.paginator import Paginator +from django.http import Http404 +from django.shortcuts import render_to_response, get_object_or_404 + from patchwork.forms import MultiplePatchForm -from patchwork.models import Comment, Patch +from patchwork.models import Comment, Patch, EmailConfirmation +from patchwork.paginator import Paginator +from patchwork.requestcontext import PatchworkRequestContext +from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle def generic_list(request, project, view, @@ -222,3 +225,31 @@ def patch_to_mbox(patch): mail['Date'] = email.utils.formatdate(utc_timestamp) return mail + + +def confirm(request, key): + import patchwork.views.user + import patchwork.views.mail + + views = { + 'userperson': patchwork.views.user.link_confirm, + 'registration': patchwork.views.user.register_confirm, + 'optout': patchwork.views.mail.optout_confirm, + 'optin': patchwork.views.mail.optin_confirm, + } + + conf = get_object_or_404(EmailConfirmation, key=key) + if conf.type not in views: + raise Http404 + + if conf.active and conf.is_valid(): + return views[conf.type](request, conf) + + context = PatchworkRequestContext(request) + context['conf'] = conf + if not conf.active: + context['error'] = 'inactive' + elif not conf.is_valid(): + context['error'] = 'expired' + + return render_to_response('patchwork/confirm-error.html', context) diff --git a/patchwork/views/api.py b/patchwork/views/api.py new file mode 100644 index 00000000..91f4f3d0 --- /dev/null +++ b/patchwork/views/api.py @@ -0,0 +1,54 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import json + +from django.db.models import Q +from django.http import HttpResponse + +from patchwork.models import Person + + +def submitters(request): + search = request.GET.get('q', '') + limit = request.GET.get('l', None) + + if len(search) <= 3: + return HttpResponse(content_type="application/json") + + queryset = Person.objects.filter(Q(name__icontains=search) | + Q(email__icontains=search)) + if limit is not None: + try: + limit = int(limit) + except ValueError: + limit = None + + if limit is not None and limit > 0: + queryset = queryset[:limit] + + data = [] + for submitter in queryset: + item = {} + item['pk'] = submitter.id + item['name'] = submitter.name + item['email'] = submitter.email + data.append(item) + + return HttpResponse(json.dumps(data), content_type="application/json") diff --git a/patchwork/views/base.py b/patchwork/views/base.py deleted file mode 100644 index cd9c43f0..00000000 --- a/patchwork/views/base.py +++ /dev/null @@ -1,138 +0,0 @@ -# Patchwork - automated patch tracking system -# Copyright (C) 2008 Jeremy Kerr -# -# This file is part of the Patchwork package. -# -# Patchwork is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# Patchwork is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Patchwork; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -from __future__ import absolute_import - -import json - -from django.conf import settings -from django.core import urlresolvers -from django.db.models import Q -from django.http import HttpResponse, HttpResponseRedirect, Http404 -from django.shortcuts import render_to_response, get_object_or_404 -from django.template.loader import render_to_string - -from patchwork.models import Project, Person, EmailConfirmation -from patchwork.requestcontext import PatchworkRequestContext - - -def projects(request): - context = PatchworkRequestContext(request) - projects = Project.objects.all() - - if projects.count() == 1: - return HttpResponseRedirect( - urlresolvers.reverse('patchwork.views.patch.list', - kwargs={'project_id': projects[0].linkname})) - - context['projects'] = projects - return render_to_response('patchwork/projects.html', context) - - -def pwclientrc(request, project_id): - project = get_object_or_404(Project, linkname=project_id) - context = PatchworkRequestContext(request) - context.project = project - if settings.FORCE_HTTPS_LINKS or request.is_secure(): - context['scheme'] = 'https' - else: - context['scheme'] = 'http' - response = HttpResponse(content_type="text/plain") - response['Content-Disposition'] = 'attachment; filename=.pwclientrc' - response.write(render_to_string('patchwork/pwclientrc', context)) - return response - - -def pwclient(request): - context = PatchworkRequestContext(request) - response = HttpResponse(content_type="text/x-python") - response['Content-Disposition'] = 'attachment; filename=pwclient' - response.write(render_to_string('patchwork/pwclient', context)) - return response - - -def confirm(request, key): - import patchwork.views.user - import patchwork.views.mail - views = { - 'userperson': patchwork.views.user.link_confirm, - 'registration': patchwork.views.user.register_confirm, - 'optout': patchwork.views.mail.optout_confirm, - 'optin': patchwork.views.mail.optin_confirm, - } - - conf = get_object_or_404(EmailConfirmation, key=key) - if conf.type not in views: - raise Http404 - - if conf.active and conf.is_valid(): - return views[conf.type](request, conf) - - context = PatchworkRequestContext(request) - context['conf'] = conf - if not conf.active: - context['error'] = 'inactive' - elif not conf.is_valid(): - context['error'] = 'expired' - - return render_to_response('patchwork/confirm-error.html', context) - - -def submitter_complete(request): - search = request.GET.get('q', '') - limit = request.GET.get('l', None) - - if len(search) <= 3: - return HttpResponse(content_type="application/json") - - queryset = Person.objects.filter(Q(name__icontains=search) | - Q(email__icontains=search)) - if limit is not None: - try: - limit = int(limit) - except ValueError: - limit = None - - if limit is not None and limit > 0: - queryset = queryset[:limit] - - data = [] - for submitter in queryset: - item = {} - item['pk'] = submitter.id - item['name'] = submitter.name - item['email'] = submitter.email - data.append(item) - - return HttpResponse(json.dumps(data), content_type="application/json") - -help_pages = {'': 'index.html', - 'about/': 'about.html', - } - -if settings.ENABLE_XMLRPC: - help_pages['pwclient/'] = 'pwclient.html' - - -def help(request, path): - context = PatchworkRequestContext(request) - if path in help_pages: - return render_to_response( - 'patchwork/help/' + help_pages[path], context) - raise Http404 diff --git a/patchwork/views/help.py b/patchwork/views/help.py new file mode 100644 index 00000000..cf14fe25 --- /dev/null +++ b/patchwork/views/help.py @@ -0,0 +1,43 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from __future__ import absolute_import + +from django.conf import settings +from django.http import Http404 +from django.shortcuts import render_to_response + +from patchwork.requestcontext import PatchworkRequestContext + + +help_pages = { + '': 'index.html', + 'about/': 'about.html', +} + +if settings.ENABLE_XMLRPC: + help_pages['pwclient/'] = 'pwclient.html' + + +def help(request, path): + context = PatchworkRequestContext(request) + if path in help_pages: + return render_to_response( + 'patchwork/help/' + help_pages[path], context) + raise Http404 diff --git a/patchwork/views/project.py b/patchwork/views/project.py index 82103d47..e3f37471 100644 --- a/patchwork/views/project.py +++ b/patchwork/views/project.py @@ -20,12 +20,28 @@ from __future__ import absolute_import from django.contrib.auth.models import User +from django.core import urlresolvers +from django.http import HttpResponseRedirect from django.shortcuts import render_to_response, get_object_or_404 from patchwork.models import Patch, Project from patchwork.requestcontext import PatchworkRequestContext +def list(request): + context = PatchworkRequestContext(request) + projects = Project.objects.all() + + if projects.count() == 1: + return HttpResponseRedirect( + urlresolvers.reverse('patchwork.views.patch.list', + kwargs={'project_id': projects[0].linkname})) + + context['projects'] = projects + + return render_to_response('patchwork/projects.html', context) + + def project(request, project_id): context = PatchworkRequestContext(request) project = get_object_or_404(Project, linkname=project_id) diff --git a/patchwork/views/pwclient.py b/patchwork/views/pwclient.py new file mode 100644 index 00000000..b3e0052f --- /dev/null +++ b/patchwork/views/pwclient.py @@ -0,0 +1,50 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from __future__ import absolute_import + +from django.conf import settings +from django.http import HttpResponse +from django.shortcuts import get_object_or_404 +from django.template.loader import render_to_string + +from patchwork.models import Project +from patchwork.requestcontext import PatchworkRequestContext + + +def pwclientrc(request, project_id): + project = get_object_or_404(Project, linkname=project_id) + context = PatchworkRequestContext(request) + context.project = project + if settings.FORCE_HTTPS_LINKS or request.is_secure(): + context['scheme'] = 'https' + else: + context['scheme'] = 'http' + response = HttpResponse(content_type="text/plain") + response['Content-Disposition'] = 'attachment; filename=.pwclientrc' + response.write(render_to_string('patchwork/pwclientrc', context)) + return response + + +def pwclient(request): + context = PatchworkRequestContext(request) + response = HttpResponse(content_type="text/x-python") + response['Content-Disposition'] = 'attachment; filename=pwclient' + response.write(render_to_string('patchwork/pwclient', context)) + return response diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py index b9514a40..d928400c 100644 --- a/patchwork/views/xmlrpc.py +++ b/patchwork/views/xmlrpc.py @@ -30,7 +30,7 @@ except ImportError: from xmlrpc.server import XMLRPCDocGenerator import sys -from django.core import urlresolvers +from django.core.urlresolvers import reverse from django.contrib.auth import authenticate from django.http import ( HttpResponse, HttpResponseRedirect, HttpResponseServerError) @@ -136,8 +136,8 @@ dispatcher = PatchworkXMLRPCDispatcher() @csrf_exempt def xmlrpc(request): if request.method not in ['POST', 'GET']: - return HttpResponseRedirect(urlresolvers.reverse( - 'patchwork.views.help', kwargs={'path': 'pwclient/'})) + return HttpResponseRedirect(reverse('patchwork.views.help.help', + kwargs={'path': 'pwclient/'})) response = HttpResponse() diff --git a/templates/base.html b/templates/base.html index 2e0b487f..b6f7056e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -34,7 +34,7 @@ Patchwork + href="{% url 'patchwork.views.project.list' %}">Patchwork {% block heading %}{% endblock %}