user.py 4.41 KB
Newer Older
Skia's avatar
Skia committed
1
2
# This file contains all the views that concern the user model
from django.shortcuts import render, redirect, get_object_or_404
Skia's avatar
Skia committed
3
from django.contrib.auth import logout as auth_logout, views
Skia's avatar
Skia committed
4
from django.contrib.auth.forms import PasswordChangeForm
5
from django.core.urlresolvers import reverse
Skia's avatar
Skia committed
6
from django.core.mail import send_mail
Skia's avatar
Skia committed
7
8
import logging

Skia's avatar
Skia committed
9
10
from core.views.forms import RegisteringForm, LoginForm, UserEditForm
from core.models import User
Skia's avatar
Skia committed
11

Skia's avatar
Skia committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def login(request):
    """
    The login view

    Needs to be improve with correct handling of form exceptions
    """
    return views.login(request, template_name="core/login.html")

def logout(request):
    """
    The logout view
    """
    return views.logout_then_login(request)

def password_change(request):
    """
    Allows a user to change its password
    """
Skia's avatar
Skia committed
30
    return views.password_change(request, template_name="core/password_change.html", post_change_redirect=reverse("core:password_change_done"))
Skia's avatar
Skia committed
31
32
33
34
35
36
37

def password_change_done(request):
    """
    Allows a user to change its password
    """
    return views.password_change_done(request, template_name="core/password_change_done.html")

Skia's avatar
Skia committed
38
def password_reset(request):
Skia's avatar
Skia committed
39
40
41
    """
    Allows someone to enter an email adresse for resetting password
    """
Skia's avatar
Skia committed
42
43
44
    return views.password_reset(request,
                                template_name="core/password_reset.html",
                                email_template_name="core/password_reset_email.html",
Skia's avatar
Skia committed
45
                                post_reset_redirect="core:password_reset_done",
Skia's avatar
Skia committed
46
                               )
Skia's avatar
Skia committed
47
48

def password_reset_done(request):
Skia's avatar
Skia committed
49
50
51
52
    """
    Confirm that the reset email has been sent
    """
    return views.password_reset_done(request, template_name="core/password_reset_done.html")
Skia's avatar
Skia committed
53

Skia's avatar
Skia committed
54
def password_reset_confirm(request, uidb64=None, token=None):
Skia's avatar
Skia committed
55
56
57
    """
    Provide a reset password formular
    """
Skia's avatar
Skia committed
58
59
60
61
62
63
    return views.password_reset_confirm(request, uidb64=uidb64, token=token,
                                        post_reset_redirect="core:password_reset_complete",
                                        template_name="core/password_reset_confirm.html",
                                       )

def password_reset_complete(request):
Skia's avatar
Skia committed
64
65
66
    """
    Confirm the password has sucessfully been reset
    """
Skia's avatar
Skia committed
67
68
69
    return views.password_reset_complete(request,
                                         template_name="core/password_reset_complete.html",
                                        )
Skia's avatar
Skia committed
70

Skia's avatar
Skia committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def register(request):
    context = {'title': 'Register a user'}
    if request.method == 'POST':
        form = RegisteringForm(request.POST)
        if form.is_valid():
            logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name'])
            u = form.save()
            context['user_registered'] = u
            context['tests'] = 'TEST_REGISTER_USER_FORM_OK'
            form = RegisteringForm()
        else:
            context['error'] = 'Erreur'
            context['tests'] = 'TEST_REGISTER_USER_FORM_FAIL'
    else:
        form = RegisteringForm()
    context['form'] = form.as_p()
    return render(request, "core/register.html", context)

def user(request, user_id=None):
    """
    Display a user's profile
    """
    context = {'title': 'View a user'}
    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)

def user_edit(request, user_id=None):
    """
    This view allows a user, or the allowed users to modify a profile
    """
    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):
            p = get_object_or_404(User, pk=user_id)
            if request.method == 'POST':
                f = UserEditForm(request.POST, instance=p)
                # Saving user
                if f.is_valid():
                    f.save()
                    context['tests'] = "USER_SAVED"
                else:
                    context['tests'] = "USER_NOT_SAVED"
            else:
                f = UserEditForm(instance=p)
            context['profile'] = p
            context['user_form'] = f.as_p()
            return render(request, "core/edit_user.html", context)
    return user(request, user_id)