From ecf561e305a6b648679536897ae498514c3ea36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Sat, 9 Jan 2016 01:10:37 +0100 Subject: [PATCH] Added login info text in config and view. --- openslides/core/signals.py | 8 +++++++ openslides/users/views.py | 33 +++++++++++++++++++++++++-- tests/integration/users/test_views.py | 4 +++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/openslides/core/signals.py b/openslides/core/signals.py index 13afbd393..e1dd700a6 100644 --- a/openslides/core/signals.py +++ b/openslides/core/signals.py @@ -75,6 +75,14 @@ def setup_general_config(sender, **kwargs): group=ugettext_lazy('General'), subgroup=ugettext_lazy('System')) + yield ConfigVariable( + name='general_login_info_text', + default_value='', + label=ugettext_lazy('Show this text on the login page.'), + weight=140, + group=ugettext_lazy('General'), + subgroup=ugettext_lazy('System')) + # Projector yield ConfigVariable( diff --git a/openslides/users/views.py b/openslides/users/views.py index 938194609..104afef1d 100644 --- a/openslides/users/views.py +++ b/openslides/users/views.py @@ -191,7 +191,7 @@ class UserLoginView(APIView): """ Login the user. """ - http_method_names = ['post'] + http_method_names = ['get', 'post'] def post(self, *args, **kwargs): form = AuthenticationForm(self.request, data=self.request.data) @@ -202,7 +202,36 @@ class UserLoginView(APIView): return super().post(*args, **kwargs) def get_context_data(self, **context): - context['user_id'] = self.user.pk + """ + Adds some context. + + For GET requests adds login info text to context. This info text is + taken from the config. If this value is empty, a special text is used + if the admin user has the password 'admin'. + + For POST requests adds the id of the current user to the context. + """ + if self.request.method == 'GET': + if config['general_login_info_text']: + context['info_text'] = config['general_login_info_text'] + else: + try: + user = User.objects.get(username='admin') + except User.DoesNotExist: + context['info_text'] = '' + else: + if user.check_password('admin'): + context['info_text'] = _( + 'Installation was successfully. Use {username} and ' + '{password} for first login. Important: Please change ' + 'your password!'.format( + username='admin', + password='admin')) + else: + context['info_text'] = '' + else: + # self.request.method == 'POST' + context['user_id'] = self.user.pk return super().get_context_data(**context) diff --git a/tests/integration/users/test_views.py b/tests/integration/users/test_views.py index de90a7cbf..2533ef2fe 100644 --- a/tests/integration/users/test_views.py +++ b/tests/integration/users/test_views.py @@ -64,7 +64,9 @@ class TestUserLoginView(TestCase): def test_get(self): response = self.client.get(self.url) - self.assertEqual(response.status_code, 405) + self.assertEqual(response.status_code, 200) + self.assertTrue( + json.loads(response.content.decode()).get('info_text')) def test_post_no_data(self): response = self.client.post(self.url)