forms.py
1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#-*- coding:utf-8 -*-
import random
import string
from django.forms import *
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.models import User
from accounts.models import UserSettings
class AddUserForm(ModelForm):
def save(self, commit=True, request=None):
user = super(AddUserForm, self).save(commit=False)
password = ''.join(random.choice(string.printable) for i in range(32))
user.set_password(password)
if commit:
user.save()
fakeform = PasswordResetForm(data={'email': user.email})
if fakeform.is_valid(): # oczywiście, że jest
fakeform.save(request=request)
UserSettings.objects.create(user=user)
user.groups.add(*self.cleaned_data['groups'])
return user
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'groups']
class SettingsForm(ModelForm):
def __init__(self, vocabularies=None, **kwargs):
super(SettingsForm, self).__init__(**kwargs)
if vocabularies:
self.fields['default_owner'].queryset = vocabularies
else:
self.fields['default_owner'].widget = HiddenInput()
class Meta:
model = UserSettings
fields = ['incremental_search', 'filter_search', 'default_owner']