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
|
2015-11-24 13:01:10 +00:00
|
|
|
from .forms import RegisteringForm, LoginForm, EditUserForm, PageEditForm, PagePropForm
|
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 15:28:49 +00:00
|
|
|
"""
|
|
|
|
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):
|
2015-11-19 15:28:49 +00:00
|
|
|
"""
|
2015-11-24 09:53:16 +00:00
|
|
|
The logout view
|
2015-11-19 15:28:49 +00:00
|
|
|
"""
|
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):
|
2015-11-24 09:53:16 +00:00
|
|
|
"""
|
|
|
|
Display a user's profile
|
|
|
|
"""
|
2015-11-19 15:28:49 +00:00
|
|
|
context = {'title': 'View a user'}
|
2015-11-19 08:46:05 +00:00
|
|
|
if user_id == None:
|
2015-11-19 15:28:49 +00:00
|
|
|
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
|
|
|
|
2015-11-19 15:28:49 +00:00
|
|
|
def user_edit(request, user_id=None):
|
2015-11-24 09:53:16 +00:00
|
|
|
"""
|
|
|
|
This view allows a user, or the allowed users to modify a profile
|
|
|
|
"""
|
2015-11-19 15:28:49 +00:00
|
|
|
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)
|
2015-11-24 13:01:10 +00:00
|
|
|
context['user_form'] = EditUserForm(instance=context['profile']).as_p()
|
2015-11-19 15:28:49 +00:00
|
|
|
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):
|
2015-11-24 09:53:16 +00:00
|
|
|
"""
|
|
|
|
This view displays a page or the link to create it if 404
|
|
|
|
"""
|
2015-11-20 14:47:01 +00:00
|
|
|
context = {'title': 'View a Page'}
|
|
|
|
if page_name == None:
|
|
|
|
context['page_list'] = Page.objects.all
|
|
|
|
return render(request, "core/page.html", context)
|
2015-11-23 16:23:00 +00:00
|
|
|
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
|
2015-11-24 09:53:16 +00:00
|
|
|
context['tests'] = "PAGE_FOUND : "+context['page'].title
|
2015-11-23 16:23:00 +00:00
|
|
|
else:
|
2015-11-20 14:47:01 +00:00
|
|
|
context['title'] = "This page does not exist"
|
|
|
|
context['new_page'] = page_name
|
2015-11-24 09:53:16 +00:00
|
|
|
context['tests'] = "PAGE_NOT_FOUND"
|
2015-11-20 14:47:01 +00:00
|
|
|
return render(request, "core/page.html", context)
|
|
|
|
|
|
|
|
def page_edit(request, page_name=None):
|
2015-11-24 09:53:16 +00:00
|
|
|
"""
|
|
|
|
page_edit view, able to create a page, save modifications, and display the page ModelForm
|
|
|
|
"""
|
2015-11-20 14:47:01 +00:00
|
|
|
context = {'title': 'Edit a page',
|
|
|
|
'page_name': page_name}
|
2015-11-23 16:23:00 +00:00
|
|
|
p = Page.get_page_by_full_name(page_name)
|
2015-11-24 09:53:16 +00:00
|
|
|
# New page
|
2015-11-20 14:47:01 +00:00
|
|
|
if p == None:
|
2015-11-24 09:53:16 +00:00
|
|
|
parent_name = '/'.join(page_name.split('/')[:-1])
|
|
|
|
name = page_name.split('/')[-1]
|
|
|
|
if parent_name == "":
|
|
|
|
p = Page(name=name)
|
|
|
|
else:
|
|
|
|
parent = Page.get_page_by_full_name(parent_name)
|
|
|
|
p = Page(name=name, parent=parent)
|
|
|
|
# Saving page
|
2015-11-20 14:47:01 +00:00
|
|
|
if request.method == 'POST':
|
2015-11-24 13:01:10 +00:00
|
|
|
f = PageEditForm(request.POST, instance=p)
|
2015-11-20 14:47:01 +00:00
|
|
|
if f.is_valid():
|
|
|
|
f.save()
|
2015-11-23 16:23:00 +00:00
|
|
|
context['tests'] = "PAGE_SAVED"
|
2015-11-23 12:30:30 +00:00
|
|
|
else:
|
2015-11-23 16:23:00 +00:00
|
|
|
context['tests'] = "PAGE_NOT_SAVED"
|
2015-11-24 09:53:16 +00:00
|
|
|
# Default: display the edit form without change
|
2015-11-23 12:30:30 +00:00
|
|
|
else:
|
2015-11-23 16:23:00 +00:00
|
|
|
context['tests'] = "POST_NOT_RECEIVED"
|
2015-11-24 13:01:10 +00:00
|
|
|
f = PageEditForm(instance=p)
|
2015-11-23 12:30:30 +00:00
|
|
|
context['page'] = p
|
2015-11-24 13:01:10 +00:00
|
|
|
context['page_edit'] = f.as_p()
|
2015-11-20 14:47:01 +00:00
|
|
|
return render(request, 'core/page.html', context)
|
|
|
|
|
2015-11-24 13:01:10 +00:00
|
|
|
def page_prop(request, page_name=None):
|
|
|
|
"""
|
|
|
|
page_prop view, able to change a page's properties
|
|
|
|
"""
|
|
|
|
context = {'title': 'Page properties',
|
|
|
|
'page_name': page_name}
|
|
|
|
p = Page.get_page_by_full_name(page_name)
|
|
|
|
# New page
|
|
|
|
if p == None:
|
|
|
|
parent_name = '/'.join(page_name.split('/')[:-1])
|
|
|
|
name = page_name.split('/')[-1]
|
|
|
|
if parent_name == "":
|
|
|
|
p = Page(name=name)
|
|
|
|
else:
|
|
|
|
parent = Page.get_page_by_full_name(parent_name)
|
|
|
|
p = Page(name=name, parent=parent)
|
|
|
|
# Saving page
|
|
|
|
if request.method == 'POST':
|
|
|
|
f = PagePropForm(request.POST, instance=p)
|
|
|
|
if f.is_valid():
|
|
|
|
f.save()
|
|
|
|
context['tests'] = "PAGE_SAVED"
|
|
|
|
else:
|
|
|
|
context['tests'] = "PAGE_NOT_SAVED"
|
|
|
|
# Default: display the edit form without change
|
|
|
|
else:
|
|
|
|
context['tests'] = "POST_NOT_RECEIVED"
|
|
|
|
f = PagePropForm(instance=p)
|
|
|
|
context['page'] = p
|
|
|
|
context['page_prop'] = f.as_p()
|
|
|
|
return render(request, 'core/page.html', context)
|