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]
username = StringField(
label=_('E-Mail'),
validators=[
InputRequired(),
Email(),
UniqueColumnValue(User)
]
)
[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