user.py 3.9 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
4
from django.core.urlresolvers import reverse
Skia's avatar
Skia committed
5
6
from django.views.generic.edit import UpdateView
from django.views.generic import ListView, DetailView
Skia's avatar
Skia committed
7
8
import logging

Skia's avatar
Skia committed
9
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
Skia's avatar
Skia committed
10
from core.views.forms import RegisteringForm, UserGroupsForm
Skia's avatar
Skia committed
11
from core.models import User
Skia's avatar
Skia committed
12

Skia's avatar
Skia committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
31
    return views.password_change(request, template_name="core/password_change.html", post_change_redirect=reverse("core:password_change_done"))
Skia's avatar
Skia committed
32
33
34
35
36
37
38

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

def password_reset_done(request):
Skia's avatar
Skia committed
50
51
52
53
    """
    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
54

Skia's avatar
Skia committed
55
def password_reset_confirm(request, uidb64=None, token=None):
Skia's avatar
Skia committed
56
57
58
    """
    Provide a reset password formular
    """
Skia's avatar
Skia committed
59
60
61
62
63
64
    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
65
66
67
    """
    Confirm the password has sucessfully been reset
    """
Skia's avatar
Skia committed
68
69
70
    return views.password_reset_complete(request,
                                         template_name="core/password_reset_complete.html",
                                        )
Skia's avatar
Skia committed
71

Skia's avatar
Skia committed
72

Skia's avatar
Skia committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)

Skia's avatar
Skia committed
91
class UserView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
92
93
94
    """
    Display a user's profile
    """
Skia's avatar
Skia committed
95
96
97
98
99
100
101
102
103
104
    model = User
    pk_url_kwarg = "user_id"
    context_object_name = "profile"

class UserListView(ListView):
    """
    Displays the user list
    """
    model = User

Skia's avatar
Skia committed
105
class UserUpdateProfileView(CanEditMixin, UpdateView):
Skia's avatar
Skia committed
106
107
108
109
110
111
112
113
    """
    Edit a user's profile
    """
    model = User
    pk_url_kwarg = "user_id"
    template_name = "core/user_edit.html"
    fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', )

Skia's avatar
Skia committed
114
class UserUpdateGroupsView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
115
116
117
118
119
120
121
122
    """
    Edit a user's groups
    """
    model = User
    pk_url_kwarg = "user_id"
    template_name = "core/user_groups.html"
    form_class = UserGroupsForm