Source code for gazette.forms.user

from onegov.form import Form
from onegov.form.fields import ChosenSelectField, ChosenSelectMultipleField
from onegov.form.fields import PhoneNumberField
from onegov.form.validators import UniqueColumnValue
from onegov.gazette import _
from onegov.user import User, UserGroupCollection
from onegov.user import UserGroup
from sqlalchemy import cast
from sqlalchemy import String
from wtforms.fields import RadioField
from wtforms.fields import StringField
from wtforms.validators import Email
from wtforms.validators import InputRequired


[docs] class UserForm(Form):
[docs] role = RadioField( label=_('Role'), choices=[], default='member', validators=[ InputRequired() ] )
[docs] group = ChosenSelectField( label=_('Group'), choices=[('', '')] )
[docs] name = StringField( label=_('Name'), validators=[ InputRequired() ] )
[docs] username = StringField( label=_('E-Mail'), validators=[ InputRequired(), Email(), UniqueColumnValue(User) ] )
[docs] phone_number = PhoneNumberField( label=_('Phone number'), description='+41791112233', )
[docs] def on_request(self) -> None: self.role.choices = [] model = getattr(self, 'model', None) if self.request.is_private(model): self.role.choices = [('member', _('Editor'))] if self.request.is_secret(model): self.role.choices.append(('editor', _('Publisher'))) self.group.choices = self.request.session.query( cast(UserGroup.id, String), UserGroup.name ).all() self.group.choices.insert( 0, ('', self.request.translate(_('- none -'))) )
[docs] def update_model(self, model: User) -> None: assert self.username.data is not None model.username = self.username.data model.role = self.role.data model.realname = self.name.data model.group_id = self.group.data or None model.phone_number = self.phone_number.formatted_data
[docs] def apply_model(self, model: User) -> None: self.username.data = model.username self.role.data = model.role self.name.data = model.realname self.group.data = str(model.group_id or '') self.phone_number.data = model.phone_number
[docs] class ExportUsersForm(Form):
[docs] group_names = ChosenSelectMultipleField( label=_('Group Names'), choices=[] )
[docs] def populate_group_names(self) -> None: groups = UserGroupCollection(self.request.session) cls = groups.model_class q = groups.query().with_entities(cls.id, cls.name) self.group_names.choices = [ (str(entry.id), entry.name) for entry in q ]
[docs] def on_request(self) -> None: self.populate_group_names()