Repository: fossasia/open-event-server

Issue:
Create custom forms for firstname,lastname and email for attendees upon event creation.
**Is your feature request related to a problem? Please describe.**
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->
Firstname, lastname and email address are compulsory fields for an attendee. In order to implement the custom attendee form builder we need to add custom forms for these three fields as soon as an event is created.



Discussion:

Resolving patch:
diff --git a/app/api/events.py b/app/api/events.py
--- a/app/api/events.py
+++ b/app/api/events.py
@@ -13,6 +13,7 @@
 from app.api.bootstrap import api
 from app.api.data_layers.EventCopyLayer import EventCopyLayer
 from app.api.helpers.db import save_to_db, safe_query
+from app.api.helpers.events import create_custom_forms_for_attendees
 from app.api.helpers.exceptions import ForbiddenException, ConflictException, UnprocessableEntity
 from app.api.helpers.files import create_save_image_sizes
 from app.api.helpers.permission_manager import has_access
@@ -116,9 +117,9 @@ def before_post(self, args, kwargs, data=None):
         :return:
         """
         is_verified = User.query.filter_by(id=kwargs['user_id']).first().is_verified
-        if (data.get('state', None) == 'published' and not is_verified):
+        if data.get('state', None) == 'published' and not is_verified:
             raise ForbiddenException({'source': ''},
-                                      "Only verified accounts can publish events")
+                                     "Only verified accounts can publish events")
         if data.get('state', None) == 'published' and not data.get('location_name', None):
             raise ConflictException({'pointer': '/data/attributes/location-name'},
                                     "Location is required to publish the event")
@@ -138,6 +139,10 @@ def after_create_object(self, event, data, view_kwargs):
         role_invite = RoleInvite(user.email, role.title_name, event.id, role.id, datetime.now(pytz.utc),
                                  status='accepted')
         save_to_db(role_invite, 'Organiser Role Invite Added')
+
+        # create custom forms for compulsory fields of attendee form.
+        create_custom_forms_for_attendees(event)
+
         if event.state == 'published' and event.schedule_published_on:
             start_export_tasks(event)
 
diff --git a/app/api/helpers/events.py b/app/api/helpers/events.py
new file mode 100644
--- /dev/null
+++ b/app/api/helpers/events.py
@@ -0,0 +1,30 @@
+from app.api.helpers.db import save_to_db
+from app.models.custom_form import CustomForms
+
+
+def create_custom_forms_for_attendees(event):
+    """
+    Create and save the custom forms for the required fields of attendees.
+    :param event:
+    :return:
+    """
+    # common values
+    form = 'attendee'
+    is_required = True
+    is_included = True
+    is_fixed = True
+    event_id = event.id
+    form_type = 'text'
+
+    first_name_form = CustomForms(form=form, is_required=is_required, is_included=is_included, is_fixed=is_fixed,
+                                  event_id=event_id, type=form_type, field_identifier='firstName')
+
+    last_name_form = CustomForms(form=form, is_required=is_required, is_included=is_included, is_fixed=is_fixed,
+                                 event_id=event_id, type=form_type, field_identifier='lastName')
+
+    email_form = CustomForms(form=form, is_required=is_required, is_included=is_included, is_fixed=is_fixed,
+                             event_id=event_id, type='email', field_identifier='email')
+
+    save_to_db(first_name_form, 'First name form saved')
+    save_to_db(last_name_form, 'Last name form saved')
+    save_to_db(email_form, 'Email form saved')
diff --git a/app/models/custom_form.py b/app/models/custom_form.py
--- a/app/models/custom_form.py
+++ b/app/models/custom_form.py
@@ -38,7 +38,7 @@
 ATTENDEE_FORM = {
     "firstname": {"include": 1, "require": 1},
     "lastname": {"include": 1, "require": 1},
-    "email": {"include": 1, "require": 0},
+    "email": {"include": 1, "require": 1},
     "address": {"include": 1, "require": 0},
     "city": {"include": 1, "require": 0},
     "state": {"include": 1, "require": 0},


Test patch: