class LogRowDatasetForm(Form): class Meta(object): model = OSIRISLogDataset all_fields_optional = True ut_start_time = TimeField() number = HiddenField() spectrograph = ModelFormField(LogRowSpecForm) imager = ModelFormField(LogRowImagerForm)
class CategoryDefaultEditForm(CategoryEditBaseForm): title = ModelFormField(DefaultCategoryTitleInputForm, label='Title') meeting_type_slugs = fields.SelectMultipleField('Meeting types') def __init__(self, *args, **kwargs): super(CategoryDefaultEditForm, self).__init__(*args, **kwargs) self.meeting_type_slugs.choices = [ (m.slug, m.label) for m in MeetingType.query.ignore_def() ] if self.obj and self.meeting_type_slugs.data is None: self.meeting_type_slugs.data = [ m.slug for m in self.obj.meeting_types ] class Meta: model = CategoryDefault def save(self): category = super(CategoryDefaultEditForm, self).save() category.meeting_types = MeetingType.query.filter( MeetingType.slug.in_(self.meeting_type_slugs.data)).all() db.session.commit() return category
class AdminCustomFieldEditForm(CustomFieldEditForm): label = ModelFormField(AdminCustomFieldLabelInputForm, label='Field label') meeting_type_slugs = fields.SelectMultipleField('Meeting types') def __init__(self, *args, **kwargs): super(AdminCustomFieldEditForm, self).__init__(*args, **kwargs) self.meeting_type_slugs.choices = [ (m.slug, m.label) for m in MeetingType.query.ignore_def() ] if self.obj and self.meeting_type_slugs.data is None: self.meeting_type_slugs.data = [ m.slug for m in self.obj.meeting_types ] def save(self): custom_field = self.obj or CustomField() self.populate_obj(custom_field) custom_field.meeting_types = MeetingType.query.filter( MeetingType.slug.in_(self.meeting_type_slugs.data)).all() db.session.commit() if not custom_field.id: last_sort = (CustomField.query.with_entities( CustomField.sort).order_by(desc(CustomField.sort)).first()) if last_sort: custom_field.sort = last_sort[0] + 1 db.session.add(custom_field) db.session.commit()
class StaffEditForm(BaseForm): class Meta: model = Staff user = ModelFormField(UserForm) def save(self): staff = self.obj or Staff() # do not use populate_obj here, session.add will not work staff.full_name = self.full_name.data staff.title = self.title.data email = self.user.email.data if staff.user is None: try: staff.user = User.query.filter_by(email=email).one() except NoResultFound: staff.user = User(email=email, recover_token=str(uuid4()), recover_time=datetime.now()) send_activation_mail(staff.user.email, staff.user.recover_token) else: staff.user.email = email if self.user.is_superuser: staff.user.is_superuser = self.user.is_superuser.data if staff.id is None: db.session.add(staff) db.session.commit() return staff
class PhraseEditBaseForm(BaseForm): description = ModelFormField(DescriptionInputForm, label='Description') def save(self): phrase = self.obj or self.meta.model() self.populate_obj(phrase) if phrase.id is None: db.session.add(phrase) db.session.commit()
class GradingScaleForm(ModelFlaskForm): class Meta: model = GradingScale field_args = { 'name': { 'label': 'Name of Custom Scale', } } increments = FieldList(ModelFormField(GradeIncrementForm))
class InstitutionForm(ModelForm): class Meta: model = Institution chemical = ModelFormField(ChemicalForm) cell_line = ModelFormField(CellLineField) endpoint = ModelFormField(EndpointForm) medium = ModelFormField(MediumForm) solvent = ModelFormField(SolventForm) experimenter = ModelFormField(PersonForm) corresponding_author = ModelFormField(PersonForm)
class BirthdayForm(ModelForm): class Meta: model = BirthdayRegistry month = form.SelectField(validators=[validator.DataRequired()], coerce=int, choices=MONTH_CHOICES) date = form.SelectField(validators=[validator.DataRequired()], coerce=int, choices=DATE_CHOICES) year = form.SelectField(validators=[validator.DataRequired()], coerce=int, choices=YEAR_CHOICES) address = ModelFormField(DeliveryForm)
class CategoryEditForm(CategoryEditBaseForm): title = ModelFormField(CategoryTitleInputForm, label='Title') class Meta: model = Category def save(self): category = super(CategoryEditForm, self).save() for participant in category.participants: participant.set_representing() db.session.commit() return category
class WeddingRegistryForm(ModelForm): class Meta: model = WeddingRegistry email = form.StringField(validators=[ validator.Optional(), validator.Email(), validator.Length(max=50) ], render_kw={"placeholder": "Email address"}) month = form.SelectField(validators=[validator.DataRequired()], coerce=int, choices=MONTH_CHOICES) date = form.SelectField(validators=[validator.DataRequired()], coerce=int, choices=DATE_CHOICES) year = form.SelectField(validators=[validator.DataRequired()], coerce=int, choices=YEAR_CHOICES) address = ModelFormField(DeliveryForm)
class CustomFieldEditForm(BaseForm): label = ModelFormField(CustomFieldLabelInputForm, label='Field label') hint = ModelFormField(TranslationInputForm, label='Hint') custom_field_choices = SelectMultipleFieldWithoutValidation('Choices', choices=[], coerce=unicode) photo_size = CustomSelectField( 'Photo Size', default="", choices=CustomField.PHOTO_SIZE_CHOICES, coerce=lambda _value: getattr(_value, 'code', unicode(_value)), description=( "If a photo size is selected, the participant will be " "required to crop the image to the corresponding aspect ratio " "while registering. (width x height)")) class Meta: model = CustomField def __init__(self, *args, **kwargs): custom_field_type = kwargs.pop('custom_field_type', None) super(CustomFieldEditForm, self).__init__(*args, **kwargs) self.hint.english.validators = [] delattr(self.hint.english.flags, 'required') excluded_types = [ CustomField.SELECT, CustomField.CATEGORY, CustomField.LANGUAGE ] if custom_field_type == CustomField.MEDIA: excluded_types.append(CustomField.EVENT) self.field_type.choices = [ i for i in self.field_type.choices if i[0] not in excluded_types ] if custom_field_type: self.custom_field_type.data = custom_field_type setattr(self.label.form, 'custom_field_type', custom_field_type) if self.field_type.data == CustomField.EVENT: self.required.data = False if (self.field_type.data in (CustomField.MULTI_CHECKBOX, CustomField.RADIO) and self.obj): self.custom_field_choices.choices = ( CustomFieldChoice.query.filter_by( custom_field=self.obj).join(Translation).with_entities( Translation.english, Translation.english)) count = CustomFieldValue.query.filter_by( custom_field=self.obj).count() if count: self.custom_field_choices.flags.disabled = True if request.method == 'GET': self.custom_field_choices.data = [ i[0] for i in self.custom_field_choices.choices ] def save(self): cf = self.obj or CustomField() self.populate_obj(cf) cf.meeting = g.meeting is_choice_field_disabled = self.custom_field_choices.flags.disabled if (cf.field_type in (CustomField.MULTI_CHECKBOX, CustomField.RADIO) and not is_choice_field_disabled): CustomFieldChoice.query.filter_by(custom_field_id=cf.id).delete() for choice in self.custom_field_choices.data: cf_choice = CustomFieldChoice(custom_field=cf) cf_choice.value = Translation(english=choice) db.session.add(cf_choice) if not cf.id: last_sort = (CustomField.query.filter_by( meeting=g.meeting).with_entities(CustomField.sort).order_by( desc(CustomField.sort)).first()) if last_sort: cf.sort = last_sort[0] + 1 db.session.add(cf) db.session.commit()
class EventForm(ModelForm): class Meta: model = self.Event unknown_field = ModelFormField(self.LocationForm)
class EventForm(ModelForm): class Meta: model = self.Event location = ModelFormField(LocationForm)
class ViewForm(ModelForm, TranslatedForm): class Meta: model = View columns = ModelFieldList(FormField(ViewColumnForm)) groupers = ModelFieldList(FormField(ViewGrouperForm)) sorters = ModelFieldList(FormField(ViewSorterForm)) columns_choices = None filters = ModelFormField(ViewFiltersForm) def set_columns(self, columns): for column in columns: self.columns.append_entry(column) self.columns[-1].column.choices = copy.copy(self.columns_choices) if ('', '') in self.columns[-1].column.choices: self.columns[-1].column.choices.remove(('', '')) def set_groupers(self, groupers): for grouper in groupers: self.groupers.append_entry(grouper) self.groupers[-1].column.choices = self.columns_choices def set_sorters(self, sorters): for sorter in sorters: self.sorters.append_entry(sorter) self.sorters[-1].column.choices = self.columns_choices def get_view(self): view = View() view.title = self.title.data view.link_name = self.link_name.data view.datasource = self.datasource.data view.buttontext = self.buttontext.data view.reload_intervall = self.reload_intervall.data view.layout_number_columns = self.layout_number_columns.data return view def set_columns_choices(self, choices, update=False): self.columns_choices = choices if update: for column_form in self.columns: column_form.column.choices = choices for sorter_form in self.sorters: sorter_form.column.choices = choices for grouper_form in self.groupers: grouper_form.column.choices = choices def get_columns(self, view_id): columns = [] for column_form in self.columns: column = ViewColumn() column.column = column_form.column.data column.parent_id = view_id columns.append(column) return columns def get_groupers(self, view_id): groupers = [] for grouper_form in self.groupers: grouper = ViewGrouper() grouper.column = grouper_form.column.data grouper.parent_id = view_id groupers.append(grouper) return groupers def get_sorters(self, view_id): sorters = [] for sorter_form in self.sorters: sorter = ViewSorter() sorter.column = sorter_form.column.data sorter.sorter_option = sorter_form.sorter_option.data sorter.parent_id = view_id sorters.append(sorter) return sorters def get_filters(self): return self.filters.get_filters()
class ServerForm(ModelForm): class Meta: model = Server status = QuerySelectField(query_factory=status) provider_account = ModelFormField(ProviderAccountForm)
class MeetingEditForm(BaseForm): class Meta: model = Meeting field_args = { 'venue_address': { 'widget': widgets.TextArea() }, 'date_start': { 'format': '%d.%m.%Y', }, 'date_end': { 'format': '%d.%m.%Y', } } title = ModelFormField(TranslationInputForm, label='Description') badge_header = ModelFormField(TranslationInputForm, label='Badge header') venue_city = ModelFormField(TranslationInputForm, label='City') meeting_type_slug = fields.SelectField('Meeting Type') photo_field_id = fields.SelectField('Photo Field', coerce=int) address_field_id = fields.SelectField('Address Field', coerce=int) telephone_field_id = fields.SelectField('Telephone Field', coerce=int) media_photo_field_id = fields.SelectField('Media Photo Field', coerce=int) settings = MeetingSettingsField('Settings', choices=MEETING_SETTINGS) def __init__(self, *args, **kwargs): super(MeetingEditForm, self).__init__(*args, **kwargs) self.badge_header.english.validators = [] delattr(self.badge_header.english.flags, 'required') self.meeting_type_slug.choices = [ (mt.slug, mt.label) for mt in MeetingType.query.ignore_def()] self.photo_field_id.choices = [(0, '-----')] self.address_field_id.choices = [(0, '-----')] self.telephone_field_id.choices = [(0, '-----')] self.media_photo_field_id.choices = [(0, '-----')] if self.obj: image_query = self.obj.custom_fields.filter_by( field_type=CustomField.IMAGE) participant_query = image_query.filter_by( custom_field_type=CustomField.PARTICIPANT) image_fields = [(x.id, x.label) for x in participant_query] self.photo_field_id.choices += image_fields media_query = image_query.filter_by( custom_field_type=CustomField.MEDIA) image_fields = [(x.id, x.label) for x in media_query] self.media_photo_field_id.choices += image_fields text_query = (self.obj.custom_fields .filter_by(is_primary=False, is_protected=False) .filter( CustomField.field_type.in_( [CustomField.TEXT, CustomField.TEXT_AREA]) ) ) text_fields = [(x.id, x.label) for x in text_query] self.address_field_id.choices += text_fields self.telephone_field_id.choices += text_fields def validate_settings(self, field): settings = dict(MEETING_SETTINGS) for key in field.data: if key not in settings: raise ValidationError("Setting doesn't exist") def _clean_badge_header(self, meeting): if not self.badge_header.data['english']: old_badge_header, meeting.badge_header = meeting.badge_header, None if old_badge_header.id: db.session.delete(old_badge_header) def _save_phrases(self, meeting): phrases_default = ( PhraseDefault.query .filter_by(meeting_type_slug=meeting.meeting_type_slug) ) for phrase_default in phrases_default: phrase = copy_attributes(Phrase(), phrase_default) phrase.description = ( copy_attributes(Translation(), phrase_default.description) if phrase_default.description else Translation(english='')) phrase.meeting = meeting db.session.add(phrase) db.session.flush() def _save_categories(self, meeting): for category_default in meeting.meeting_type.default_categories: category = copy_attributes(Category(), category_default) category.title = copy_attributes(Translation(), category_default.title) category.tags = category_default.tags category.meeting = meeting db.session.add(category) db.session.flush() def _save_custom_field(self, meeting): add_custom_fields_for_meeting(meeting, form_class=ParticipantDummyForm) query = ( CustomField.query.filter_by(meeting=meeting) .with_entities(CustomField.sort) .order_by(desc(CustomField.sort)) .first()) last_sort = query[0] + 1 # Copy default custom fields for meeting type for field_default in meeting.meeting_type.default_fields: field = copy_attributes(CustomField(), field_default) field.label = copy_attributes(Translation(), field_default.label) field.sort = last_sort last_sort += 1 field.meeting = meeting db.session.add(field) db.session.flush() def save(self): meeting = self.obj or Meeting() # Store meetings settings to prevent overwriting them initial_settings = {k: v for k, v in (meeting.settings or {}).items() if k not in dict(MEETING_SETTINGS)} self.populate_obj(meeting) meeting.settings.update(initial_settings) meeting.photo_field_id = meeting.photo_field_id or None meeting.address_field_id = meeting.address_field_id or None meeting.telephone_field_id = meeting.telephone_field_id or None meeting.media_photo_field_id = meeting.media_photo_field_id or None self._clean_badge_header(meeting) if meeting.id is None: meeting.owner = current_user.staff db.session.add(meeting) self._save_phrases(meeting) self._save_categories(meeting) self._save_custom_field(meeting) if meeting.media_participant_enabled: add_custom_fields_for_meeting( meeting, form_class=MediaParticipantDummyForm) db.session.commit() return meeting
class LocationForm(ModelForm): class Meta: model = self.Location address = ModelFormField(AddressForm)
class AddSampleForm(Form): name = ModelFormField(AddCountryForm) name = ModelFormField(AddCityForm) index = ModelFormField(AddAddressForm)
def __init__(self, model, *args, **kwargs): model_form = model_form_factory(RelationshipFormMixin) form_cls = form_class_factory(model, model_form, include_primary_keys=True) super().__init__(ModelFormField(form_cls), *args, **kwargs)