From 2c1afc9b3886b58dde779d64f4bf26c7f25e8b36 Mon Sep 17 00:00:00 2001 From: Nicolas Joyard <joyard.nicolas@gmail.com> Date: Thu, 16 Jun 2016 08:59:47 +0200 Subject: [PATCH] Redirect group URLs to filtered representative list --- memopol/urls.py | 6 +++--- memopol/views/redirects.py | 21 +++++++++++++++++++++ memopol/views/representative_list.py | 6 +----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/memopol/urls.py b/memopol/urls.py index 2cf2f678..848cb768 100644 --- a/memopol/urls.py +++ b/memopol/urls.py @@ -10,7 +10,7 @@ from views.group_ac import GroupAutocomplete from views.group_list import GroupList from views.representative_detail import RepresentativeDetail from views.representative_list import RepresentativeList -from views.redirects import RedirectGroupList +from views.redirects import RedirectGroupList, RedirectGroupRepresentativeList import api @@ -21,12 +21,12 @@ urlpatterns = [ url( r'^legislature/representative/(?P<group_kind>\w+)/(?P<chamber>.+)/' + r'(?P<group>.+)/$', - RepresentativeList.as_view(), + RedirectGroupRepresentativeList.as_view(), name='representative-list' ), url( r'^legislature/representative/(?P<group_kind>\w+)/(?P<group>.+)/$', - RepresentativeList.as_view(), + RedirectGroupRepresentativeList.as_view(), name='representative-list' ), url( diff --git a/memopol/views/redirects.py b/memopol/views/redirects.py index 876c7eab..fa65d39d 100644 --- a/memopol/views/redirects.py +++ b/memopol/views/redirects.py @@ -1,9 +1,30 @@ # coding: utf-8 +from django.core.urlresolvers import reverse from django.views.generic.base import RedirectView +from representatives.models import Group + class RedirectGroupList(RedirectView): permanent = True query_string = True pattern_name = 'group-list' + + +class RedirectGroupRepresentativeList(RedirectView): + permanent = True + + def get_redirect_url(self, *args, **kwargs): + if kwargs['group_kind'] == 'chamber': + chamber = Group.objects.get(kind='chamber', name=kwargs['group']) + return '%s?chamber=%s' % (reverse('representative-list'), + chamber.pk) + elif kwargs['group_kind'] == 'country': + country = Group.objects.get(kind='country', name=kwargs['group']) + return '%s?country=%s' % (reverse('representative-list'), + country.pk) + else: + group = Group.objects.get(kind=kwargs['group_kind'], + name=kwargs['group']) + return '%s?group=%s' % (reverse('representative-list'), group.pk) diff --git a/memopol/views/representative_list.py b/memopol/views/representative_list.py index 9f700efa..b5efe723 100644 --- a/memopol/views/representative_list.py +++ b/memopol/views/representative_list.py @@ -3,13 +3,9 @@ from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin, \ ActiveLegislatureMixin -import datetime - -from django.db import models -from django.utils.text import slugify from django.views import generic -from representatives.models import Group, Representative +from representatives.models import Representative from ..filters import RepresentativeFilter from .representative_mixin import RepresentativeViewMixin -- GitLab