def add_custom_field_sex(ctx, meeting): # previously named add_custom_sex_field(ctx): app = ctx.obj['app'] with app.test_request_context(): meeting_obj = Meeting.query.filter_by(id=meeting).scalar() meetings = [meeting_obj] if meeting_obj else Meeting.query.all() for meeting in meetings: query = (CustomField.query.filter_by(slug='sex', meeting=meeting)) if query.scalar(): continue custom_field = CustomField() custom_field.meeting = meeting custom_field.slug = 'sex' custom_field.label = Translation(english=u'Sex') custom_field.required = False custom_field.field_type = CustomField.SELECT custom_field.is_primary = True custom_field.visible_on_registration_form = True custom_field.is_protected = True db.session.add(custom_field) _add_choice_values_for_custom_field(custom_field, Participant.SEX_CHOICES) click.echo(u'Sex field added for meeting %s \n' % meeting.id) db.session.commit()
def add_verified_flag_mp(ctx): app = ctx.obj['app'] with app.test_request_context(): for meeting in Meeting.query.all(): if meeting.settings.get('media_participant_enabled', False): if meeting.custom_fields.filter_by( custom_field_type=CustomField.MEDIA, slug='verified').count(): continue cf = CustomField(slug='verified', meeting_id=meeting.id, field_type=CustomField.CHECKBOX, is_primary=True, custom_field_type=CustomField.MEDIA) cf.label = Translation(english='Acknowledged') cf.sort = meeting.custom_fields.filter_by( custom_field_type=CustomField.MEDIA).count() + 1 db.session.add(cf) db.session.commit()
def add_participant_custom_fields(meeting): """Adds participants fields as CustomFields to meeting.""" for i, field in enumerate(ParticipantDummyForm()): custom_field = CustomField() custom_field.meeting = meeting custom_field.slug = field.name custom_field.label = Translation(english=unicode(field.label.text)) custom_field.required = field.flags.required custom_field.field_type = _CUSTOM_FIELD_MAPPER[field.type] custom_field.is_primary = True custom_field.sort = i + 1 custom_field.visible_on_registration_form = True db.session.add(custom_field) if custom_field.field_type == CustomField.SELECT: _add_choice_values_for_custom_field(custom_field, field.choices) db.session.commit()
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()
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): 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()
def add_custom_fields_for_meeting(meeting, form_class=ParticipantDummyForm): """Adds participants fields as CustomFields to meeting.""" form = form_class() for i, field in enumerate(form): query = ( CustomField.query .filter_by(slug=field.name, meeting=meeting) .filter_by(custom_field_type=form.CUSTOM_FIELD_TYPE) ) if query.scalar(): continue custom_field = CustomField() custom_field.meeting = meeting custom_field.slug = field.name custom_field.label = Translation(english=unicode(field.label.text)) custom_field.required = field.flags.required custom_field.field_type = _CUSTOM_FIELD_MAPPER[field.type] custom_field.is_primary = True custom_field.custom_field_type = form.CUSTOM_FIELD_TYPE custom_field.max_length = _extract_max_length_from_field(field) if field.name in form.meta.visible_on_registration_form: custom_field.visible_on_registration_form = True else: custom_field.visible_on_registration_form = False if field.name in form.meta.protected_fields: custom_field.is_protected = True else: custom_field.is_protected = False custom_field.sort = i + 1 db.session.add(custom_field) if custom_field.field_type in (CustomField.SELECT, CustomField.LANGUAGE): _add_choice_values_for_custom_field(custom_field, field.choices) db.session.commit()
def add_participant_custom_fields(meeting, form_class=ParticipantDummyForm): """Adds participants fields as CustomFields to meeting.""" for i, field in enumerate(form_class()): custom_field = CustomField() custom_field.meeting = meeting custom_field.slug = field.name custom_field.label = Translation(english=unicode(field.label.text)) custom_field.required = field.flags.required custom_field.field_type = _CUSTOM_FIELD_MAPPER[field.type] custom_field.custom_field_type = form_class.CUSTOM_FIELD_TYPE custom_field.is_primary = True custom_field.sort = i + 1 custom_field.visible_on_registration_form = True db.session.add(custom_field) if custom_field.field_type == CustomField.SELECT: _add_choice_values_for_custom_field(custom_field, field.choices) db.session.commit()