<s> import base64 <EOL> import binascii <EOL> import imghdr <EOL> import uuid <EOL> from django . core . exceptions import ValidationError <EOL> from django . core . files . base import ContentFile <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from rest_framework . fields import ( <EOL> DateField , <EOL> DateTimeField , <EOL> DictField , <EOL> FloatField , <EOL> ImageField , <EOL> IntegerField , <EOL> ) <EOL> from rest_framework . utils import html , humanize_datetime , representation <EOL> from . compat import ( <EOL> DateRange , <EOL> DateTimeTZRange , <EOL> NumericRange , <EOL> postgres_fields , <EOL> ) <EOL> DEFAULT_CONTENT_TYPE = "<STR_LIT>" <EOL> ALLOWED_IMAGE_TYPES = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> EMPTY_VALUES = ( None , '<STR_LIT>' , [ ] , ( ) , { } ) <EOL> class Base64ImageField ( ImageField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . represent_in_base64 = kwargs . pop ( '<STR_LIT>' , False ) <EOL> super ( Base64ImageField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_internal_value ( self , base64_data ) : <EOL> if base64_data in EMPTY_VALUES : <EOL> return None <EOL> if isinstance ( base64_data , six . string_types ) : <EOL> if '<STR_LIT>' in base64_data : <EOL> header , base64_data = base64_data . split ( '<STR_LIT>' ) <EOL> try : <EOL> decoded_file = base64 . b64decode ( base64_data ) <EOL> except ( TypeError , binascii . Error ) : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> file_name = str ( uuid . uuid4 ( ) ) [ : <NUM_LIT:12> ] <EOL> file_extension = self . get_file_extension ( file_name , decoded_file ) <EOL> if file_extension not in ALLOWED_IMAGE_TYPES : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> complete_file_name = file_name + "<STR_LIT:.>" + file_extension <EOL> data = ContentFile ( decoded_file , name = complete_file_name ) <EOL> return super ( Base64ImageField , self ) . to_internal_value ( data ) <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> def get_file_extension ( self , filename , decoded_file ) : <EOL> extension = imghdr . what ( filename , decoded_file ) <EOL> extension = "<STR_LIT>" if extension == "<STR_LIT>" else extension <EOL> return extension <EOL> def to_representation ( self , image ) : <EOL> if self . represent_in_base64 : <EOL> try : <EOL> with open ( image . path , '<STR_LIT:rb>' ) as f : <EOL> return base64 . b64encode ( f . read ( ) ) . decode ( ) <EOL> except Exception : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> else : <EOL> return super ( ImageField , self ) . to_representation ( image ) <EOL> class RangeField ( DictField ) : <EOL> range_type = None <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def to_internal_value ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if html . is_html_input ( data ) : <EOL> data = html . parse_html_dict ( data ) <EOL> if not isinstance ( data , dict ) : <EOL> self . fail ( '<STR_LIT>' , input_type = type ( data ) . __name__ ) <EOL> validated_dict = { } <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> value = data . pop ( key ) <EOL> except KeyError : <EOL> continue <EOL> validated_dict [ six . text_type ( key ) ] = self . child . run_validation ( value ) <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> value = data . pop ( key ) <EOL> except KeyError : <EOL> continue <EOL> validated_dict [ six . text_type ( key ) ] = value <EOL> if data : <EOL> self . fail ( '<STR_LIT>' , extra = '<STR_LIT:U+002CU+0020>' . join ( map ( str , data . keys ( ) ) ) ) <EOL> return self . range_type ( ** validated_dict ) <EOL> def to_representation ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value . isempty : <EOL> return { '<STR_LIT>' : True } <EOL> lower = self . child . to_representation ( value . lower ) if value . lower is not None else None <EOL> upper = self . child . to_representation ( value . upper ) if value . upper is not None else None <EOL> return { '<STR_LIT>' : lower , <EOL> '<STR_LIT>' : upper , <EOL> '<STR_LIT>' : value . _bounds } <EOL> class IntegerRangeField ( RangeField ) : <EOL> child = IntegerField ( ) <EOL> range_type = NumericRange <EOL> class FloatRangeField ( RangeField ) : <EOL> child = FloatField ( ) <EOL> range_type = NumericRange <EOL> class DateTimeRangeField ( RangeField ) : <EOL> child = DateTimeField ( ) <EOL> range_type = DateTimeTZRange <EOL> class DateRangeField ( RangeField ) : <EOL> child = DateField ( ) <EOL> range_type = DateRange <EOL> if postgres_fields is not None : <EOL> from rest_framework . serializers import ModelSerializer <EOL> ModelSerializer . serializer_field_mapping [ postgres_fields . DateTimeRangeField ] = DateTimeRangeField <EOL> ModelSerializer . serializer_field_mapping [ postgres_fields . DateRangeField ] = DateRangeField <EOL> ModelSerializer . serializer_field_mapping [ postgres_fields . IntegerRangeField ] = IntegerRangeField <EOL> ModelSerializer . serializer_field_mapping [ postgres_fields . FloatRangeField ] = FloatRangeField </s>
