Commit 39c75180265ee2727aa459dc3dc8dccbf9d4ccf4
1 parent
6889bf5e
Exists in
master
and in
5 other branches
Resolving password bug in user creation through admin app (again)
Showing
2 changed files
with
27 additions
and
2 deletions
Show diff stats
users/admin.py
1 | from django.contrib import admin | 1 | from django.contrib import admin |
2 | from .models import User | 2 | from .models import User |
3 | -from .forms import UserForm | 3 | +from .forms import AdminUserForm |
4 | 4 | ||
5 | class UserAdmin(admin.ModelAdmin): | 5 | class UserAdmin(admin.ModelAdmin): |
6 | list_display = ['username', 'name', 'email', 'is_staff', 'is_active'] | 6 | list_display = ['username', 'name', 'email', 'is_staff', 'is_active'] |
7 | search_fields = ['username', 'name', 'email'] | 7 | search_fields = ['username', 'name', 'email'] |
8 | - # form = UserForm | 8 | + form = AdminUserForm |
9 | 9 | ||
10 | admin.site.register(User, UserAdmin) | 10 | admin.site.register(User, UserAdmin) |
11 | \ No newline at end of file | 11 | \ No newline at end of file |
users/forms.py
@@ -26,6 +26,31 @@ class ProfileForm(forms.ModelForm): | @@ -26,6 +26,31 @@ class ProfileForm(forms.ModelForm): | ||
26 | 'password':forms.PasswordInput | 26 | 'password':forms.PasswordInput |
27 | } | 27 | } |
28 | 28 | ||
29 | +class AdminUserForm(forms.ModelForm): | ||
30 | + def save(self, commit=True): | ||
31 | + super(AdminUserForm, self).save(commit=False) | ||
32 | + | ||
33 | + self.instance.set_password(self.cleaned_data['password']) | ||
34 | + self.instance.save() | ||
35 | + | ||
36 | + if self.instance.is_staff: | ||
37 | + assign_role(self.instance, 'system_admin') | ||
38 | + elif self.instance.type_profile == 2: | ||
39 | + assign_role(self.instance, 'student') | ||
40 | + elif self.instance.type_profile == 1: | ||
41 | + assign_role(self.instance, 'professor') | ||
42 | + | ||
43 | + self.instance.save() | ||
44 | + | ||
45 | + return self.instance | ||
46 | + | ||
47 | + class Meta: | ||
48 | + model = User | ||
49 | + fields = ['username', 'name', 'email', 'password', 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active'] | ||
50 | + widgets = { | ||
51 | + 'password':forms.PasswordInput | ||
52 | + } | ||
53 | + | ||
29 | class UserForm(RegisterUserForm): | 54 | class UserForm(RegisterUserForm): |
30 | 55 | ||
31 | class Meta: | 56 | class Meta: |