Repository: fossasia/open-event-server

Issue:
Sponsored events with images not getting copied
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
The image resizing task needs to be turned into a celery task to avoid server block

**To Reproduce**
Steps to reproduce the behavior:
1. Try copying an event with Sponsor images
2. See error

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
The `create_save_resized_image` function shouldn't jam the server

**Additional details (please complete the following information):**
 - OS: [e.g. MacOS, Ubuntu, CentOS] Ubuntu 16.04 LTS
 - Python Version [e.g. `3.5`, `3.6`] 3.6

**Additional context**
<!-- Add any other context about the problem here. -->
Looking into it


Discussion:

Resolving patch:
diff --git a/app/api/event_copy.py b/app/api/event_copy.py
--- a/app/api/event_copy.py
+++ b/app/api/event_copy.py
@@ -19,6 +19,10 @@
 event_copy = Blueprint('event_copy', __name__, url_prefix='/v1/events')
 
 
+def start_sponsor_logo_generation_task(event_id):
+    from .helpers.tasks import sponsor_logos_url_task
+    sponsor_logos_url_task.delay(event_id=event_id)
+
 @event_copy.route('/<identifier>/copy', methods=['POST'])
 def create_event_copy(identifier):
     id = 'identifier'
@@ -70,11 +74,11 @@ def create_event_copy(identifier):
         db.session.expunge(sponsor)  # expunge the object from session
         make_transient(sponsor)
         sponsor.event_id = event.id
-        logo_url = create_save_resized_image(image_file=sponsor.logo_url, resize=False)
         delattr(sponsor, 'id')
-        sponsor.logo_url = logo_url
         save_to_db(sponsor)
 
+    start_sponsor_logo_generation_task(event.id)
+
     for location in microlocations:
         location_id = location.id
         db.session.expunge(location)  # expunge the object from session
diff --git a/app/api/helpers/tasks.py b/app/api/helpers/tasks.py
--- a/app/api/helpers/tasks.py
+++ b/app/api/helpers/tasks.py
@@ -19,7 +19,7 @@
 import logging
 import traceback
 
-from app.api.helpers.files import create_save_image_sizes
+from app.api.helpers.files import create_save_image_sizes, create_save_resized_image
 from app.api.helpers.request_context_task import RequestContextTask
 from app.api.helpers.mail import send_export_mail, send_import_mail
 from app.api.helpers.notification import send_notif_after_import, send_notif_after_export
@@ -31,6 +31,7 @@
 from app.api.imports import import_event_task_base
 from app.models.event import Event
 from app.models.order import Order
+from app.models.sponsor import Sponsor
 from app.models.discount_code import DiscountCode
 from app.models.ticket_holder import TicketHolder
 from app.api.helpers.ICalExporter import ICalExporter
@@ -120,6 +121,20 @@ def resize_user_images_task(self, user_id, original_image_url):
         logging.exception('Error encountered while generating resized images for user with id: {}'.format(user_id))
 
 
+@celery.task(base=RequestContextTask, name='sponsor.logo.urls', bind=True)
+def sponsor_logos_url_task(self, event_id):
+    sponsors = Sponsor.query.filter_by(event_id=event_id, deleted_at=None).all()
+    for sponsor in sponsors:
+        try:
+            logging.info('Sponsor logo url generation task started {}'.format(sponsor.logo_url))
+            new_logo_url = create_save_resized_image(image_file=sponsor.logo_url, resize=False)
+            sponsor.logo_url = new_logo_url
+            save_to_db(sponsor)
+            logging.info('Sponsor logo url successfully generated')
+        except(urllib.error.HTTPError, urllib.error.URLError):
+            logging.exception('Error encountered while logo generation')
+
+
 @celery.task(base=RequestContextTask, name='resize.speaker.images', bind=True)
 def resize_speaker_images_task(self, speaker_id, photo_url):
     speaker = safe_query(db, Speaker, 'id', speaker_id, 'speaker_id')


Test patch: