def _create_accompanying_persons_field(max_persons, persons_count_against_limit, registration=None, data=None, num_persons=0): section = RegistrationFormSection(registration_form=dummy_regform, title='dummy_section', is_manager_only=False) db.session.add(section) db.session.flush() field = RegistrationFormField(input_type='accompanying_persons', title='Field', parent=section, registration_form=dummy_regform) field.field_impl.form_item.data = { 'max_persons': max_persons, 'persons_count_against_limit': persons_count_against_limit, } field.versioned_data = field.field_impl.form_item.data if registration: registration.data.append( RegistrationData( field_data=field.current_data, data=(data if data is not None else _create_accompanying_persons(num_persons)))) db.session.flush() return field
def multi_choice_field(): field = RegistrationFormField(input_type='multi_choice') field.versioned_data = { 'choices': [ {'id': _id(1), 'places_limit': 0, 'is_billable': False, 'price': 0}, {'id': _id(2), 'places_limit': 0, 'is_billable': False, 'price': 0}, {'id': _id(3), 'places_limit': 0, 'is_billable': True, 'price': 10} ] } return field
def _process(self): field_data = snakify_keys(request.json['fieldData']) form_field = RegistrationFormField(parent_id=self.section.id, registration_form=self.regform) _fill_form_field_with_data(form_field, field_data) db.session.add(form_field) db.session.flush() return jsonify(view_data=form_field.view_data)
def _create_form(self): create_personal_data_fields(self.regform) for item in self.regform.form_items: if not item.is_field: item.position = 1 # pd section continue # we have nothing but personal data fields right now. no need for extra checks! if item.personal_data_type != PersonalDataType.country: self.pd_field_map[item.personal_data_type] = item if item.personal_data_type == PersonalDataType.title: self.title_map = { v: k for k, v in item.data['captions'].iteritems() } # create administrative section for statuses if self.status_used: section = RegistrationFormSection(registration_form=self.regform, is_manager_only=True, title='Status', position=2) if self.status_used: choices = [] for status in ('refused', 'excused', 'invited', 'accepted', 'rejected', 'declined'): uuid = unicode(uuid4()) caption = status.title() choices.append({ 'price': 0, 'is_billable': False, 'places_limit': 0, 'is_enabled': True, 'caption': caption, 'id': uuid }) self.status_map[status] = { 'uuid': uuid, 'caption': caption } field_data = { 'item_type': 'dropdown', 'with_extra_slots': False, 'default_item': None, 'choices': choices } self.status_field = field = RegistrationFormField( registration_form=self.regform, parent=section, input_type='single_choice', title='Status') field.data, field.versioned_data = field.field_impl.process_field_data( field_data)
def test_modify_registration(monkeypatch, dummy_event, dummy_user, dummy_regform): session.set_session_user(dummy_user) monkeypatch.setattr('indico.modules.users.util.get_user_by_email', lambda *args, **kwargs: dummy_user) # Extend the dummy_regform with more sections and fields user_section = RegistrationFormSection(registration_form=dummy_regform, title='dummy_section', is_manager_only=False) db.session.add(user_section) db.session.flush() boolean_field = RegistrationFormField(parent_id=user_section.id, registration_form=dummy_regform) _fill_form_field_with_data(boolean_field, { 'input_type': 'bool', 'default_value': False, 'title': 'Yes/No' }) db.session.add(boolean_field) multi_choice_field = RegistrationFormField(parent_id=user_section.id, registration_form=dummy_regform) _fill_form_field_with_data( multi_choice_field, { 'input_type': 'multi_choice', 'with_extra_slots': False, 'title': 'Multi Choice', 'choices': [ { 'caption': 'A', 'id': 'new:test1', 'is_enabled': True }, { 'caption': 'B', 'id': 'new:test2', 'is_enabled': True }, ] }) choice_uuid = next(k for k, v in multi_choice_field.data['captions'].items() if v == 'A') db.session.add(multi_choice_field) db.session.flush() # Add a manager-only section management_section = RegistrationFormSection( registration_form=dummy_regform, title='manager_section', is_manager_only=True) db.session.add(management_section) db.session.flush() checkbox_field = RegistrationFormField(parent_id=management_section.id, registration_form=dummy_regform) _fill_form_field_with_data(checkbox_field, { 'input_type': 'checkbox', 'is_required': True, 'title': 'Checkbox' }) db.session.add(checkbox_field) db.session.flush() # Create a registration data = { boolean_field.html_field_name: True, multi_choice_field.html_field_name: { choice_uuid: 2 }, checkbox_field.html_field_name: True, 'email': dummy_user.email, 'first_name': dummy_user.first_name, 'last_name': dummy_user.last_name } reg = create_registration(dummy_regform, data, invitation=None, management=True, notify_user=False) assert reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 2} assert reg.data_by_field[checkbox_field.id].data # Modify the registration data = { boolean_field.html_field_name: True, multi_choice_field.html_field_name: { choice_uuid: 1 }, checkbox_field.html_field_name: False, } modify_registration(reg, data, management=False, notify_user=False) assert reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 1} # Assert that the manager field is not changed assert reg.data_by_field[checkbox_field.id].data # Modify as a manager data = { multi_choice_field.html_field_name: { choice_uuid: 3 }, checkbox_field.html_field_name: False, } modify_registration(reg, data, management=True, notify_user=False) assert reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 3} assert not reg.data_by_field[checkbox_field.id].data # Add a new field after registering new_multi_choice_field = RegistrationFormField( parent_id=user_section.id, registration_form=dummy_regform) _fill_form_field_with_data( new_multi_choice_field, { 'input_type': 'multi_choice', 'with_extra_slots': False, 'title': 'Multi Choice', 'choices': [ { 'caption': 'A', 'id': 'new:test3', 'is_enabled': True }, ] }) db.session.add(new_multi_choice_field) db.session.flush() modify_registration(reg, {}, management=False, notify_user=False) assert reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 3} assert not reg.data_by_field[checkbox_field.id].data # Assert that the new field got a default value assert reg.data_by_field[new_multi_choice_field.id].data == {} # Remove a field after registering multi_choice_field.is_deleted = True db.session.flush() data = { multi_choice_field.html_field_name: { choice_uuid: 7 }, } modify_registration(reg, data, management=True, notify_user=False) assert reg.data_by_field[boolean_field.id].data # Assert that the removed field keeps its old value assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 3} assert not reg.data_by_field[checkbox_field.id].data assert reg.data_by_field[new_multi_choice_field.id].data == {}
def test_create_registration(monkeypatch, dummy_event, dummy_user, dummy_regform): monkeypatch.setattr('indico.modules.users.util.get_user_by_email', lambda *args, **kwargs: dummy_user) # Extend the dummy_regform with more sections and fields section = RegistrationFormSection(registration_form=dummy_regform, title='dummy_section', is_manager_only=False) db.session.add(section) db.session.flush() boolean_field = RegistrationFormField(parent_id=section.id, registration_form=dummy_regform) _fill_form_field_with_data(boolean_field, { 'input_type': 'bool', 'default_value': False, 'title': 'Yes/No' }) db.session.add(boolean_field) multi_choice_field = RegistrationFormField(parent_id=section.id, registration_form=dummy_regform) _fill_form_field_with_data( multi_choice_field, { 'input_type': 'multi_choice', 'with_extra_slots': False, 'title': 'Multi Choice', 'choices': [ { 'caption': 'A', 'id': 'new:test1', 'is_enabled': True }, { 'caption': 'B', 'id': 'new:test2', 'is_enabled': True }, ] }) db.session.add(multi_choice_field) db.session.flush() data = { boolean_field.html_field_name: True, multi_choice_field.html_field_name: { 'test1': 2 }, 'email': dummy_user.email, 'first_name': dummy_user.first_name, 'last_name': dummy_user.last_name } reg = create_registration(dummy_regform, data, invitation=None, management=False, notify_user=False) assert reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {'test1': 2} db.session.delete(reg) db.session.flush() # Make sure that missing data gets default values: data = { 'email': dummy_user.email, 'first_name': dummy_user.first_name, 'last_name': dummy_user.last_name } reg = create_registration(dummy_regform, data, invitation=None, management=False, notify_user=False) assert not reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {} db.session.delete(reg) db.session.flush() # Add a manager only section section = RegistrationFormSection(registration_form=dummy_regform, title='manager_section', is_manager_only=True) db.session.add(section) db.session.flush() checkbox_field = RegistrationFormField(parent_id=section.id, registration_form=dummy_regform) _fill_form_field_with_data(checkbox_field, { 'input_type': 'checkbox', 'title': 'Checkbox' }) db.session.add(checkbox_field) db.session.flush() data = { checkbox_field.html_field_name: True, 'email': dummy_user.email, 'first_name': dummy_user.first_name, 'last_name': dummy_user.last_name } reg = create_registration(dummy_regform, data, invitation=None, management=False, notify_user=False) assert not reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {} # Assert that the manager field gets the default value, not the value sent assert not reg.data_by_field[checkbox_field.id].data db.session.delete(reg) db.session.flush() # Try again with management=True data = { checkbox_field.html_field_name: True, 'email': dummy_user.email, 'first_name': dummy_user.first_name, 'last_name': dummy_user.last_name } reg = create_registration(dummy_regform, data, invitation=None, management=True, notify_user=False) assert not reg.data_by_field[boolean_field.id].data assert reg.data_by_field[multi_choice_field.id].data == {} # Assert that the manager field gets properly set with management=True assert reg.data_by_field[checkbox_field.id].data