Skip to content

Commit

Permalink
feat: Assign is_complex automatically to custom form (#7086)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamareebjamal authored Jun 28, 2020
1 parent 5901e1f commit 0f00132
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/api/custom_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from app.api.helpers.utilities import require_relationship
from app.api.schema.custom_forms import CustomFormSchema
from app.models import db
from app.models.custom_form import CustomForms
from app.models.custom_form import CUSTOM_FORM_IDENTIFIER_NAME_MAP, CustomForms
from app.models.event import Event


Expand All @@ -32,6 +32,12 @@ def before_post(self, args, kwargs, data):
{'parameter': 'event_id'}, "Event: {} not found".format(data['event_id'])
)

# Assign is_complex to True if not found in identifier map of form type
data['is_complex'] = (
CUSTOM_FORM_IDENTIFIER_NAME_MAP[data['form']].get(data['field_identifier'])
is None
)

schema = CustomFormSchema
methods = [
'POST',
Expand Down
2 changes: 1 addition & 1 deletion app/api/schema/custom_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Meta:
description = fields.Str(allow_none=True)
is_required = fields.Boolean(default=False)
is_included = fields.Boolean(default=False)
is_complex = fields.Boolean(default=False)
is_complex = fields.Boolean(dump_only=True)
is_fixed = fields.Boolean(default=False)
event = Relationship(
attribute='event',
Expand Down
Empty file.
81 changes: 81 additions & 0 deletions tests/all/integration/api/custom_forms/test_custom_forms_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import json

from app.models.custom_form import CustomForms
from tests.factories.event import EventFactoryBasic


def test_custom_form_create(db, client, user, jwt):
user.is_super_admin = True
event = EventFactoryBasic(owner=user)
db.session.commit()

data = json.dumps(
{
'data': {
'type': 'custom-form',
"attributes": {
"form": "attendee",
"type": "email",
"field-identifier": "email",
"name": "Email",
"is-included": True,
"is-required": True,
"is-complex": True,
},
"relationships": {
"event": {"data": {"id": str(event.id), "type": "event"}}
},
}
}
)

response = client.post(
'/v1/custom-forms',
content_type='application/vnd.api+json',
headers=jwt,
data=data,
)

assert response.status_code == 201
c_id = json.loads(response.data)['data']['id']
custom_form = CustomForms.query.get(c_id)

assert custom_form.form == 'attendee'
assert custom_form.type == 'email'
assert custom_form.field_identifier == 'email'
assert custom_form.is_complex is False

data = json.dumps(
{
'data': {
'type': 'custom-form',
"attributes": {
"form": "attendee",
"type": "number",
"field-identifier": "vortexTensor",
"name": "Vortex Tensor",
"is-included": True,
"is-required": True,
},
"relationships": {
"event": {"data": {"id": str(event.id), "type": "event"}}
},
}
}
)

response = client.post(
'/v1/custom-forms',
content_type='application/vnd.api+json',
headers=jwt,
data=data,
)

assert response.status_code == 201
c_id = json.loads(response.data)['data']['id']
custom_form = CustomForms.query.get(c_id)

assert custom_form.form == 'attendee'
assert custom_form.type == 'number'
assert custom_form.identifier == 'vortex_tensor'
assert custom_form.is_complex is True

0 comments on commit 0f00132

Please sign in to comment.