From 82b17968755c5ff8077e2eb4be2cf68de04f886a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Sat, 13 Apr 2013 18:13:11 +0200 Subject: [PATCH] Fixed issue #585. Sort group members by first name or last name in group detail view according to config variable --- .../templates/participant/group_detail.html | 2 +- openslides/participant/views.py | 10 ++++ tests/participant/test_views.py | 54 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/participant/test_views.py diff --git a/openslides/participant/templates/participant/group_detail.html b/openslides/participant/templates/participant/group_detail.html index ea3f5fab0..99e46ad8c 100644 --- a/openslides/participant/templates/participant/group_detail.html +++ b/openslides/participant/templates/participant/group_detail.html @@ -18,7 +18,7 @@

{% trans "Members" %}

    -{% for member in group.user_set.all %} +{% for member in group_members %}
  1. {{ member }}
  2. {% empty %}

    {% trans "No members available." %}

    diff --git a/openslides/participant/views.py b/openslides/participant/views.py index 3e5a4551f..cff7d8922 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -343,6 +343,16 @@ class GroupDetailView(DetailView, PermissionMixin): template_name = 'participant/group_detail.html' context_object_name = 'group' + def get_context_data(self, *args, **kwargs): + context = super(GroupDetailView, self).get_context_data(*args, **kwargs) + query = User.objects + if config['participant_sort_users_by_first_name']: + query = query.order_by('first_name') + else: + query = query.order_by('last_name') + context['group_members'] = query.filter(django_user__groups__in=[context['group']]) + return context + class GroupCreateView(CreateView): """ diff --git a/tests/participant/test_views.py b/tests/participant/test_views.py new file mode 100644 index 000000000..3ea840e33 --- /dev/null +++ b/tests/participant/test_views.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + Tests for views of openslides.participant + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: 2011–2013 by OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. +""" + +import re +from django.test.client import Client + +from openslides.config.api import config +from openslides.utils.test import TestCase +from openslides.participant.models import User, Group + + +class GroupViews(TestCase): + """ + Tests the detail view for groups and later also the other views. + """ + def setUp(self): + self.user_1 = User.objects.create(last_name='chahshah7eiqueip5eiW', + first_name='mi6iu2Te6ei9iohue3ex', + username='mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW', + is_superuser=True) + self.user_2 = User.objects.create(last_name='uquahx3Wohtieph9baer', + first_name='aWei4ien6Se0vie0xeiv', + username='aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer') + self.delegate = Group.objects.get(pk=3) + self.user_1.groups.add(self.delegate) + self.user_2.groups.add(self.delegate) + + self.client = Client() + login_user = User.objects.create(username='loginusername', is_superuser=True) + login_user.reset_password('default') + self.client.login(username='loginusername', password='default') + + def test_detail(self): + self.assertFalse(config['participant_sort_users_by_first_name']) + response = self.client.get('/participant/group/3/') + pattern = r'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW|aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer' + match = re.findall(pattern, response.content) + self.assertEqual(match[0], 'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW') + self.assertEqual(match[1], 'aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer') + + config['participant_sort_users_by_first_name'] = True + self.assertTrue(config['participant_sort_users_by_first_name']) + response = self.client.get('/participant/group/3/') + pattern = r'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW|aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer' + match = re.findall(pattern, response.content) + self.assertEqual(match[1], 'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW') + self.assertEqual(match[0], 'aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer')