Commit 7ce54a3ae1f88b17f7bc9ae1947946da888bf5fb
1 parent
2079e350
Exists in
master
and in
3 other branches
Refactoring users
Showing
10 changed files
with
213 additions
and
360 deletions
Show diff stats
amadeus/settings.py
localization/migrations/0001_initial.py
users/admin.py
... | ... | @@ -1,10 +0,0 @@ |
1 | -from django.contrib import admin | |
2 | -from .models import User | |
3 | -from .forms import UserForm | |
4 | - | |
5 | -class UserAdmin(admin.ModelAdmin): | |
6 | - list_display = ['username', 'name', 'email', 'is_staff', 'is_active'] | |
7 | - search_fields = ['username', 'name', 'email'] | |
8 | - form = UserForm | |
9 | - | |
10 | -admin.site.register(User, UserAdmin) |
users/forms.py
1 | 1 | # coding=utf-8 |
2 | -import os, re | |
3 | -from datetime import date | |
4 | -from pycpfcnpj import cpfcnpj | |
5 | -from django.conf import settings | |
6 | 2 | from django import forms |
7 | 3 | from django.utils.translation import ugettext_lazy as _ |
8 | 4 | from rolepermissions.shortcuts import assign_role |
9 | -from django.contrib.auth.forms import UserCreationForm | |
10 | 5 | from .models import User |
11 | 6 | |
12 | 7 | class Validation(forms.ModelForm): |
13 | - def clean_email(self): | |
14 | - email = self.cleaned_data['email'] | |
15 | - | |
16 | - if User.objects.filter(email = email).exists(): | |
17 | - raise forms.ValidationError(_('There is already a registered User with this e-mail')) | |
18 | - | |
19 | - return email | |
20 | - | |
21 | - def validate_cpf(self, cpf): | |
22 | - cpf = ''.join(re.findall('\d', str(cpf))) | |
23 | - | |
24 | - if not cpf == '': | |
25 | - if cpfcnpj.validate(cpf): | |
26 | - return True | |
27 | - | |
28 | - return False | |
29 | - | |
30 | - return True | |
31 | - | |
32 | - def clean_cpf(self): | |
33 | - cpf = self.cleaned_data['cpf'] | |
34 | - | |
35 | - if not self.validate_cpf(cpf): | |
36 | - raise forms.ValidationError(_('Please enter a valid CPF')) | |
37 | - | |
38 | - return cpf | |
39 | - | |
40 | - def clean_birth_date(self): | |
41 | - birth_date = self.cleaned_data['birth_date'] | |
42 | - | |
43 | - if not birth_date is None: | |
44 | - if birth_date >= date.today(): | |
45 | - raise forms.ValidationError(_('Please enter a valid date')) | |
46 | - | |
47 | - return birth_date | |
48 | - | |
49 | -class ValidationRegister(Validation): | |
50 | 8 | def clean_password(self): |
51 | 9 | password = self.cleaned_data.get('password') |
52 | 10 | |
... | ... | @@ -70,35 +28,6 @@ class ValidationRegister(Validation): |
70 | 28 | |
71 | 29 | return password2 |
72 | 30 | |
73 | -class UserForm(Validation): | |
74 | - def save(self, commit=True): | |
75 | - super(AdminUserForm, self).save(commit=False) | |
76 | - | |
77 | - self.instance.set_password(self.cleaned_data['password']) | |
78 | - self.instance.save() | |
79 | - | |
80 | - if self.instance.is_staff: | |
81 | - assign_role(self.instance, 'system_admin') | |
82 | - elif self.instance.type_profile == 2: | |
83 | - assign_role(self.instance, 'student') | |
84 | - elif self.instance.type_profile == 1: | |
85 | - assign_role(self.instance, 'professor') | |
86 | - elif self.instance.type_profile == 3: | |
87 | - assign_role(self.instance, 'coordinator') | |
88 | - | |
89 | - self.instance.save() | |
90 | - | |
91 | - return self.instance | |
92 | - | |
93 | - class Meta: | |
94 | - model = User | |
95 | - fields = ['username', 'name', 'email', 'password', | |
96 | - 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', | |
97 | - 'image', 'titration', 'year_titration', 'institution', 'curriculum', 'is_staff', 'is_active'] | |
98 | - widgets = { | |
99 | - 'password':forms.PasswordInput | |
100 | - } | |
101 | - | |
102 | 31 | class RegisterUserForm(ValidationRegister): |
103 | 32 | password = forms.CharField(label=_('Password'), widget=forms.PasswordInput) |
104 | 33 | password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput) |
... | ... | @@ -111,17 +40,12 @@ class RegisterUserForm(ValidationRegister): |
111 | 40 | self.instance.set_password(self.cleaned_data['password']) |
112 | 41 | |
113 | 42 | self.instance.save() |
43 | + | |
44 | + if self.instance.type_profile == 2: | |
45 | + assign_role(self.instance, 'student') | |
114 | 46 | |
115 | 47 | return self.instance |
116 | 48 | |
117 | 49 | class Meta: |
118 | 50 | model = User |
119 | - fields = ['username', 'name', 'email',] | |
120 | - | |
121 | -class UpdateProfileForm(Validation): | |
122 | - | |
123 | - class Meta: | |
124 | - model = User | |
125 | - fields = ['username', 'name', 'email', 'birth_date', 'city', | |
126 | - 'state', 'gender', 'cpf', 'phone', 'image', 'titration', | |
127 | - 'year_titration', 'institution', 'curriculum'] | |
51 | + fields = ['username', 'name', 'last_name', 'social_name',] | ... | ... |
users/migrations/0001_initial.py
1 | 1 | # -*- coding: utf-8 -*- |
2 | -# Generated by Django 1.10 on 2016-12-19 17:21 | |
2 | +# Generated by Django 1.10 on 2016-12-20 18:17 | |
3 | 3 | from __future__ import unicode_literals |
4 | 4 | |
5 | 5 | import django.contrib.auth.models |
6 | 6 | import django.core.validators |
7 | 7 | from django.db import migrations, models |
8 | -import django.db.models.deletion | |
9 | 8 | import re |
10 | 9 | |
11 | 10 | |
... | ... | @@ -15,7 +14,6 @@ class Migration(migrations.Migration): |
15 | 14 | |
16 | 15 | dependencies = [ |
17 | 16 | ('auth', '0008_alter_user_username_max_length'), |
18 | - ('localization', '0001_initial'), | |
19 | 17 | ] |
20 | 18 | |
21 | 19 | operations = [ |
... | ... | @@ -26,25 +24,18 @@ class Migration(migrations.Migration): |
26 | 24 | ('password', models.CharField(max_length=128, verbose_name='password')), |
27 | 25 | ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), |
28 | 26 | ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), |
29 | - ('username', models.CharField(help_text='A short name that will be used to identify you in the platform and to access it', max_length=35, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[\\w.@+-]+$', 32), 'Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .', 'invalid')], verbose_name='Login')), | |
30 | - ('email', models.EmailField(max_length=254, unique=True, verbose_name='Mail')), | |
31 | - ('name', models.CharField(max_length=100, verbose_name='Name')), | |
32 | - ('gender', models.CharField(blank=True, choices=[('M', 'Male'), ('F', 'Female')], max_length=1, null=True, verbose_name='Gender')), | |
27 | + ('email', models.EmailField(help_text='Your email address that will be used to access the platform', max_length=254, unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[\\w.@+-]+$', 32), 'Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .', 'invalid')], verbose_name='Mail')), | |
28 | + ('username', models.CharField(max_length=100, verbose_name='Name')), | |
29 | + ('last_name', models.CharField(max_length=100, verbose_name='Last Name')), | |
30 | + ('social_name', models.CharField(blank=True, max_length=100, null=True, verbose_name='Social Name')), | |
31 | + ('description', models.TextField(blank=True, verbose_name='Description')), | |
33 | 32 | ('image', models.ImageField(blank=True, null=True, upload_to='users/', verbose_name='Photo')), |
34 | - ('birth_date', models.DateField(blank=True, null=True, verbose_name='Birth Date')), | |
35 | - ('phone', models.CharField(blank=True, max_length=30, verbose_name='Phone')), | |
36 | - ('cpf', models.CharField(blank=True, max_length=15, null=True, verbose_name='CPF')), | |
37 | - ('type_profile', models.IntegerField(blank=True, choices=[(1, 'Professor'), (2, 'Student')], null=True, verbose_name='Type')), | |
38 | - ('titration', models.CharField(blank=True, max_length=50, null=True, verbose_name='Titration')), | |
39 | - ('year_titration', models.CharField(blank=True, max_length=4, null=True, verbose_name='Year of titration')), | |
40 | - ('institution', models.CharField(blank=True, max_length=50, null=True, verbose_name='Institution')), | |
41 | - ('curriculum', models.FileField(blank=True, null=True, upload_to='users/curriculum/', verbose_name='Curriculum')), | |
33 | + ('type_profile', models.IntegerField(blank=True, choices=[(1, 'Professor'), (2, 'Student'), (3, 'Coordinator')], null=True, verbose_name='Type')), | |
42 | 34 | ('date_created', models.DateTimeField(auto_now_add=True, verbose_name='Create Date')), |
35 | + ('show_email', models.IntegerField(choices=[(1, 'Allow everyone to see my address'), (2, 'Only classmates can see my address'), (3, 'Nobody can see my address')], null=True, verbose_name='Show email?')), | |
43 | 36 | ('is_staff', models.BooleanField(default=False, verbose_name='Administrator')), |
44 | 37 | ('is_active', models.BooleanField(default=True, verbose_name='Active')), |
45 | - ('city', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='localization.City', verbose_name='City')), | |
46 | 38 | ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), |
47 | - ('state', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='localization.State', verbose_name='State')), | |
48 | 39 | ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), |
49 | 40 | ], |
50 | 41 | options={ | ... | ... |
users/migrations/0002_auto_20161219_2218.py
... | ... | @@ -1,20 +0,0 @@ |
1 | -# -*- coding: utf-8 -*- | |
2 | -# Generated by Django 1.10 on 2016-12-20 01:18 | |
3 | -from __future__ import unicode_literals | |
4 | - | |
5 | -from django.db import migrations, models | |
6 | - | |
7 | - | |
8 | -class Migration(migrations.Migration): | |
9 | - | |
10 | - dependencies = [ | |
11 | - ('users', '0001_initial'), | |
12 | - ] | |
13 | - | |
14 | - operations = [ | |
15 | - migrations.AlterField( | |
16 | - model_name='user', | |
17 | - name='type_profile', | |
18 | - field=models.IntegerField(blank=True, choices=[(1, 'Professor'), (2, 'Student'), (3, 'Coordinator')], null=True, verbose_name='Type'), | |
19 | - ), | |
20 | - ] |
users/models.py
... | ... | @@ -6,37 +6,28 @@ from django.utils.translation import ugettext_lazy as _ |
6 | 6 | from django.contrib.auth.models import AbstractBaseUser, UserManager, PermissionsMixin |
7 | 7 | from django.contrib.staticfiles.templatetags.staticfiles import static |
8 | 8 | |
9 | -from localization.models import City, State | |
10 | - | |
11 | 9 | class User(AbstractBaseUser, PermissionsMixin): |
12 | 10 | |
13 | - username = models.CharField(_('Login'), max_length = 35, unique = True, validators = [ | |
11 | + email = models.EmailField(_('Mail'), unique = True, validators = [ | |
14 | 12 | validators.RegexValidator( |
15 | 13 | re.compile('^[\w.@+-]+$'), |
16 | 14 | _('Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .') |
17 | 15 | , 'invalid' |
18 | 16 | ) |
19 | - ], help_text = _('A short name that will be used to identify you in the platform and to access it')) | |
20 | - email = models.EmailField(_('Mail'), unique = True) | |
21 | - name = models.CharField(_('Name'), max_length = 100) | |
22 | - city = models.ForeignKey(City, verbose_name = _('City'), blank = True, null = True) | |
23 | - state = models.ForeignKey(State, verbose_name = _('State'), blank = True, null = True) | |
24 | - gender = models.CharField(_('Gender'), max_length = 1, choices = (('M', _('Male')), ('F', _('Female'))), blank=True, null=True) | |
17 | + ], help_text = _('Your email address that will be used to access the platform')) | |
18 | + username = models.CharField(_('Name'), max_length = 100) | |
19 | + last_name = models.CharField(_('Last Name'), max_length = 100) | |
20 | + social_name = models.CharField(_('Social Name'), max_length = 100, blank = True, null = True) | |
21 | + description = models.TextField(_('Description'), blank = True) | |
25 | 22 | image = models.ImageField(verbose_name = _('Photo'), null=True, blank = True, upload_to = 'users/') |
26 | - birth_date = models.DateField(_('Birth Date'), null=True, blank=True) | |
27 | - phone = models.CharField(_('Phone'), max_length = 30, blank = True) | |
28 | - cpf = models.CharField(_('CPF'), max_length = 15, blank=True, null=True) | |
29 | 23 | type_profile = models.IntegerField(_('Type'), null = True, blank = True, choices = ((1, _('Professor')), (2, _('Student')), (3, _('Coordinator')))) |
30 | - titration = models.CharField(_('Titration'), max_length = 50, blank = True, null = True) | |
31 | - year_titration = models.CharField(_('Year of titration'), max_length = 4, blank = True, null = True) | |
32 | - institution = models.CharField(_('Institution'), max_length = 50, blank=True, null=True) | |
33 | - curriculum = models.FileField(verbose_name = _('Curriculum'), upload_to='users/curriculum/', null=True, blank=True) | |
34 | 24 | date_created = models.DateTimeField(_('Create Date'), auto_now_add = True) |
25 | + show_email = models.IntegerField(_('Show email?'), null = True, choices = ((1, _('Allow everyone to see my address')), (2, _('Only classmates can see my address')), (3, _('Nobody can see my address')))) | |
35 | 26 | is_staff = models.BooleanField(_('Administrator'), default = False) |
36 | 27 | is_active = models.BooleanField(_('Active'), default = True) |
37 | 28 | |
38 | - USERNAME_FIELD = 'username' | |
39 | - REQUIRED_FIELDS = ['email'] | |
29 | + USERNAME_FIELD = 'email' | |
30 | + REQUIRED_FIELDS = ['username', 'last_name'] | |
40 | 31 | |
41 | 32 | objects = UserManager() |
42 | 33 | |
... | ... | @@ -45,7 +36,7 @@ class User(AbstractBaseUser, PermissionsMixin): |
45 | 36 | verbose_name_plural = _('Users') |
46 | 37 | |
47 | 38 | def __str__(self): |
48 | - return self.name or self.username | |
39 | + return self.social_name or self.username | |
49 | 40 | |
50 | 41 | def save(self, *args, **kwargs): |
51 | 42 | if not self.is_staff and self.type_profile is None: |
... | ... | @@ -53,9 +44,6 @@ class User(AbstractBaseUser, PermissionsMixin): |
53 | 44 | |
54 | 45 | super(User, self).save(*args, **kwargs) |
55 | 46 | |
56 | - def get_short_name(self): | |
57 | - return self.name | |
58 | - | |
59 | 47 | @property |
60 | 48 | def image_url(self): |
61 | 49 | if self.image and hasattr(self.image, 'url'): | ... | ... |
users/templates/users/login.html
... | ... | @@ -41,8 +41,8 @@ |
41 | 41 | {% csrf_token %} |
42 | 42 | <div class="col-md-10 col-md-offset-1"> |
43 | 43 | <div class="form-group"> |
44 | - <label for="inputEmail" class="control-label"> {% trans 'Username' %}</label> | |
45 | - <input form="form-login" for="inputSmall" type="text" name="username" class="form-control" placeholder="{% trans 'Username' %}" id="inputEmail" value="{% if username %}{{username}}{% endif %}"> | |
44 | + <label for="inputEmail" class="control-label"> {% trans 'Email' %}</label> | |
45 | + <input form="form-login" for="inputSmall" type="text" name="email" class="form-control" placeholder="{% trans 'Email' %}" id="inputEmail" value="{% if username %}{{username}}{% endif %}"> | |
46 | 46 | </div> |
47 | 47 | </div> |
48 | 48 | <div class="col-md-10 col-md-offset-1"> |
... | ... | @@ -51,20 +51,13 @@ |
51 | 51 | <input form="form-login" for="inputSmall" type="password" name="password" class="form-control" id="inputPassword" placeholder="{% trans 'Password' %}"> |
52 | 52 | </div> |
53 | 53 | </div> |
54 | - <div class="col-md-10 col-md-offset-1"> | |
55 | - <div class="checkbox"> | |
56 | - <label> | |
57 | - <input type="checkbox" name="checkbox"><span class="checkbox-material"></span> {% trans 'Remember Email' %} | |
58 | - </label> | |
59 | - </div> | |
60 | - </div> | |
61 | 54 | </form> |
62 | 55 | <div class="row"> |
63 | 56 | <div class="col-md-5 col-xs-6 col-sm-6 col-lg-5 col-lg-offset-1 col-md-offset-1 text-center"> |
64 | 57 | <button type="submite" class="btn btn-success btn-raised btn-block" form="form-login" style="position: initial;"> {% trans 'Log in' %} </button> |
65 | 58 | </div> |
66 | 59 | <div class="col-md-5 col-xs-6 col-sm-6 col-lg-5 text-center"> |
67 | - <a class="btn btn-default btn-raised btn-block" href="#" formaction="#" style="position: initial;">{% trans 'Guest' %}</a> | |
60 | + <a class="btn btn-default btn-raised btn-block" href="#" formaction="#" style="position: initial;">{% trans 'Sign Up' %}</a> | |
68 | 61 | </div> |
69 | 62 | </div> |
70 | 63 | <div class="row"> |
... | ... | @@ -74,7 +67,6 @@ |
74 | 67 | </div> |
75 | 68 | </div> |
76 | 69 | </div> |
77 | - <a class="btn btn-raised btn-primary btn-block" href="#">{% trans 'Sign Up' %} </a> | |
78 | 70 | </div> |
79 | 71 | </div> |
80 | 72 | ... | ... |
users/urls.py
... | ... | @@ -2,21 +2,7 @@ from django.conf.urls import url |
2 | 2 | |
3 | 3 | from . import views |
4 | 4 | |
5 | - | |
6 | 5 | urlpatterns = [ |
7 | 6 | url(r'^login/$', views.login, name='login'), |
8 | - #url(r'^$', views.UsersListView.as_view(), name='manage'), | |
9 | - url(r'^create/$', views.Create.as_view(), name='create'), | |
10 | - url(r'^edit/(?P<username>[\w_-]+)/$', views.Update.as_view(), name='update'), | |
11 | - url(r'^view/(?P<username>[\w_-]+)/$', views.View.as_view(), name='view'), | |
12 | - url(r'^delete/(?P<username>[\w_-]+)/$', views.delete_user, name='delete'), | |
13 | - url(r'^remove/(?P<username>[\w_-]+)/$', views.remove_account, name='remove'), | |
14 | - url(r'^profile/$', views.Profile.as_view(), name='profile'), | |
15 | - url(r'^search/$', views.SearchView.as_view(), name='search'), | |
16 | - # | |
17 | - url(r'^profile/update/$', views.UpdateProfile.as_view(), name='update_profile'), | |
18 | - url(r'^profile/change_password/$', views.Change_password.as_view(), name='change_password'), | |
19 | - url(r'^profile/remove_account/$', views.Remove_account.as_view(), name='remove_account'), | |
20 | - url(r'^profile/delete/$', views.DeleteUser.as_view(), name='delete_profile'), | |
21 | - | |
7 | + | |
22 | 8 | ] | ... | ... |
users/views.py
... | ... | @@ -3,8 +3,9 @@ from django.db.models import Q |
3 | 3 | from django.views import generic |
4 | 4 | from django.contrib import messages |
5 | 5 | from rolepermissions.mixins import HasRoleMixin |
6 | +from django.contrib.auth import authenticate, login as login_user | |
6 | 7 | from django.contrib.auth.mixins import LoginRequiredMixin |
7 | -from django.core.urlresolvers import reverse_lazy | |
8 | +from django.core.urlresolvers import reverse, reverse_lazy | |
8 | 9 | from django.utils.translation import ugettext_lazy as _ |
9 | 10 | from rolepermissions.shortcuts import assign_role |
10 | 11 | from rolepermissions.verifications import has_role |
... | ... | @@ -12,7 +13,6 @@ from itertools import chain |
12 | 13 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
13 | 14 | |
14 | 15 | from .models import User |
15 | -from .forms import UserForm, UpdateProfileForm | |
16 | 16 | |
17 | 17 | |
18 | 18 | #API IMPORTS |
... | ... | @@ -21,204 +21,204 @@ from .serializers import UserSerializer |
21 | 21 | from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly |
22 | 22 | |
23 | 23 | # ================ ADMIN ======================= |
24 | -class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView): | |
25 | - | |
26 | - allowed_roles = ['system_admin'] | |
27 | - #login_url = reverse_lazy("core:home") | |
28 | - redirect_field_name = 'next' | |
29 | - template_name = 'list_users.html' | |
30 | - context_object_name = 'users' | |
31 | - paginate_by = 10 | |
32 | - | |
33 | - def get_queryset(self): | |
34 | - search = self.request.GET.get('search', None) | |
35 | - | |
36 | - if search is None: | |
37 | - users = User.objects.all().order_by('name').exclude( username = self.request.user.username) | |
38 | - else: | |
39 | - users = User.objects.filter(Q(username = search) | Q(name = search) | Q(name__icontains = search) | Q(username__icontains = search)).exclude( username = self.request.user.username) | |
40 | - | |
41 | - return users | |
42 | - | |
43 | - def get_context_data (self, **kwargs): | |
44 | - context = super(UsersListView, self).get_context_data(**kwargs) | |
45 | - context['title'] = 'Manage Users' | |
46 | - return context | |
47 | - | |
48 | -class Create(HasRoleMixin, LoginRequiredMixin, generic.edit.CreateView): | |
49 | - | |
50 | - allowed_roles = ['system_admin'] | |
51 | - #login_url = reverse_lazy("core:home") | |
52 | - redirect_field_name = 'next' | |
53 | - template_name = 'users/create.html' | |
54 | - form_class = UserForm | |
55 | - context_object_name = 'acc' | |
56 | - success_url = reverse_lazy('users:manage') | |
57 | - | |
58 | - def form_valid(self, form): | |
59 | - self.object = form.save() | |
60 | - | |
61 | - if self.object.type_profile == 2: | |
62 | - assign_role(self.object, 'student') | |
63 | - elif self.object.type_profile == 1: | |
64 | - assign_role(self.object, 'professor') | |
65 | - elif self.object.is_staff: | |
66 | - assign_role(self.object, 'system_admin') | |
67 | - | |
68 | - self.object.save() | |
69 | - | |
70 | - messages.success(self.request, ('User ')+self.object.name+(' created successfully!')) | |
71 | - | |
72 | - return super(Create, self).form_valid(form) | |
73 | - def get_context_data (self, **kwargs): | |
74 | - context = super(Create, self).get_context_data(**kwargs) | |
75 | - context['title'] = "Add User" | |
76 | - return context | |
77 | - | |
78 | -class Update(HasRoleMixin, LoginRequiredMixin, generic.UpdateView): | |
79 | - | |
80 | - allowed_roles = ['system_admin'] | |
81 | - #login_url = reverse_lazy("core:home") | |
82 | - redirect_field_name = 'next' | |
83 | - template_name = 'users/update.html' | |
84 | - slug_field = 'username' | |
85 | - slug_url_kwarg = 'username' | |
86 | - context_object_name = 'acc' | |
87 | - model = User | |
88 | - form_class = UserForm | |
89 | - success_url = reverse_lazy('users:manage') | |
90 | - | |
91 | - def form_valid(self, form): | |
92 | - self.object = form.save(commit = False) | |
93 | - | |
94 | - if self.object.type_profile == 2: | |
95 | - assign_role(self.object, 'student') | |
96 | - elif self.object.type_profile == 1: | |
97 | - assign_role(self.object, 'professor') | |
98 | - elif self.object.is_staff: | |
99 | - assign_role(self.object, 'system_admin') | |
100 | - | |
101 | - self.object.save() | |
102 | - | |
103 | - messages.success(self.request, _('User ')+self.object.name+_(' updated successfully!')) | |
104 | - | |
105 | - return super(Update, self).form_valid(form) | |
106 | - | |
107 | - def get_context_data (self, **kwargs): | |
108 | - context = super(Update, self).get_context_data(**kwargs) | |
109 | - context['title'] = "Update User" | |
110 | - return context | |
111 | - | |
112 | -class View(LoginRequiredMixin, generic.DetailView): | |
113 | - | |
114 | - #login_url = reverse_lazy("core:home") | |
115 | - redirect_field_name = 'next' | |
116 | - model = User | |
117 | - context_object_name = 'acc' | |
118 | - template_name = 'users/view.html' | |
119 | - slug_field = 'username' | |
120 | - slug_url_kwarg = 'username' | |
121 | - | |
122 | - def get_context_data (self, **kwargs): | |
123 | - context = super(View, self).get_context_data(**kwargs) | |
124 | - context['title'] = "User" | |
125 | - return context | |
126 | - | |
127 | -def delete_user(request,username): | |
128 | - user = get_object_or_404(User,username = username) | |
129 | - user.delete() | |
130 | - messages.success(request,_("User deleted Successfully!")) | |
131 | - return redirect('users:manage') | |
132 | - | |
133 | -def remove_account(request,username): | |
134 | - user = get_object_or_404(User,username = username) | |
135 | - user.delete() | |
136 | - messages.success(request,_("User deleted Successfully!")) | |
137 | - #return redirect('core:logout') | |
138 | - | |
139 | -class Change_password(generic.TemplateView): | |
140 | - template_name = 'users/change_password.html' | |
141 | - | |
142 | - def get_context_data (self, **kwargs): | |
143 | - context = super(Change_password, self).get_context_data(**kwargs) | |
144 | - context['title'] = "Change Password" | |
145 | - return context | |
146 | - | |
147 | -class Remove_account(generic.TemplateView): | |
148 | - template_name = 'users/remove_account.html' | |
149 | - | |
150 | - def get_context_data (self, **kwargs): | |
151 | - context = super(Remove_account, self).get_context_data(**kwargs) | |
152 | - context['title'] = "Remove Account" | |
153 | - return context | |
154 | - | |
155 | -class UpdateProfile(LoginRequiredMixin, generic.edit.UpdateView): | |
156 | - #login_url = reverse_lazy("core:home") | |
157 | - template_name = 'users/edit_profile.html' | |
158 | - form_class = UpdateProfileForm | |
159 | - success_url = reverse_lazy('users:profile') | |
160 | - | |
161 | - def get_object(self): | |
162 | - user = get_object_or_404(User, username = self.request.user.username) | |
163 | - return user | |
164 | - | |
165 | - def get_context_data(self, **kwargs): | |
166 | - context = super(UpdateProfile, self).get_context_data(**kwargs) | |
167 | - context['title'] = 'Update Profile' | |
24 | +# class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView): | |
25 | + | |
26 | +# allowed_roles = ['system_admin'] | |
27 | +# #login_url = reverse_lazy("core:home") | |
28 | +# redirect_field_name = 'next' | |
29 | +# template_name = 'list_users.html' | |
30 | +# context_object_name = 'users' | |
31 | +# paginate_by = 10 | |
32 | + | |
33 | +# def get_queryset(self): | |
34 | +# search = self.request.GET.get('search', None) | |
35 | + | |
36 | +# if search is None: | |
37 | +# users = User.objects.all().order_by('name').exclude( username = self.request.user.username) | |
38 | +# else: | |
39 | +# users = User.objects.filter(Q(username = search) | Q(name = search) | Q(name__icontains = search) | Q(username__icontains = search)).exclude( username = self.request.user.username) | |
40 | + | |
41 | +# return users | |
42 | + | |
43 | +# def get_context_data (self, **kwargs): | |
44 | +# context = super(UsersListView, self).get_context_data(**kwargs) | |
45 | +# context['title'] = 'Manage Users' | |
46 | +# return context | |
47 | + | |
48 | +# class Create(HasRoleMixin, LoginRequiredMixin, generic.edit.CreateView): | |
49 | + | |
50 | +# allowed_roles = ['system_admin'] | |
51 | +# #login_url = reverse_lazy("core:home") | |
52 | +# redirect_field_name = 'next' | |
53 | +# template_name = 'users/create.html' | |
54 | +# form_class = UserForm | |
55 | +# context_object_name = 'acc' | |
56 | +# success_url = reverse_lazy('users:manage') | |
57 | + | |
58 | +# def form_valid(self, form): | |
59 | +# self.object = form.save() | |
60 | + | |
61 | +# if self.object.type_profile == 2: | |
62 | +# assign_role(self.object, 'student') | |
63 | +# elif self.object.type_profile == 1: | |
64 | +# assign_role(self.object, 'professor') | |
65 | +# elif self.object.is_staff: | |
66 | +# assign_role(self.object, 'system_admin') | |
67 | + | |
68 | +# self.object.save() | |
69 | + | |
70 | +# messages.success(self.request, ('User ')+self.object.name+(' created successfully!')) | |
71 | + | |
72 | +# return super(Create, self).form_valid(form) | |
73 | +# def get_context_data (self, **kwargs): | |
74 | +# context = super(Create, self).get_context_data(**kwargs) | |
75 | +# context['title'] = "Add User" | |
76 | +# return context | |
77 | + | |
78 | +# class Update(HasRoleMixin, LoginRequiredMixin, generic.UpdateView): | |
79 | + | |
80 | +# allowed_roles = ['system_admin'] | |
81 | +# #login_url = reverse_lazy("core:home") | |
82 | +# redirect_field_name = 'next' | |
83 | +# template_name = 'users/update.html' | |
84 | +# slug_field = 'username' | |
85 | +# slug_url_kwarg = 'username' | |
86 | +# context_object_name = 'acc' | |
87 | +# model = User | |
88 | +# form_class = UserForm | |
89 | +# success_url = reverse_lazy('users:manage') | |
90 | + | |
91 | +# def form_valid(self, form): | |
92 | +# self.object = form.save(commit = False) | |
93 | + | |
94 | +# if self.object.type_profile == 2: | |
95 | +# assign_role(self.object, 'student') | |
96 | +# elif self.object.type_profile == 1: | |
97 | +# assign_role(self.object, 'professor') | |
98 | +# elif self.object.is_staff: | |
99 | +# assign_role(self.object, 'system_admin') | |
100 | + | |
101 | +# self.object.save() | |
102 | + | |
103 | +# messages.success(self.request, _('User ')+self.object.name+_(' updated successfully!')) | |
104 | + | |
105 | +# return super(Update, self).form_valid(form) | |
106 | + | |
107 | +# def get_context_data (self, **kwargs): | |
108 | +# context = super(Update, self).get_context_data(**kwargs) | |
109 | +# context['title'] = "Update User" | |
110 | +# return context | |
111 | + | |
112 | +# class View(LoginRequiredMixin, generic.DetailView): | |
113 | + | |
114 | +# #login_url = reverse_lazy("core:home") | |
115 | +# redirect_field_name = 'next' | |
116 | +# model = User | |
117 | +# context_object_name = 'acc' | |
118 | +# template_name = 'users/view.html' | |
119 | +# slug_field = 'username' | |
120 | +# slug_url_kwarg = 'username' | |
121 | + | |
122 | +# def get_context_data (self, **kwargs): | |
123 | +# context = super(View, self).get_context_data(**kwargs) | |
124 | +# context['title'] = "User" | |
125 | +# return context | |
126 | + | |
127 | +# def delete_user(request,username): | |
128 | +# user = get_object_or_404(User,username = username) | |
129 | +# user.delete() | |
130 | +# messages.success(request,_("User deleted Successfully!")) | |
131 | +# return redirect('users:manage') | |
132 | + | |
133 | +# def remove_account(request,username): | |
134 | +# user = get_object_or_404(User,username = username) | |
135 | +# user.delete() | |
136 | +# messages.success(request,_("User deleted Successfully!")) | |
137 | +# #return redirect('core:logout') | |
138 | + | |
139 | +# class Change_password(generic.TemplateView): | |
140 | +# template_name = 'users/change_password.html' | |
141 | + | |
142 | +# def get_context_data (self, **kwargs): | |
143 | +# context = super(Change_password, self).get_context_data(**kwargs) | |
144 | +# context['title'] = "Change Password" | |
145 | +# return context | |
146 | + | |
147 | +# class Remove_account(generic.TemplateView): | |
148 | +# template_name = 'users/remove_account.html' | |
149 | + | |
150 | +# def get_context_data (self, **kwargs): | |
151 | +# context = super(Remove_account, self).get_context_data(**kwargs) | |
152 | +# context['title'] = "Remove Account" | |
153 | +# return context | |
154 | + | |
155 | +# class UpdateProfile(LoginRequiredMixin, generic.edit.UpdateView): | |
156 | +# #login_url = reverse_lazy("core:home") | |
157 | +# template_name = 'users/edit_profile.html' | |
158 | +# form_class = UpdateProfileForm | |
159 | +# success_url = reverse_lazy('users:profile') | |
160 | + | |
161 | +# def get_object(self): | |
162 | +# user = get_object_or_404(User, username = self.request.user.username) | |
163 | +# return user | |
164 | + | |
165 | +# def get_context_data(self, **kwargs): | |
166 | +# context = super(UpdateProfile, self).get_context_data(**kwargs) | |
167 | +# context['title'] = 'Update Profile' | |
168 | 168 | |
169 | - context['form'] = UpdateProfileForm(instance = self.object) | |
169 | +# context['form'] = UpdateProfileForm(instance = self.object) | |
170 | 170 | |
171 | - return context | |
171 | +# return context | |
172 | 172 | |
173 | - def form_valid(self, form): | |
174 | - form.save() | |
175 | - messages.success(self.request, _('Profile edited successfully!')) | |
173 | +# def form_valid(self, form): | |
174 | +# form.save() | |
175 | +# messages.success(self.request, _('Profile edited successfully!')) | |
176 | 176 | |
177 | - return super(UpdateProfile, self).form_valid(form) | |
177 | +# return super(UpdateProfile, self).form_valid(form) | |
178 | 178 | |
179 | -class DeleteUser(LoginRequiredMixin, generic.edit.DeleteView): | |
180 | - allowed_roles = ['student'] | |
181 | - #login_url = reverse_lazy("core:home") | |
182 | - model = User | |
179 | +# class DeleteUser(LoginRequiredMixin, generic.edit.DeleteView): | |
180 | +# allowed_roles = ['student'] | |
181 | +# #login_url = reverse_lazy("core:home") | |
182 | +# model = User | |
183 | 183 | |
184 | - #success_url = reverse_lazy('core:index') | |
185 | - success_message = "Deleted Successfully" | |
184 | +# #success_url = reverse_lazy('core:index') | |
185 | +# success_message = "Deleted Successfully" | |
186 | 186 | |
187 | - def get_queryset(self): | |
188 | - user = get_object_or_404(User, username = self.request.user.username) | |
189 | - return user | |
187 | +# def get_queryset(self): | |
188 | +# user = get_object_or_404(User, username = self.request.user.username) | |
189 | +# return user | |
190 | 190 | |
191 | 191 | |
192 | -class Profile(LoginRequiredMixin, generic.DetailView): | |
192 | +# class Profile(LoginRequiredMixin, generic.DetailView): | |
193 | 193 | |
194 | - #login_url = reverse_lazy("core:home") | |
195 | - redirect_field_name = 'next' | |
196 | - context_object_name = 'user' | |
197 | - template_name = 'users/profile.html' | |
194 | +# #login_url = reverse_lazy("core:home") | |
195 | +# redirect_field_name = 'next' | |
196 | +# context_object_name = 'user' | |
197 | +# template_name = 'users/profile.html' | |
198 | 198 | |
199 | - def get_object(self): | |
200 | - user = get_object_or_404(User, username = self.request.user.username) | |
201 | - return user | |
199 | +# def get_object(self): | |
200 | +# user = get_object_or_404(User, username = self.request.user.username) | |
201 | +# return user | |
202 | 202 | |
203 | - def get_context_data (self, **kwargs): | |
204 | - context = super(Profile, self).get_context_data(**kwargs) | |
205 | - context['title'] = "Profile" | |
206 | - return context | |
203 | +# def get_context_data (self, **kwargs): | |
204 | +# context = super(Profile, self).get_context_data(**kwargs) | |
205 | +# context['title'] = "Profile" | |
206 | +# return context | |
207 | 207 | |
208 | -class SearchView(LoginRequiredMixin, generic.ListView): | |
208 | +# class SearchView(LoginRequiredMixin, generic.ListView): | |
209 | 209 | |
210 | - #login_url = reverse_lazy("core:home") | |
211 | - redirect_field_name = 'next' | |
212 | - queryset = None | |
213 | - template_name = 'users/search.html' | |
214 | - paginate_by = 10 | |
210 | +# #login_url = reverse_lazy("core:home") | |
211 | +# redirect_field_name = 'next' | |
212 | +# queryset = None | |
213 | +# template_name = 'users/search.html' | |
214 | +# paginate_by = 10 | |
215 | 215 | |
216 | - def get_context_data(self, **kwargs): | |
217 | - context = super(SearchView, self).get_context_data(**kwargs) | |
218 | - search = self.request.GET.get('search', None) | |
216 | +# def get_context_data(self, **kwargs): | |
217 | +# context = super(SearchView, self).get_context_data(**kwargs) | |
218 | +# search = self.request.GET.get('search', None) | |
219 | 219 | |
220 | 220 | |
221 | - return context | |
221 | +# return context | |
222 | 222 | |
223 | 223 | |
224 | 224 | def login(request): |
... | ... | @@ -226,7 +226,7 @@ def login(request): |
226 | 226 | context['title'] = 'Log In' |
227 | 227 | |
228 | 228 | if request.POST: |
229 | - username = request.POST['username'] | |
229 | + username = request.POST['email'] | |
230 | 230 | password = request.POST['password'] |
231 | 231 | user = authenticate(username=username, password=password) |
232 | 232 | if user is not None: | ... | ... |