Sith/core/views.py

72 lines
2.3 KiB
Python
Raw Normal View History

2015-11-19 08:46:05 +00:00
from django.shortcuts import render, redirect, get_object_or_404
2015-11-18 08:44:06 +00:00
from django.http import HttpResponse
2015-11-18 16:09:06 +00:00
from django.contrib.auth import logout as auth_logout
2015-11-18 08:44:06 +00:00
from .models import User
2015-11-18 16:09:06 +00:00
from .forms import RegisteringForm, LoginForm
2015-11-18 08:44:06 +00:00
import logging
logging.basicConfig(level=logging.DEBUG)
2015-11-19 13:44:48 +00:00
# This is a global default context that can be used everywhere and provide default basic values
# It needs to be completed by every function using templates
#context = {'title': 'Bienvenue!',
# 'tests': '',
# }
def index(request, context=None):
if context == None:
return render(request, "core/index.html", {'title': 'Bienvenue!'})
else:
return render(request, "core/index.html", context)
2015-11-18 08:44:06 +00:00
def register(request):
2015-11-19 10:23:08 +00:00
context = {'title': 'Register a user'}
2015-11-18 08:44:06 +00:00
if request.method == 'POST':
form = RegisteringForm(request.POST)
if form.is_valid():
2015-11-18 16:09:06 +00:00
logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name'])
u = form.save()
2015-11-19 10:23:08 +00:00
context['user_registered'] = u
2015-11-19 13:44:48 +00:00
context['tests'] = 'TEST_REGISTER_USER_FORM_OK'
2015-11-18 16:09:06 +00:00
form = RegisteringForm()
2015-11-19 13:44:48 +00:00
else:
context['error'] = 'Erreur'
context['tests'] = 'TEST_REGISTER_USER_FORM_FAIL'
2015-11-18 16:09:06 +00:00
else:
form = RegisteringForm()
2015-11-19 10:23:08 +00:00
context['form'] = form.as_p()
return render(request, "core/register.html", context)
2015-11-18 16:09:06 +00:00
def login(request):
2015-11-19 13:44:48 +00:00
context = {'title': 'Login'}
2015-11-18 16:09:06 +00:00
if request.method == 'POST':
try:
form = LoginForm(request)
form.login()
2015-11-19 13:44:48 +00:00
context['tests'] = 'LOGIN_OK'
return render(request, 'core/index.html', context)
2015-11-18 16:09:06 +00:00
except Exception as e:
logging.debug(e)
2015-11-19 13:44:48 +00:00
context['error'] = "Login failed"
context['tests'] = 'LOGIN_FAIL'
2015-11-18 16:09:06 +00:00
else:
form = LoginForm()
2015-11-19 13:44:48 +00:00
context['form'] = form.as_p()
return render(request, "core/login.html", context)
2015-11-18 08:44:06 +00:00
2015-11-18 16:09:06 +00:00
def logout(request):
auth_logout(request)
return redirect('core:index')
2015-11-19 08:46:05 +00:00
def user(request, user_id=None):
if user_id == None:
return render(request, "core/user.html", {'user_list': User.objects.all})
user = get_object_or_404(User, pk=user_id)
return render(request, "core/user.html", {'profile': user})
def user_edit(request, user_id):
pass