| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni |
| 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 | |
| 17 | from django.shortcuts import render |
| 18 | from django.contrib.auth import login, logout, authenticate |
| 19 | from django.http import HttpResponseRedirect |
| 20 | from sf_user.models import CustomUser |
| 21 | import urllib |
| 22 | import uuid |
| 23 | |
| 24 | |
| 25 | # Create your views here. |
| 26 | def login_view(request): |
| 27 | if hasattr(request.user, "is_guest_user") and request.user.is_guest_user == True: |
| 28 | print "is_guest", request.user.is_guest_user |
| 29 | CustomUser.objects.get(id=request.user.id).delete() |
| 30 | logout(request) |
| 31 | extra_data = {} |
| 32 | next_page = "" |
| 33 | if request.GET: |
| 34 | next_page = request.GET['next'] |
| 35 | error_message = '' |
| 36 | if request.POST: |
| 37 | print request.POST.get('username') |
| 38 | print request.POST.get('password') |
| 39 | next_page = request.POST.get('next') |
| 40 | next_page = urllib.unquote(next_page).decode('iso-8859-2') |
| 41 | user = authenticate(username=request.POST.get('username'), password=request.POST.get('password')) |
| 42 | print "Auth Result: " + str(user) + " -> " + str(user) |
| 43 | if user and user.is_active: |
| 44 | if user.is_authenticated(): |
| 45 | login(request, user) |
| 46 | print next_page |
| 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}) |
| 54 | |
| 55 | |
| 56 | def guest_login(request): |
| 57 | #user = CustomUser.objects.get(id=request.user.id) |
| 58 | if hasattr(request.user, "is_guest_user") and request.user.is_guest_user == True: |
| 59 | CustomUser.objects.get(id=request.user.id).delete() |
| 60 | logout(request) |
| 61 | next = "" |
| 62 | |
| 63 | guest_user_name = "Guest_"+str(uuid.uuid4()) |
| 64 | guest_user_email = guest_user_name+"@guest.it" |
| 65 | guest_user = CustomUser.objects.create(username=guest_user_name, is_guest_user="True", email=guest_user_email, first_name='User', last_name='Guest') |
| 66 | print guest_user.username |
| 67 | |
| 68 | if guest_user and guest_user.is_active: |
| 69 | if guest_user.is_authenticated(): |
| 70 | login(request, guest_user) |
| 71 | if next == "": |
| 72 | return HttpResponseRedirect('/home') |
| 73 | else: |
| 74 | return HttpResponseRedirect(next) |
| 75 | |
| 76 | return render(request, 'login.html', {'error_message': 'New Guest session failed.'}) |
| 77 | |
| 78 | |
| 79 | def register_view(request): |
| 80 | |
| 81 | logout(request) |
| 82 | extra_data = {} |
| 83 | next = "" |
| 84 | if request.GET: |
| 85 | next = request.GET['next'] |
| 86 | error_message = '' |
| 87 | if request.POST: |
| 88 | print "new user" |
| 89 | return render(request, 'register_user.html', {'error_message': error_message, 'collapsed_sidebar': False}) |