Source code for gazette.forms.organization

from onegov.form import Form
from onegov.form.fields import ChosenSelectField
from onegov.form.validators import UniqueColumnValue
from onegov.gazette import _
from onegov.gazette.models import GazetteNotice
from onegov.gazette.models import Organization
from onegov.gazette.validators import UnusedColumnKeyValue
from sqlalchemy import cast
from sqlalchemy import String
from wtforms.fields import BooleanField
from wtforms.fields import StringField
from wtforms.validators import InputRequired


[docs] class OrganizationForm(Form):
[docs] parent = ChosenSelectField( label=_('Parent Organization'), choices=[('', '')] )
[docs] title = StringField( label=_('Title'), validators=[ InputRequired() ] )
[docs] active = BooleanField( label=_('Active'), default=True )
[docs] name = StringField( label=_('ID'), description=_('Leave blank to set the value automatically.'), validators=[ UniqueColumnValue(Organization), UnusedColumnKeyValue(GazetteNotice._organizations) ] )
[docs] external_name = StringField( label=_('External ID'), )
[docs] def on_request(self) -> None: session = self.request.session query = session.query( cast(Organization.id, String), Organization.title ) query = query.filter(Organization.parent_id.is_(None)) query = query.order_by(Organization.order) self.parent.choices = query.all() self.parent.choices.insert( 0, ('', self.request.translate(_('- none -'))) )
[docs] def update_model(self, model: Organization) -> None: assert self.title.data is not None model.title = self.title.data model.active = self.active.data model.parent_id = self.parent.data or None if self.name.data: model.name = self.name.data model.external_name = self.external_name.data
[docs] def apply_model(self, model: Organization) -> None: self.title.data = model.title if model.active is not None: self.active.data = model.active self.name.data = model.name self.external_name.data = model.external_name self.parent.data = str(model.parent_id or '') if model.in_use: self.name.render_kw = {'readonly': True}