|
1 | | -from django.shortcuts import render |
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from braces.views import LoginRequiredMixin, PermissionRequiredMixin |
| 3 | +from django.conf import settings |
| 4 | +from django.contrib.auth.models import User |
| 5 | +from django.views.generic import CreateView, ListView, UpdateView |
2 | 6 |
|
3 | | -# Create your views here. |
| 7 | +from profiles.forms import UserForm |
| 8 | +from profiles.models import Profile |
| 9 | + |
| 10 | +PROTECTED_VIEW_PERMISSION = 'auth.change_user' |
| 11 | + |
| 12 | + |
| 13 | +class UserListView(LoginRequiredMixin, PermissionRequiredMixin, ListView): |
| 14 | + context_object_name = 'users' |
| 15 | + model = User |
| 16 | + ordering = ('-pk',) |
| 17 | + page_title = 'Users' |
| 18 | + paginate_by = settings.PAGE_SIZE |
| 19 | + permission_required = PROTECTED_VIEW_PERMISSION |
| 20 | + template_name = 'backend/user_list.html' |
| 21 | + |
| 22 | + def get_context_data(self, **kwargs): |
| 23 | + context = super(UserListView, self).get_context_data(**kwargs) |
| 24 | + context['page_title'] = self.page_title |
| 25 | + return context |
| 26 | + |
| 27 | + |
| 28 | +class UserCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateView): |
| 29 | + form_class = UserForm |
| 30 | + model = User |
| 31 | + page_title = 'Create User' |
| 32 | + permission_required = PROTECTED_VIEW_PERMISSION |
| 33 | + template_name = 'backend/user_create.html' |
| 34 | + |
| 35 | + def form_valid(self, form): |
| 36 | + pass |
| 37 | + |
| 38 | + def get_context_data(self, **kwargs): |
| 39 | + context = super(UserCreateView, self).get_context_data(**kwargs) |
| 40 | + context['page_title'] = self.page_title |
| 41 | + return context |
| 42 | + |
| 43 | + |
| 44 | +class UserUpdateView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView): |
| 45 | + form_class = UserForm |
| 46 | + model = User |
| 47 | + page_title = 'Edit User' |
| 48 | + permission_required = PROTECTED_VIEW_PERMISSION |
| 49 | + template_name = 'backend/user_edit.html' |
| 50 | + |
| 51 | + def form_valid(self, form): |
| 52 | + pass |
| 53 | + |
| 54 | + def get_context_data(self, **kwargs): |
| 55 | + context = super(UserUpdateView, self).get_context_data(**kwargs) |
| 56 | + context['page_title'] = self.page_title |
| 57 | + return context |
| 58 | + |
0 commit comments