# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Erik Stein <code@classlibrary.net>, 2016
from django . db import models
from django . utils . translation import ugexttext_lazy as _
from imagekit . models import ImageSpecField
from imagekit . processors import Adjust , Thumbnail , ResizeToFit
from . . exceptions import ConversionNotApplicable
from . . processors import Noop , Placeholder
from . fields import MediaAssetFileField
MEDIA_TYPE_CHOICES = (
( UNKNOWN , _ ( " unknown " ) ) ,
( IMAGE , _ ( " image " ) ) ,
( MOVIE , _ ( " movie " ) ) ,
( AUDIO , _ ( " audio " ) ) ,
( PDF , _ ( " pdf " ) ) ,
( SVG , _ ( " svg " ) ) ,
( VECTOR , _ ( " eps " ) ) ,
( DOCUMENT , _ ( " document " ) ) ,
)
class ImageMediaHandler ( object ) :
# thumbnail = ImageSpecField(ThumbnailSpec)
thumbnail = ImageSpecField ( source = ' file ' ,
processors = [ Adjust ( contrast = 1.2 , sharpness = 1.1 ) ,
Thumbnail ( 100 , 50 ) ] ,
format = ' JPEG ' , options = { ' quality ' : 90 } )
# class PDFFile(object)
# def get_page(self, num):
# def get_page_count(self):
class PDFMediaHandler ( object ) :
# thumbnail = ImageSpec
# small_downloadable = PDFSpecField
def page_count ( self ) :
# self.original_file = PDFFile()
return count ( self . original_file . pages )
class MovieMediaHandler ( object ) :
# thumbnail = ImageSpec
# desktop_webm = MovieSpecField
# desktop_mp4 = MovieSpecField
pass
class MediaAsset ( models . Model ) :
"""
TODO Django should have a internal = True parameter on FileFields , preventing URL based access to the files
Depending on the type the Asset might have different
"""
# TODO Added uuid field
original_file = MediaAssetFileField ( _ ( " original file " ) , metadata_field = ' metadata ' )
original_mimetype = models . CharField ( _ ( " mimetype " ) , blank = True , max_length = 50 )
metadata = models . JSONField ( )
media_type = models . ChoiceField ( _ ( " media type " ) , blank = True , choices = MEDIA_TYPE_CHOICES , default = MEDIA_TYPE_CHOICES [ 0 ] [ 0 ] )
description = models . TextField ( _ ( " internal description " ) , null = True , blank = True )
slug = SlugField ( help_text = _ ( " should contain only the most specific part of the description, e.g. \" poster \" ; the whole filename will be automatically build from the object the asset is attached and " ) )
# copyright = models.TextField(_("copyright"), null=True, blank=True)
# author = models.TextField(_("author"), null=True, blank=True)
def save ( self , * args , * * kwargs ) :
# TODO Fill media_type field
# TODO Fill mimetype field
super ( MediaAsset , self ) . save ( * args , * * kwargs )
@property
def media_handler ( self ) :
"""
Returns a media_handler instance depending on the type
of the original file .
>> > m = MediaAsset . objects . first ( )
>> > m . original_file
' media/0/reference.png '
>> > m . media_handler . thumbnail
"""
pass