forms.py 1.27 KB
#-*- 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']