Added login info text in config and view.

This commit is contained in:
Norman Jäckel 2016-01-09 01:10:37 +01:00
parent 9a6ebdb2cf
commit ecf561e305
3 changed files with 42 additions and 3 deletions

View File

@ -75,6 +75,14 @@ def setup_general_config(sender, **kwargs):
group=ugettext_lazy('General'), group=ugettext_lazy('General'),
subgroup=ugettext_lazy('System')) 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 # Projector
yield ConfigVariable( yield ConfigVariable(

View File

@ -191,7 +191,7 @@ class UserLoginView(APIView):
""" """
Login the user. Login the user.
""" """
http_method_names = ['post'] http_method_names = ['get', 'post']
def post(self, *args, **kwargs): def post(self, *args, **kwargs):
form = AuthenticationForm(self.request, data=self.request.data) form = AuthenticationForm(self.request, data=self.request.data)
@ -202,7 +202,36 @@ class UserLoginView(APIView):
return super().post(*args, **kwargs) return super().post(*args, **kwargs)
def get_context_data(self, **context): 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='<strong>admin</strong>',
password='<strong>admin</strong>'))
else:
context['info_text'] = ''
else:
# self.request.method == 'POST'
context['user_id'] = self.user.pk
return super().get_context_data(**context) return super().get_context_data(**context)

View File

@ -64,7 +64,9 @@ class TestUserLoginView(TestCase):
def test_get(self): def test_get(self):
response = self.client.get(self.url) 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): def test_post_no_data(self):
response = self.client.post(self.url) response = self.client.post(self.url)