several changes on auth flow
[osm/LW-UI.git] / authosm / views.py
1 #
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 #
16 from django.shortcuts import render
17 from django.contrib.auth import login, logout, authenticate
18 from django.http import HttpResponseRedirect
19 import urllib
20
21
22 # Create your views here.
23 def user_login(request):
24
25 logout(request)
26
27 error_message = ''
28 if request.POST:
29 print request.POST.get('username')
30 print request.POST.get('password')
31 next_page = request.POST.get('next')
32 next_page = urllib.unquote(next_page).decode('iso-8859-2')
33 try:
34 user = authenticate(username=request.POST.get('username'),
35 password=request.POST.get('password'),
36 project_id=request.POST.get('project_id'))
37 except Exception as e:
38 print e
39 res = HttpResponseRedirect('/auth')
40 res.set_cookie('logout_reason', '', max_age=10)
41 return res
42
43 if user and user.is_active:
44 if user.is_authenticated:
45 login(request, user)
46 request.session['token'] = user.session
47 if next_page == "" or next_page is None:
48 return HttpResponseRedirect('/home')
49 else:
50 return HttpResponseRedirect(next_page)
51 else:
52 error_message = 'Login failed!'
53 return render(request, 'login.html', {'error_message':error_message, 'collapsed_sidebar': False})