user.py 6.71 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
5
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
Skia's avatar
Skia committed
6
from django.views.generic.edit import UpdateView
7
from django.views.generic import ListView, DetailView, TemplateView
8
9
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
Skia's avatar
Skia committed
10
from django.conf import settings
Skia's avatar
Skia committed
11
12
import logging

Skia's avatar
Skia committed
13
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
14
from core.views.forms import RegisteringForm, UserPropForm, UserProfileForm
Skia's avatar
Skia committed
15
from core.models import User
Skia's avatar
Skia committed
16

Skia's avatar
Skia committed
17
18
19
20
21
22
def login(request):
    """
    The login view

    Needs to be improve with correct handling of form exceptions
    """
Skia's avatar
Skia committed
23
    return views.login(request, template_name="core/login.jinja")
Skia's avatar
Skia committed
24
25
26
27
28
29
30
31
32
33
34

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
35
    return views.password_change(request, template_name="core/password_change.jinja", post_change_redirect=reverse("core:password_change_done"))
Skia's avatar
Skia committed
36
37
38
39
40

def password_change_done(request):
    """
    Allows a user to change its password
    """
Skia's avatar
Skia committed
41
    return views.password_change_done(request, template_name="core/password_change_done.jinja")
Skia's avatar
Skia committed
42

Skia's avatar
Skia committed
43
def password_reset(request):
Skia's avatar
Skia committed
44
45
46
    """
    Allows someone to enter an email adresse for resetting password
    """
Skia's avatar
Skia committed
47
    return views.password_reset(request,
Skia's avatar
Skia committed
48
49
                                template_name="core/password_reset.jinja",
                                email_template_name="core/password_reset_email.jinja",
Skia's avatar
Skia committed
50
                                post_reset_redirect="core:password_reset_done",
Skia's avatar
Skia committed
51
                               )
Skia's avatar
Skia committed
52
53

def password_reset_done(request):
Skia's avatar
Skia committed
54
55
56
    """
    Confirm that the reset email has been sent
    """
Skia's avatar
Skia committed
57
    return views.password_reset_done(request, template_name="core/password_reset_done.jinja")
Skia's avatar
Skia committed
58

Skia's avatar
Skia committed
59
def password_reset_confirm(request, uidb64=None, token=None):
Skia's avatar
Skia committed
60
61
62
    """
    Provide a reset password formular
    """
Skia's avatar
Skia committed
63
64
    return views.password_reset_confirm(request, uidb64=uidb64, token=token,
                                        post_reset_redirect="core:password_reset_complete",
Skia's avatar
Skia committed
65
                                        template_name="core/password_reset_confirm.jinja",
Skia's avatar
Skia committed
66
67
68
                                       )

def password_reset_complete(request):
Skia's avatar
Skia committed
69
70
71
    """
    Confirm the password has sucessfully been reset
    """
Skia's avatar
Skia committed
72
    return views.password_reset_complete(request,
Skia's avatar
Skia committed
73
                                         template_name="core/password_reset_complete.jinja",
Skia's avatar
Skia committed
74
                                        )
Skia's avatar
Skia committed
75

Skia's avatar
Skia committed
76

Skia's avatar
Skia committed
77
def register(request):
Skia's avatar
Skia committed
78
    context = {}
Skia's avatar
Skia committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    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()
Skia's avatar
Skia committed
93
    return render(request, "core/register.jinja", context)
Skia's avatar
Skia committed
94

Skia's avatar
Skia committed
95
class UserView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
96
97
98
    """
    Display a user's profile
    """
Skia's avatar
Skia committed
99
100
101
    model = User
    pk_url_kwarg = "user_id"
    context_object_name = "profile"
Skia's avatar
Skia committed
102
    template_name = "core/user_detail.jinja"
Skia's avatar
Skia committed
103
104
105
106
107
108

class UserListView(ListView):
    """
    Displays the user list
    """
    model = User
Skia's avatar
Skia committed
109
    template_name = "core/user_list.jinja"
Skia's avatar
Skia committed
110

Skia's avatar
Skia committed
111
class UserUpdateProfileView(CanEditMixin, UpdateView):
Skia's avatar
Skia committed
112
113
114
115
116
    """
    Edit a user's profile
    """
    model = User
    pk_url_kwarg = "user_id"
Skia's avatar
Skia committed
117
    template_name = "core/user_edit.jinja"
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    form_class = UserProfileForm

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.form = self.get_form()
        if self.form.instance.profile_pict and not request.user.is_in_group(settings.SITH_MAIN_BOARD_GROUP):
            self.form.fields.pop('profile_pict', None)
        return self.render_to_response(self.get_context_data(form=self.form))

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.form = self.get_form()
        if self.form.instance.profile_pict and not request.user.is_in_group(settings.SITH_MAIN_BOARD_GROUP):
            self.form.fields.pop('profile_pict', None)
        files = request.FILES.items()
        self.form.process(files)
        if request.user.is_authenticated() and request.user.can_edit(self.object) and self.form.is_valid():
            return super(UserUpdateProfileView, self).form_valid(self.form)
        return self.form_invalid(self.form)

    def get_context_data(self, **kwargs):
        kwargs = super(UserUpdateProfileView, self).get_context_data(**kwargs)
        kwargs['profile'] = self.form.instance
        kwargs['form'] = self.form
        return kwargs
Skia's avatar
Skia committed
143

144
class UserUpdateGroupView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
145
146
147
148
149
    """
    Edit a user's groups
    """
    model = User
    pk_url_kwarg = "user_id"
150
151
152
    template_name = "core/user_group.jinja"
    form_class = modelform_factory(User, fields=['groups'],
            widgets={'groups':CheckboxSelectMultiple})
Skia's avatar
Skia committed
153
    context_object_name = "profile"
Skia's avatar
Skia committed
154

155
156
157
158
class UserToolsView(TemplateView):
    """
    Displays the logged user's tools
    """
Skia's avatar
Skia committed
159
    template_name = "core/user_tools.jinja"
Skia's avatar
Skia committed
160

161
162
163
164
165
166
    def get_context_data(self, **kwargs):
        from launderette.models import Launderette
        kwargs = super(UserToolsView, self).get_context_data(**kwargs)
        kwargs['launderettes'] = Launderette.objects.all()
        return kwargs

Skia's avatar
Skia committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
class UserAccountView(DetailView):
    """
    Display a user's account
    """
    model = User
    pk_url_kwarg = "user_id"
    template_name = "core/user_account.jinja"

    def dispatch(self, request, *arg, **kwargs): # Manually validates the rights
        res = super(UserAccountView, self).dispatch(request, *arg, **kwargs)
        if (self.object == request.user
                or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name'])
                or request.user.is_in_group(settings.SITH_GROUPS['root']['name'])):
            return res
        raise PermissionDenied

    def get_context_data(self, **kwargs):
        kwargs = super(UserAccountView, self).get_context_data(**kwargs)
        kwargs['profile'] = self.object
        try:
            kwargs['customer'] = self.object.customer
        except:
            pass
        # TODO: add list of month where account has activity
        return kwargs