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_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 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()
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_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()