blob: 2cfe04382f6db7e44388cfd4b84bdb5ca560a8e5 [file] [log] [blame]
lombardofr099364f2018-06-12 11:21:02 +02001#
2# Copyright 2018 EveryUP Srl
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16from django.shortcuts import render
17from django.contrib.auth import login, logout, authenticate
18from django.http import HttpResponseRedirect
19import urllib
20
21
22# Create your views here.
23def user_login(request):
24
25 logout(request)
26
27 error_message = ''
28 if request.POST:
lombardofr55d48e72019-06-21 19:35:14 +020029
lombardofr099364f2018-06-12 11:21:02 +020030 next_page = request.POST.get('next')
31 next_page = urllib.unquote(next_page).decode('iso-8859-2')
lombardofr99f922f2018-07-17 17:27:36 +020032 try:
33 user = authenticate(username=request.POST.get('username'),
34 password=request.POST.get('password'),
35 project_id=request.POST.get('project_id'))
36 except Exception as e:
37 print e
38 res = HttpResponseRedirect('/auth')
39 res.set_cookie('logout_reason', '', max_age=10)
40 return res
41
lombardofr099364f2018-06-12 11:21:02 +020042 if user and user.is_active:
lombardofr99f922f2018-07-17 17:27:36 +020043 if user.is_authenticated:
lombardofr099364f2018-06-12 11:21:02 +020044 login(request, user)
lombardofr835a0b72018-07-18 15:26:16 +020045 request.session['projects'] = user.get_projects()
lombardofr099364f2018-06-12 11:21:02 +020046 if next_page == "" or next_page is None:
47 return HttpResponseRedirect('/home')
48 else:
49 return HttpResponseRedirect(next_page)
50 else:
51 error_message = 'Login failed!'
lombardofr835a0b72018-07-18 15:26:16 +020052 return render(request, 'login.html', {'error_message': error_message, 'collapsed_sidebar': False})