views.py 4.83 KB
Newer Older
Skia's avatar
Skia committed
1
from django.shortcuts import render, redirect, get_object_or_404
Skia's avatar
Skia committed
2
from django.http import HttpResponse
Skia's avatar
Skia committed
3
from django.contrib.auth import logout as auth_logout
Skia's avatar
Skia committed
4
from django.db import models
Skia's avatar
Skia committed
5

Skia's avatar
Skia committed
6
7
from .models import User, Page
from .forms import RegisteringForm, LoginForm, PageForm
Skia's avatar
Skia committed
8
9
10
11
12

import logging

logging.basicConfig(level=logging.DEBUG)

Skia's avatar
Skia committed
13
14
15
16
17
18
19
20
21
22
23
# 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)
Skia's avatar
Skia committed
24
25

def register(request):
Skia's avatar
Skia committed
26
    context = {'title': 'Register a user'}
Skia's avatar
Skia committed
27
28
29
    if request.method == 'POST':
        form = RegisteringForm(request.POST)
        if form.is_valid():
Skia's avatar
Skia committed
30
31
            logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name'])
            u = form.save()
Skia's avatar
Skia committed
32
            context['user_registered'] = u
Skia's avatar
Skia committed
33
            context['tests'] = 'TEST_REGISTER_USER_FORM_OK'
Skia's avatar
Skia committed
34
            form = RegisteringForm()
Skia's avatar
Skia committed
35
36
37
        else:
            context['error'] = 'Erreur'
            context['tests'] = 'TEST_REGISTER_USER_FORM_FAIL'
Skia's avatar
Skia committed
38
39
    else:
        form = RegisteringForm()
Skia's avatar
Skia committed
40
41
    context['form'] = form.as_p()
    return render(request, "core/register.html", context)
Skia's avatar
Skia committed
42
43

def login(request):
Skia's avatar
Skia committed
44
45
46
47
48
    """
    The login view

    Needs to be improve with correct handling of form exceptions
    """
Skia's avatar
Skia committed
49
    context = {'title': 'Login'}
Skia's avatar
Skia committed
50
51
52
53
    if request.method == 'POST':
        try:
            form = LoginForm(request)
            form.login()
Skia's avatar
Skia committed
54
55
            context['tests'] = 'LOGIN_OK'
            return render(request, 'core/index.html', context)
Skia's avatar
Skia committed
56
57
        except Exception as e:
            logging.debug(e)
Skia's avatar
Skia committed
58
59
            context['error'] = "Login failed"
            context['tests'] = 'LOGIN_FAIL'
Skia's avatar
Skia committed
60
61
    else:
        form = LoginForm()
Skia's avatar
Skia committed
62
63
    context['form'] = form.as_p()
    return render(request, "core/login.html", context)
Skia's avatar
Skia committed
64

Skia's avatar
Skia committed
65
def logout(request):
Skia's avatar
Skia committed
66
    """
Skia's avatar
Skia committed
67
    The logout view
Skia's avatar
Skia committed
68
    """
Skia's avatar
Skia committed
69
70
    auth_logout(request)
    return redirect('core:index')
Skia's avatar
Skia committed
71
72

def user(request, user_id=None):
Skia's avatar
Skia committed
73
74
75
    """
    Display a user's profile
    """
Skia's avatar
Skia committed
76
    context = {'title': 'View a user'}
Skia's avatar
Skia committed
77
    if user_id == None:
Skia's avatar
Skia committed
78
79
80
81
        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)
Skia's avatar
Skia committed
82

Skia's avatar
Skia committed
83
def user_edit(request, user_id=None):
Skia's avatar
Skia committed
84
85
86
    """
    This view allows a user, or the allowed users to modify a profile
    """
Skia's avatar
Skia committed
87
88
89
90
91
92
93
94
    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)
Skia's avatar
Skia committed
95

Skia's avatar
Skia committed
96
def page(request, page_name=None):
Skia's avatar
Skia committed
97
98
99
    """
    This view displays a page or the link to create it if 404
    """
Skia's avatar
Skia committed
100
101
102
103
    context = {'title': 'View a Page'}
    if page_name == None:
        context['page_list'] = Page.objects.all
        return render(request, "core/page.html", context)
104
105
    context['page'] = Page.get_page_by_full_name(page_name)
    if context['page'] is not None:
Skia's avatar
Skia committed
106
        context['view_page'] = True
Skia's avatar
Skia committed
107
        context['title'] = context['page'].title
Skia's avatar
Skia committed
108
        context['tests'] = "PAGE_FOUND : "+context['page'].title
109
    else:
Skia's avatar
Skia committed
110
111
        context['title'] = "This page does not exist"
        context['new_page'] = page_name
Skia's avatar
Skia committed
112
        context['tests'] = "PAGE_NOT_FOUND"
Skia's avatar
Skia committed
113
114
115
    return render(request, "core/page.html", context)

def page_edit(request, page_name=None):
Skia's avatar
Skia committed
116
117
118
    """
    page_edit view, able to create a page, save modifications, and display the page ModelForm
    """
Skia's avatar
Skia committed
119
120
    context = {'title': 'Edit a page',
               'page_name': page_name}
121
    p = Page.get_page_by_full_name(page_name)
Skia's avatar
Skia committed
122
    # New page
Skia's avatar
Skia committed
123
    if p == None:
Skia's avatar
Skia committed
124
125
126
127
128
129
130
131
        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
Skia's avatar
Skia committed
132
133
134
135
    if request.method == 'POST':
        f = PageForm(request.POST, instance=p)
        if f.is_valid():
            f.save()
136
            context['tests'] = "PAGE_SAVED"
Skia's avatar
Skia committed
137
        else:
138
            context['tests'] = "PAGE_NOT_SAVED"
Skia's avatar
Skia committed
139
    # Default: display the edit form without change
Skia's avatar
Skia committed
140
    else:
141
142
        context['tests'] = "POST_NOT_RECEIVED"
        f = PageForm(instance=p)
Skia's avatar
Skia committed
143
    context['page'] = p
144
    context['page_form'] = f.as_p()
Skia's avatar
Skia committed
145
146
    return render(request, 'core/page.html', context)