Repository: fossasia/open-event-server

Issue:
Optimise approach for loading custom placeholders
The current apprach has to loop across the `custom_placeholders` dict everytime to get a placeholder image.


Discussion:

Resolving patch:
diff --git a/app/__init__.py b/app/__init__.py
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -281,6 +281,12 @@ def app_logo():
     return dict(logo=logo)
 
 
+@app.context_processor
+def app_avatar():
+    avatar = DataGetter.get_custom_placeholder_by_name('Avatar')
+    return dict(avatar=avatar)
+
+
 @app.template_filter('pretty_name')
 def pretty_name_filter(string):
     string = str(string)
diff --git a/app/views/public/event_detail.py b/app/views/public/event_detail.py
--- a/app/views/public/event_detail.py
+++ b/app/views/public/event_detail.py
@@ -45,7 +45,13 @@ def display_default():
 def display_event_detail_home(identifier):
     event = get_published_event_or_abort(identifier)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
+
     call_for_speakers = DataGetter.get_call_for_papers(event.id).first()
     accepted_sessions = DataGetter.get_sessions(event.id).all()
     if event.copyright:
@@ -71,7 +77,12 @@ def display_event_detail_home(identifier):
 def display_event_sessions(identifier):
     event = get_published_event_or_abort(identifier)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
     if not event.has_session_speakers:
         abort(404)
     call_for_speakers = DataGetter.get_call_for_papers(event.id).first()
@@ -87,7 +98,12 @@ def display_event_sessions(identifier):
 def display_event_schedule(identifier):
     event = get_published_event_or_abort(identifier)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
     if not event.has_session_speakers:
         abort(404)
     tracks = DataGetter.get_tracks(event.id)
@@ -143,7 +159,12 @@ def display_event_schedule_xcal(identifier):
 def display_event_cfs(identifier, via_hash=False):
     event = get_published_event_or_abort(identifier)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
     if not event.has_session_speakers:
         abort(404)
 
@@ -179,7 +200,12 @@ def display_event_cfs_via_hash(hash):
         abort(404)
     event = DataGetter.get_event(call_for_speakers.event_id)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
     if not event.has_session_speakers:
         abort(404)
 
@@ -214,7 +240,12 @@ def process_event_cfs(identifier, via_hash=False):
     if request.method == 'GET':
         event = get_published_event_or_abort(identifier)
         placeholder_images = DataGetter.get_event_default_images()
-        custom_placeholder = DataGetter.get_custom_placeholders()
+        if event.sub_topic:
+            custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+        elif event.topic:
+            custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+        else:
+            custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
         if not event.has_session_speakers:
             abort(404)
 
@@ -262,7 +293,12 @@ def process_event_cfs(identifier, via_hash=False):
 def display_event_coc(identifier):
     event = get_published_event_or_abort(identifier)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
     call_for_speakers = DataGetter.get_call_for_papers(event.id).first()
     if not (event.code_of_conduct and event.code_of_conduct != '' and event.code_of_conduct != ' '):
         abort(404)
@@ -278,7 +314,12 @@ def display_event_coc(identifier):
 def display_event_tickets(identifier):
     event = get_published_event_or_abort(identifier)
     placeholder_images = DataGetter.get_event_default_images()
-    custom_placeholder = DataGetter.get_custom_placeholders()
+    if event.sub_topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.sub_topic)
+    elif event.topic:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name(event.topic)
+    else:
+        custom_placeholder = DataGetter.get_custom_placeholder_by_name('Other')
     if event.copyright:
         licence_details = DataGetter.get_licence_details(event.copyright.licence)
     else:


Test patch: