2014-12-10 21:25:09 +00:00
import os
2014-11-01 21:02:31 +00:00
import uuid
2015-09-29 04:28:20 +00:00
2014-11-01 21:02:31 +00:00
from django import forms
from django . conf import settings
2015-10-18 08:25:03 +00:00
from django . utils . six import BytesIO
2014-11-01 21:02:31 +00:00
from django . utils . translation import ugettext_lazy as _
from django . core . files . uploadedfile import InMemoryUploadedFile
from django . template import defaultfilters as filters
from . utils import scale_and_crop
from . settings import (
2015-09-06 07:41:45 +00:00
MARKDOWNX_IMAGE_MAX_SIZE ,
2014-11-01 21:02:31 +00:00
MARKDOWNX_MEDIA_PATH ,
2015-09-06 07:41:45 +00:00
MARKDOWNX_UPLOAD_CONTENT_TYPES ,
MARKDOWNX_UPLOAD_MAX_SIZE ,
2014-11-01 21:02:31 +00:00
)
2015-09-06 07:41:45 +00:00
2014-11-01 21:02:31 +00:00
class ImageForm ( forms . Form ) :
2015-09-06 07:41:45 +00:00
2014-11-01 21:02:31 +00:00
image = forms . ImageField ( )
def save ( self , commit = True ) :
2015-09-06 07:41:45 +00:00
img = scale_and_crop ( self . files [ ' image ' ] , * * MARKDOWNX_IMAGE_MAX_SIZE )
2015-10-18 08:25:03 +00:00
thumb_io = BytesIO ( )
2014-11-01 21:02:31 +00:00
img . save ( thumb_io , self . files [ ' image ' ] . content_type . split ( ' / ' ) [ - 1 ] . upper ( ) )
2015-09-06 07:41:45 +00:00
2014-11-01 21:02:31 +00:00
file_name = str ( self . files [ ' image ' ] )
2015-10-19 14:44:58 +00:00
thumb_io . seek ( 0 , os . SEEK_END )
img = InMemoryUploadedFile ( thumb_io , " image " , file_name , self . files [ ' image ' ] . content_type , thumb_io . tell ( ) , None )
2015-09-06 07:41:45 +00:00
2014-11-01 21:02:31 +00:00
unique_file_name = self . get_unique_file_name ( file_name )
2014-12-10 21:25:09 +00:00
full_path = os . path . join ( settings . MEDIA_ROOT , MARKDOWNX_MEDIA_PATH , unique_file_name )
2014-11-01 21:02:31 +00:00
if not os . path . exists ( os . path . dirname ( full_path ) ) :
os . makedirs ( os . path . dirname ( full_path ) )
2015-09-06 07:41:45 +00:00
2014-11-01 21:02:31 +00:00
destination = open ( full_path , ' wb+ ' )
for chunk in img . chunks ( ) :
destination . write ( chunk )
destination . close ( )
2014-12-10 21:25:09 +00:00
return os . path . join ( settings . MEDIA_URL , MARKDOWNX_MEDIA_PATH , unique_file_name )
2014-11-01 21:02:31 +00:00
def get_unique_file_name ( instance , filename ) :
ext = filename . split ( ' . ' ) [ - 1 ]
filename = " %s . %s " % ( uuid . uuid4 ( ) , ext )
return filename
def clean ( self ) :
upload = self . cleaned_data [ ' image ' ]
content_type = upload . content_type
2015-09-06 07:41:45 +00:00
if content_type in MARKDOWNX_UPLOAD_CONTENT_TYPES :
if upload . _size > MARKDOWNX_UPLOAD_MAX_SIZE :
raise forms . ValidationError ( _ ( ' Please keep filesize under %(max)s . Current filesize %(current)s ' ) % { ' max ' : filters . filesizeformat ( MARKDOWNX_UPLOAD_MAX_SIZE ) , ' current ' : filters . filesizeformat ( upload . _size ) } )
2014-11-01 21:02:31 +00:00
else :
raise forms . ValidationError ( _ ( ' File type is not supported ' ) )
return upload