Repository: fossasia/open-event-server

Issue:
Make resizing of user images a celery task
it may time out as it is being handled synchronously right now.


Discussion:
@CosmicCoder96 Can i work on this issue please ?
Go ahead 

Resolving patch:
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
@@ -97,6 +97,23 @@ def resize_event_images_task(self, event_id, original_image_url):
         logging.exception('Error encountered while generating resized images for event with id: {}'.format(event_id))
 
 
+@celery.task(base=RequestContextTask, name='resize.user.images', bind=True)
+def resize_user_images_task(self, user_id, original_image_url):
+    user = safe_query(db, User, 'id', user_id, 'user_id')
+    try:
+        logging.info('User image resizing tasks started {}'.format(original_image_url))
+        uploaded_images = create_save_image_sizes(original_image_url, 'speaker-image', user.id)
+        user.original_image_url = uploaded_images['original_image_url']
+        user.avatar_url = uploaded_images['original_image_url']
+        user.small_image_url = uploaded_images['thumbnail_image_url']
+        user.thumbnail_image_url = uploaded_images['thumbnail_image_url']
+        user.icon_image_url = uploaded_images['icon_image_url']
+        save_to_db(user)
+        logging.info('Resized images saved successfully for user with id: {}'.format(user_id))
+    except (urllib.error.HTTPError, urllib.error.URLError):
+        logging.exception('Error encountered while generating resized images for user with id: {}'.format(user_id))
+
+
 @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')
diff --git a/app/api/schema/users.py b/app/api/schema/users.py
--- a/app/api/schema/users.py
+++ b/app/api/schema/users.py
@@ -26,7 +26,7 @@ class Meta:
     avatar_url = fields.Url(allow_none=True)
     first_name = fields.Str(allow_none=True)
     last_name = fields.Str(allow_none=True)
-    original_image_url = fields.Url(allow_none=True)
+    original_image_url = fields.Url(dump_only=True, allow_none=True)
     thumbnail_image_url = fields.Url(dump_only=True, allow_none=True)
     small_image_url = fields.Url(dump_only=True, allow_none=True)
     icon_image_url = fields.Url(dump_only=True, allow_none=True)
diff --git a/app/api/users.py b/app/api/users.py
--- a/app/api/users.py
+++ b/app/api/users.py
@@ -87,6 +87,9 @@ def after_create_object(self, user, data, view_kwargs):
         #     del uploaded_images['large_image_url']
         #     self.session.query(User).filter_by(id=user.id).update(uploaded_images)
 
+        if data.get('avatar_url'):
+            start_image_resizing_tasks(user, data['avatar_url'])
+
     decorators = (api.has_permission('is_admin', methods="GET"),)
     schema = UserSchema
     data_layer = {'session': db.session,
@@ -222,6 +225,9 @@ def before_update_object(self, user, data, view_kwargs):
         if has_access('is_admin') and ('us_marketer' in data) and data.get('is_marketer') != user.is_marketer:
             user.is_marketer = not user.is_marketer
 
+        if data.get('avatar_url'):
+            start_image_resizing_tasks(user, data['avatar_url'])
+
     def after_update_object(self, user, data, view_kwargs):
         """
         method to mail user about email change
@@ -275,3 +281,9 @@ def is_email_available():
         abort(
             make_response(jsonify(error="Email field missing"), 422)
         )
+
+
+def start_image_resizing_tasks(user, original_image_url):
+    user_id = str(user.id)
+    from .helpers.tasks import resize_user_images_task
+    resize_user_images_task.delay(user_id, original_image_url)


Test patch: