Sith/core/views.py

128 lines
4.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-20 14:47:01 +00:00
from django.db import models
2015-11-18 08:44:06 +00:00
2015-11-20 14:47:01 +00:00
from .models import User, Page
from .forms import RegisteringForm, LoginForm, PageForm
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):
"""
The login view
Needs to be improve with correct handling of form exceptions
"""
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):
"""
The logout view:w
"""
2015-11-18 16:09:06 +00:00
auth_logout(request)
return redirect('core:index')
2015-11-19 08:46:05 +00:00
def user(request, user_id=None):
context = {'title': 'View a user'}
2015-11-19 08:46:05 +00:00
if user_id == None:
context['user_list'] = User.objects.all
return render(request, "core/user.html", context)
context['profile'] = get_object_or_404(User, pk=user_id)
return render(request, "core/user.html", context)
2015-11-19 08:46:05 +00:00
def user_edit(request, user_id=None):
user_id = int(user_id)
context = {'title': 'Edit a user'}
if user_id is not None:
user_id = int(user_id)
if request.user.is_authenticated() and (request.user.pk == user_id or request.user.is_superuser):
context['profile'] = get_object_or_404(User, pk=user_id)
return render(request, "core/edit_user.html", context)
return user(request, user_id)
2015-11-19 08:46:05 +00:00
2015-11-20 14:47:01 +00:00
def page(request, page_name=None):
context = {'title': 'View a Page'}
if page_name == None:
context['page_list'] = Page.objects.all
return render(request, "core/page.html", context)
context['page'] = Page.get_page_by_full_name(page_name)
if context['page'] is not None:
2015-11-23 12:30:30 +00:00
context['view_page'] = True
2015-11-20 14:47:01 +00:00
context['title'] = context['page'].title
context['test'] = "PAGE_FOUND"
else:
2015-11-20 14:47:01 +00:00
context['title'] = "This page does not exist"
context['new_page'] = page_name
context['test'] = "PAGE_NOT_FOUND"
return render(request, "core/page.html", context)
def page_edit(request, page_name=None):
context = {'title': 'Edit a page',
'page_name': page_name}
p = Page.get_page_by_full_name(page_name)
2015-11-20 14:47:01 +00:00
if p == None:
2015-11-23 16:32:31 +00:00
# TODO: guess page name by splitting on '/'
# Same for the parent, try to guess
2015-11-20 14:47:01 +00:00
p = Page(name=page_name)
if request.method == 'POST':
f = PageForm(request.POST, instance=p)
if f.is_valid():
f.save()
context['tests'] = "PAGE_SAVED"
2015-11-23 12:30:30 +00:00
else:
context['tests'] = "PAGE_NOT_SAVED"
2015-11-23 12:30:30 +00:00
else:
context['tests'] = "POST_NOT_RECEIVED"
f = PageForm(instance=p)
2015-11-23 12:30:30 +00:00
context['page'] = p
context['page_form'] = f.as_p()
2015-11-20 14:47:01 +00:00
return render(request, 'core/page.html', context)