views.py 2.87 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
5

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

import logging

logging.basicConfig(level=logging.DEBUG)

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

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

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

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

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

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

Skia's avatar
Skia committed
79
80
81
82
83
84
85
86
87
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)
Skia's avatar
Skia committed
88