-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from edx/schen/ECOM-5031
ECOM-5031 Incorporate the stdImage library for the program banner image
- Loading branch information
Showing
14 changed files
with
199 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from rest_framework import serializers | ||
|
||
|
||
class StdImageSerializerField(serializers.Field): | ||
""" | ||
Custom serializer field to render out proper JSON representation of the StdImage field on model | ||
""" | ||
def to_representation(self, obj): | ||
serialized = {} | ||
for size_key in obj.field.variations: | ||
# Get different sizes specs from the model field | ||
# Then get the file path from the available files | ||
sized_file = getattr(obj, size_key, None) | ||
if sized_file: | ||
path = sized_file.url | ||
serialized_image = serialized.setdefault(size_key, {}) | ||
# In case MEDIA_URL does not include scheme+host, ensure that the URLs are absolute and not relative | ||
serialized_image['url'] = self.context['request'].build_absolute_uri(path) | ||
serialized_image['width'] = obj.field.variations[size_key]['width'] | ||
serialized_image['height'] = obj.field.variations[size_key]['height'] | ||
|
||
return serialized | ||
|
||
def to_internal_value(self, obj): | ||
""" We do not need to save/edit this banner image through serializer yet """ | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
Helper methods for testing the processing of image files. | ||
""" | ||
from io import BytesIO | ||
from PIL import Image | ||
|
||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
|
||
|
||
def make_image_stream(): | ||
""" | ||
Helper to generate values for program banner_image | ||
""" | ||
image = Image.new('RGB', (1440, 900), 'green') | ||
bio = BytesIO() | ||
image.save(bio, format='JPEG') | ||
return bio | ||
|
||
|
||
def make_image_file(name): | ||
image_stream = make_image_stream() | ||
return SimpleUploadedFile(name, image_stream.getvalue(), content_type='image/jpeg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
course_discovery/apps/course_metadata/data_loaders/tests/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
JSON = 'application/json' | ||
JPEG = 'image/jpeg' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
course_discovery/apps/course_metadata/migrations/0019_program_banner_image.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import stdimage.models | ||
import stdimage.utils | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('course_metadata', '0018_auto_20160815_2252'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='program', | ||
name='banner_image', | ||
field=stdimage.models.StdImageField(upload_to=stdimage.utils.UploadToAutoSlugClassNameDir('uuid', path='/media/programs/banner_images'), null=True, blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters