Commit b87594b0 by Mushtaq Ali

More changes

parent 7c491711
......@@ -9,12 +9,14 @@ from lxml.etree import Element, SubElement
from enum import Enum
from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
from edxval.models import Video, EncodedVideo, CourseVideo, Profile, VideoImage
from edxval.serializers import VideoSerializer
from utils import get_video_image_storage
logger = logging.getLogger(__name__) # pylint: disable=C0103
from django.core.files.base import ContentFile
class ValError(Exception):
......@@ -183,17 +185,33 @@ def update_video_status(edx_video_id, status):
video.save()
def update_video_image(edx_video_id, course_id, serialized_data):
def get_video_image_url(video_image, image_filename=None):
"""
Returns video image url.
"""
storage = get_video_image_storage()
# TODO: Is getting file path using video_image.image.name a good way? or Do we need to get file path using
# video_image_path_name(video_image, file_name)
file_name = video_image.image.name
video_image_url = storage.url(file_name)
return video_image_url
def update_video_image(edx_video_id, course_id, serialized_data, file_name):
"""
Update video image for an existing video.
Args:
edx_video_id: ID of the video
edx_video_id: ID of the video.
course_id: ID of course.
serialized_data: Serialized data of image for video.
file_name: File name of the image file.
Raises:
Raises ValVideoNotFoundError if the video cannot be retrieved.
"""
# TODO: Is sending file name a good way ?
try:
video = _get_video(edx_video_id)
......@@ -207,9 +225,9 @@ def update_video_image(edx_video_id, course_id, serialized_data):
video=video,
course_id=course_id
)
video_image.image.save('', ContentFile(serialized_data))
video_image.image.save(file_name, ContentFile(serialized_data))
video_image.save()
return get_video_image_url(video_image, file_name)
def create_profile(profile_name):
......
......@@ -13,12 +13,12 @@ invalid profile_name will be returned.
import logging
from django.conf import settings
from django.db import models
from django.dispatch import receiver
from django.core.validators import MinValueValidator, RegexValidator
from django.core.urlresolvers import reverse
from django.core.files.storage import get_storage_class
from utils import video_image_path_name, get_video_image_storage
logger = logging.getLogger(__name__) # pylint: disable=C0103
......@@ -74,36 +74,6 @@ class Profile(models.Model):
return self.profile_name
def _create_path(directory, filename):
"""
Returns the full path for the given directory and filename.
"""
return '{}-{}'.format(directory, filename)
def _directory_name(edx_video_id):
"""
Returns the directory name for the given edx_video_id.
"""
return '{}{}'.format(settings.VIDEO_THUMBNAIL_SETTINGS.get('DIRECTORY_PREFIX', ''), edx_video_id)
def video_thumbnail_path_name(thumbnail_model, filename): # pylint:disable=unused-argument
"""
Returns path name to use for the given Video instance.
"""
return _create_path(_directory_name(thumbnail_model.video_id), filename)
def get_video_thumbnail_storage():
"""
Return the configured django storage backend.
"""
return get_storage_class(
settings.VIDEO_THUMBNAIL_SETTINGS.get('STORAGE_CLASS'),
)(**settings.VIDEO_THUMBNAIL_SETTINGS.get('STORAGE_KWARGS', {}))
class CustomizableFileField(models.ImageField):
"""
Subclass of FileField that allows custom settings to not
......@@ -114,8 +84,8 @@ class CustomizableFileField(models.ImageField):
"""
def __init__(self, *args, **kwargs):
kwargs.update(dict(
upload_to=video_thumbnail_path_name,
storage=get_video_thumbnail_storage(),
upload_to=video_image_path_name,
storage=get_video_image_storage(),
max_length=500, # allocate enough for filepath
blank=True,
null=True
......
......@@ -180,9 +180,9 @@ LOGGING = {
COURSE_KEY_PATTERN = r'(?P<course_key_string>[^/+]+(/|\+)[^/+]+(/|\+)[^/]+)'
COURSE_ID_PATTERN = COURSE_KEY_PATTERN.replace('course_key_string', 'course_id')
VIDEO_THUMBNAIL_SETTINGS = dict(
VIDEO_IMAGE_SETTINGS = dict(
# Backend storage
# STORAGE_CLASS='storages.backends.s3boto.S3BotoStorage',
# STORAGE_KWARGS=dict(bucket='video-inage-test-bucket'),
# STORAGE_KWARGS=dict(bucket='video-image-test-bucket'),
DIRECTORY_PREFIX='videoimage/',
)
"""
Util methods to be used in api and models.
"""
from django.conf import settings
from django.core.files.storage import get_storage_class
def _create_path(directory, filename):
"""
Returns the full path for the given directory and filename.
"""
return '{}-{}'.format(directory, filename)
def _directory_name(edx_video_id):
"""
Returns the directory name for the given edx_video_id.
"""
return '{}{}'.format(settings.VIDEO_IMAGE_SETTINGS.get('DIRECTORY_PREFIX', ''), edx_video_id)
def video_image_path_name(image_model, filename): # pylint:disable=unused-argument
"""
Returns path name to use for the given Video instance.
"""
return _create_path(_directory_name(image_model.video.edx_video_id), filename)
def get_video_image_storage():
"""
Return the configured django storage backend.
"""
return get_storage_class(
settings.VIDEO_IMAGE_SETTINGS.get('STORAGE_CLASS'),
)(**settings.VIDEO_IMAGE_SETTINGS.get('STORAGE_KWARGS', {}))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment