Merge pull request #1786 from normanjaeckel/LoginInfoView

Added login info text in config and view.
This commit is contained in:
Norman Jäckel 2016-01-09 10:24:56 +01:00
commit 6c33c60600
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'),
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(

View File

@ -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='<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)

View File

@ -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)