Skip to content

Commit b5dd7c5

Browse files
authored
Implement resizing of images for speakers (fossasia#5596)
* Refactor create image sizes method * Create celery task for resizing of speaker images
1 parent 359d016 commit b5dd7c5

File tree

5 files changed

+113
-68
lines changed

5 files changed

+113
-68
lines changed

app/api/helpers/files.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -135,63 +135,56 @@ def create_save_image_sizes(image_file, image_sizes_type, unique_identifier=None
135135
if unique_identifier is None:
136136
unique_identifier = get_file_name()
137137

138-
large_aspect = image_sizes.full_aspect if image_sizes.full_aspect else True
139-
large_basewidth = image_sizes.full_width if image_sizes.full_width else 1300
140-
large_height_size = image_sizes.full_height if image_sizes.full_width else 500
141-
142138
if image_sizes_type == 'speaker-image':
143-
thumbnail_aspect = image_sizes.thumbnail_aspect if image_sizes.thumbnail_aspect else True
139+
thumbnail_aspect = icon_aspect = small_aspect = True
144140
thumbnail_basewidth = thumbnail_height_size = image_sizes.thumbnail_size_width_height
145-
else:
146-
thumbnail_aspect = image_sizes.thumbnail_aspect
147-
thumbnail_basewidth = image_sizes.thumbnail_width
148-
thumbnail_height_size = image_sizes.thumbnail_height
149-
150-
if image_sizes_type == 'speaker-image':
151-
icon_aspect = image_sizes.icon_aspect if image_sizes.icon_aspect else True
152141
icon_basewidth = icon_height_size = image_sizes.icon_size_width_height
153-
else:
154-
icon_aspect = image_sizes.icon_aspect
155-
icon_basewidth = image_sizes.icon_width
156-
icon_height_size = image_sizes.icon_height
157-
158-
if image_sizes_type == 'event-image':
159-
original_upload_path = UPLOAD_PATHS['event']['original'].format(
160-
identifier=unique_identifier)
161-
large_upload_path = UPLOAD_PATHS['event']['large'].format(
162-
identifier=unique_identifier)
163-
thumbnail_upload_path = UPLOAD_PATHS['event']['thumbnail'].format(
164-
identifier=unique_identifier)
165-
icon_upload_path = UPLOAD_PATHS['event']['icon'].format(
166-
identifier=unique_identifier)
167-
elif image_sizes_type == 'speaker-image':
142+
small_basewidth = small_height_size = image_sizes.small_size_width_height
168143
original_upload_path = UPLOAD_PATHS['user']['original'].format(
169144
identifier=unique_identifier)
170-
large_upload_path = UPLOAD_PATHS['user']['large'].format(
145+
small_upload_path = UPLOAD_PATHS['user']['small'].format(
171146
identifier=unique_identifier)
172147
thumbnail_upload_path = UPLOAD_PATHS['user']['thumbnail'].format(
173148
identifier=unique_identifier)
174149
icon_upload_path = UPLOAD_PATHS['user']['icon'].format(
175150
identifier=unique_identifier)
151+
new_images = {
152+
'original_image_url': create_save_resized_image(image_file, 0, 0, 0, original_upload_path, resize=False),
153+
'small_image_url': create_save_resized_image(image_file, small_basewidth, small_aspect, small_height_size,
154+
small_upload_path),
155+
'thumbnail_image_url': create_save_resized_image(image_file, thumbnail_basewidth, thumbnail_aspect,
156+
thumbnail_height_size, thumbnail_upload_path),
157+
'icon_image_url': create_save_resized_image(image_file, icon_basewidth, icon_aspect, icon_height_size,
158+
icon_upload_path)
159+
}
160+
176161
else:
177-
original_upload_path = UPLOAD_PATHS[image_sizes_type]['original'].format(
162+
large_aspect = image_sizes.full_aspect if image_sizes.full_aspect else False
163+
large_basewidth = image_sizes.full_width if image_sizes.full_width else 1300
164+
large_height_size = image_sizes.full_height if image_sizes.full_width else 500
165+
thumbnail_aspect = image_sizes.thumbnail_aspect if image_sizes.full_aspect else False
166+
thumbnail_basewidth = image_sizes.thumbnail_width if image_sizes.thumbnail_width else 500
167+
thumbnail_height_size = image_sizes.thumbnail_height if image_sizes.thumbnail_height else 200
168+
icon_aspect = image_sizes.icon_aspect if image_sizes.icon_aspect else False
169+
icon_basewidth = image_sizes.icon_width if image_sizes.icon_width else 75
170+
icon_height_size = image_sizes.icon_height if image_sizes.icon_height else 30
171+
original_upload_path = UPLOAD_PATHS['event']['original'].format(
178172
identifier=unique_identifier)
179-
large_upload_path = UPLOAD_PATHS[image_sizes_type]['large'].format(
173+
large_upload_path = UPLOAD_PATHS['event']['large'].format(
180174
identifier=unique_identifier)
181-
thumbnail_upload_path = UPLOAD_PATHS[image_sizes_type]['thumbnail'].format(
175+
thumbnail_upload_path = UPLOAD_PATHS['event']['thumbnail'].format(
182176
identifier=unique_identifier)
183-
icon_upload_path = UPLOAD_PATHS[image_sizes_type]['icon'].format(
177+
icon_upload_path = UPLOAD_PATHS['event']['icon'].format(
184178
identifier=unique_identifier)
185-
186-
new_images = {
187-
'original_image_url': create_save_resized_image(image_file, 0, 0, 0, original_upload_path, resize=False),
188-
'large_image_url': create_save_resized_image(image_file, large_basewidth, large_aspect, large_height_size,
189-
large_upload_path),
190-
'thumbnail_image_url': create_save_resized_image(image_file, thumbnail_basewidth, thumbnail_aspect,
191-
thumbnail_height_size, thumbnail_upload_path),
192-
'icon_image_url': create_save_resized_image(image_file, icon_basewidth, icon_aspect, icon_height_size,
193-
icon_upload_path)
194-
}
179+
new_images = {
180+
'original_image_url': create_save_resized_image(image_file, 0, 0, 0, original_upload_path, resize=False),
181+
'large_image_url': create_save_resized_image(image_file, large_basewidth, large_aspect, large_height_size,
182+
large_upload_path),
183+
'thumbnail_image_url': create_save_resized_image(image_file, thumbnail_basewidth, thumbnail_aspect,
184+
thumbnail_height_size, thumbnail_upload_path),
185+
'icon_image_url': create_save_resized_image(image_file, icon_basewidth, icon_aspect, icon_height_size,
186+
icon_upload_path)
187+
}
195188

196189
return new_images
197190

app/api/helpers/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
'avatar': 'users/{user_id}/avatar',
4747
'thumbnail': 'users/{identifier}/thumbnail',
4848
'original': 'users/{identifier}/original',
49-
'large': 'users/{identifier}/large',
49+
'small': 'users/{identifier}/small',
5050
'icon': 'users/{identifier}/icon'
5151
},
5252
'temp': {

app/api/helpers/tasks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ def resize_event_images_task(self, event_id, original_image_url):
9797
logging.exception('Error encountered while generating resized images for event with id: {}'.format(event_id))
9898

9999

100+
@celery.task(base=RequestContextTask, name='resize.speaker.images', bind=True)
101+
def resize_speaker_images_task(self, speaker_id, photo_url):
102+
speaker = safe_query(db, Speaker, 'id', speaker_id, 'speaker_id')
103+
try:
104+
logging.info('Speaker image resizing tasks started for speaker with id {}'.format(speaker_id))
105+
uploaded_images = create_save_image_sizes(photo_url, 'speaker-image', speaker_id)
106+
speaker.small_image_url = uploaded_images['small_image_url']
107+
speaker.thumbnail_image_url = uploaded_images['thumbnail_image_url']
108+
speaker.icon_image_url = uploaded_images['icon_image_url']
109+
save_to_db(speaker)
110+
logging.info('Resized images saved successfully for speaker with id: {}'.format(speaker_id))
111+
except (urllib.error.HTTPError, urllib.error.URLError):
112+
logging.exception('Error encountered while generating resized images for event with id: {}'.format(speaker_id))
113+
114+
100115
@celery.task(base=RequestContextTask, name='export.event', bind=True)
101116
def export_event_task(self, email, event_id, settings):
102117
event = safe_query(db, Event, 'id', event_id, 'event_id')

app/api/speakers.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,25 @@ def before_post(self, args, kwargs, data=None):
5151
raise ObjectNotFound({'parameter': 'session_id'},
5252
"Session: {} not found".format(session_id))
5353

54+
def after_create_object(self, speaker, data, view_kwargs):
55+
"""
56+
after create method to save resized images for speaker
57+
:param speaker:
58+
:param data:
59+
:param view_kwargs:
60+
:return:
61+
"""
62+
63+
if data.get('photo_url'):
64+
start_image_resizing_tasks(speaker, data['photo_url'])
65+
5466
schema = SpeakerSchema
5567
methods = ['POST', ]
5668
data_layer = {'session': db.session,
57-
'model': Speaker
58-
}
69+
'model': Speaker,
70+
'methods': {
71+
'after_create_object': after_create_object
72+
}}
5973

6074

6175
class SpeakerList(ResourceList):
@@ -100,11 +114,27 @@ class SpeakerDetail(ResourceDetail):
100114
"""
101115
Speakers Detail by id
102116
"""
117+
118+
def before_update_object(self, speaker, data, view_kwargs):
119+
"""
120+
method to save image urls before updating speaker object
121+
:param speaker:
122+
:param data:
123+
:param view_kwargs:
124+
:return:
125+
"""
126+
127+
if data.get('photo_url') and data['photo_url'] != speaker.photo_url:
128+
start_image_resizing_tasks(speaker, data['photo_url'])
129+
103130
decorators = (api.has_permission('is_coorganizer_or_user_itself', methods="PATCH,DELETE", fetch="event_id",
104131
fetch_as="event_id", model=Speaker),)
105132
schema = SpeakerSchema
106133
data_layer = {'session': db.session,
107-
'model': Speaker}
134+
'model': Speaker,
135+
'methods': {
136+
'before_update_object': before_update_object
137+
}}
108138

109139

110140
class SpeakerRelationshipRequired(ResourceRelationship):
@@ -128,3 +158,9 @@ class SpeakerRelationshipOptional(ResourceRelationship):
128158
schema = SpeakerSchema
129159
data_layer = {'session': db.session,
130160
'model': Speaker}
161+
162+
163+
def start_image_resizing_tasks(speaker, photo_url):
164+
speaker_id = str(speaker.id)
165+
from .helpers.tasks import resize_speaker_images_task
166+
resize_speaker_images_task.delay(speaker_id, photo_url)

app/api/users.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ def after_create_object(self, user, data, view_kwargs):
7171
send_email_with_action(user, USER_REGISTER_WITH_PASSWORD, app_name=get_settings()['app_name'],
7272
email=user.email)
7373
send_email_confirmation(user.email, link)
74-
75-
if data.get('original_image_url'):
76-
try:
77-
uploaded_images = create_save_image_sizes(data['original_image_url'], 'speaker-image', user.id)
78-
except (urllib.error.HTTPError, urllib.error.URLError):
79-
raise UnprocessableEntity(
80-
{'source': 'attributes/original-image-url'}, 'Invalid Image URL'
81-
)
82-
uploaded_images['small_image_url'] = uploaded_images['thumbnail_image_url']
83-
del uploaded_images['large_image_url']
84-
self.session.query(User).filter_by(id=user.id).update(uploaded_images)
74+
# TODO Handle in a celery task
75+
# if data.get('original_image_url'):
76+
# try:
77+
# uploaded_images = create_save_image_sizes(data['original_image_url'], 'speaker-image', user.id)
78+
# except (urllib.error.HTTPError, urllib.error.URLError):
79+
# raise UnprocessableEntity(
80+
# {'source': 'attributes/original-image-url'}, 'Invalid Image URL'
81+
# )
82+
# uploaded_images['small_image_url'] = uploaded_images['thumbnail_image_url']
83+
# del uploaded_images['large_image_url']
84+
# self.session.query(User).filter_by(id=user.id).update(uploaded_images)
8585

8686
decorators = (api.has_permission('is_admin', methods="GET"),)
8787
schema = UserSchema
@@ -188,17 +188,18 @@ def before_get_object(self, view_kwargs):
188188
view_kwargs['id'] = None
189189

190190
def before_update_object(self, user, data, view_kwargs):
191-
if data.get('original_image_url') and data['original_image_url'] != user.original_image_url:
192-
try:
193-
uploaded_images = create_save_image_sizes(data['original_image_url'], 'speaker-image', user.id)
194-
except (urllib.error.HTTPError, urllib.error.URLError):
195-
raise UnprocessableEntity(
196-
{'source': 'attributes/original-image-url'}, 'Invalid Image URL'
197-
)
198-
data['original_image_url'] = uploaded_images['original_image_url']
199-
data['small_image_url'] = uploaded_images['thumbnail_image_url']
200-
data['thumbnail_image_url'] = uploaded_images['thumbnail_image_url']
201-
data['icon_image_url'] = uploaded_images['icon_image_url']
191+
# TODO: Make a celery task for this
192+
# if data.get('avatar_url') and data['original_image_url'] != user.original_image_url:
193+
# try:
194+
# uploaded_images = create_save_image_sizes(data['original_image_url'], 'speaker-image', user.id)
195+
# except (urllib.error.HTTPError, urllib.error.URLError):
196+
# raise UnprocessableEntity(
197+
# {'source': 'attributes/original-image-url'}, 'Invalid Image URL'
198+
# )
199+
# data['original_image_url'] = uploaded_images['original_image_url']
200+
# data['small_image_url'] = uploaded_images['thumbnail_image_url']
201+
# data['thumbnail_image_url'] = uploaded_images['thumbnail_image_url']
202+
# data['icon_image_url'] = uploaded_images['icon_image_url']
202203

203204
if data.get('email') and data['email'] != user.email:
204205
try:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy