Repository: fossasia/open-event-server

Issue:
Unable to edit some events, 400 error
This is because when event sub_topic field is set as None (null) , then the request form object (request.form) doesn't contain that key . Thus a KeyError happens and this results in 400 error. 

This behavior can be seen in imported samples (https://github.com/fossasia/open-event/tree/master/sample) which don't have event sub_topic field set. 



Discussion:

Resolving patch:
diff --git a/app/api/helpers/custom_fields.py b/app/api/helpers/custom_fields.py
--- a/app/api/helpers/custom_fields.py
+++ b/app/api/helpers/custom_fields.py
@@ -6,7 +6,7 @@
 
 
 EMAIL_REGEX = re.compile(r'\S+@\S+\.\S+')
-URI_REGEX = re.compile(r'(http|https|ftp)://\S*(\S+\.|localhost/)\S+')
+URI_REGEX = re.compile(r'(http|https|ftp)://\S*(\S+\.|localhost(\:\d+)?/)\S+')
 
 
 class CustomField(Raw):
diff --git a/app/helpers/data.py b/app/helpers/data.py
--- a/app/helpers/data.py
+++ b/app/helpers/data.py
@@ -1439,10 +1439,10 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
         event.location_name = form['location_name']
         event.description = form['description']
         event.event_url = form['event_url']
-        event.type = form['type']
-        event.topic = form['topic']
+        event.type = form.get('type')
+        event.topic = form.get('topic')
         event.show_map = 1 if form.get('show_map') == 'on' else 0
-        event.sub_topic = form['sub_topic']
+        event.sub_topic = form.get('sub_topic')
         event.privacy = form.get('privacy', 'public')
         event.payment_country = form.get('payment_country')
 
diff --git a/app/views/admin/models_views/events.py b/app/views/admin/models_views/events.py
--- a/app/views/admin/models_views/events.py
+++ b/app/views/admin/models_views/events.py
@@ -1,6 +1,7 @@
 import json
 import datetime
 import os
+import traceback
 import binascii
 from uuid import uuid4
 
@@ -294,7 +295,6 @@ def edit_view(self, event_id):
     @expose('/<event_id>/edit/<step>', methods=('GET', 'POST'))
     @can_access
     def edit_view_stepped(self, event_id, step):
-
         event = DataGetter.get_event(event_id)
         session_types = DataGetter.get_session_types_by_event_id(event_id).all(
         )
@@ -367,10 +367,13 @@ def edit_view_stepped(self, event_id, step):
                 old_sponsor_logos.append(sponsor.logo)
                 old_sponsor_names.append(sponsor.name)
 
-            event = DataManager.edit_event(
-                request, event_id, event, session_types, tracks, social_links,
-                microlocations, call_for_speakers, sponsors, custom_forms, img_files, old_sponsor_logos,
-                old_sponsor_names, tax)
+            try:
+                event = DataManager.edit_event(
+                    request, event_id, event, session_types, tracks, social_links,
+                    microlocations, call_for_speakers, sponsors, custom_forms, img_files, old_sponsor_logos,
+                    old_sponsor_names, tax)
+            except Exception:
+                traceback.print_exc()
 
             if (request.form.get('state',
                                  u'Draft') == u'Published') and string_empty(


Test patch: