<s> from __future__ import unicode_literals <EOL> import collections <EOL> import copy <EOL> import datetime <EOL> import decimal <EOL> import math <EOL> import warnings <EOL> from itertools import tee <EOL> from django . db import connection <EOL> from django . db . models . query_utils import QueryWrapper <EOL> from django . conf import settings <EOL> from django import forms <EOL> from django . core import exceptions , validators <EOL> from django . utils . datastructures import DictWrapper <EOL> from django . utils . dateparse import parse_date , parse_datetime , parse_time <EOL> from django . utils . functional import curry , total_ordering <EOL> from django . utils . text import capfirst <EOL> from django . utils import timezone <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_text , force_text <EOL> from django . utils . ipv6 import clean_ipv6_address <EOL> from django . utils import six <EOL> class NOT_PROVIDED : <EOL> pass <EOL> BLANK_CHOICE_DASH = [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> BLANK_CHOICE_NONE = [ ( "<STR_LIT>" , "<STR_LIT:None>" ) ] <EOL> class FieldDoesNotExist ( Exception ) : <EOL> pass <EOL> @ total_ordering <EOL> class Field ( object ) : <EOL> """<STR_LIT>""" <EOL> empty_strings_allowed = True <EOL> creation_counter = <NUM_LIT:0> <EOL> auto_creation_counter = - <NUM_LIT:1> <EOL> default_validators = [ ] <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT:null>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT:blank>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> } <EOL> def _description ( self ) : <EOL> return _ ( '<STR_LIT>' ) % { <EOL> '<STR_LIT>' : self . __class__ . __name__ <EOL> } <EOL> description = property ( _description ) <EOL> def __init__ ( self , verbose_name = None , name = None , primary_key = False , <EOL> max_length = None , unique = False , blank = False , null = False , <EOL> db_index = False , rel = None , default = NOT_PROVIDED , editable = True , <EOL> serialize = True , unique_for_date = None , unique_for_month = None , <EOL> unique_for_year = None , choices = None , help_text = '<STR_LIT>' , db_column = None , <EOL> db_tablespace = None , auto_created = False , validators = [ ] , <EOL> error_messages = None ) : <EOL> self . name = name <EOL> self . verbose_name = verbose_name <EOL> self . primary_key = primary_key <EOL> self . max_length , self . _unique = max_length , unique <EOL> self . blank , self . null = blank , null <EOL> self . rel = rel <EOL> self . default = default <EOL> self . editable = editable <EOL> self . serialize = serialize <EOL> self . unique_for_date , self . unique_for_month = ( unique_for_date , <EOL> unique_for_month ) <EOL> self . unique_for_year = unique_for_year <EOL> self . _choices = choices or [ ] <EOL> self . help_text = help_text <EOL> self . db_column = db_column <EOL> self . db_tablespace = db_tablespace or settings . DEFAULT_INDEX_TABLESPACE <EOL> self . auto_created = auto_created <EOL> self . db_index = db_index <EOL> if auto_created : <EOL> self . creation_counter = Field . auto_creation_counter <EOL> Field . auto_creation_counter -= <NUM_LIT:1> <EOL> else : <EOL> self . creation_counter = Field . creation_counter <EOL> Field . creation_counter += <NUM_LIT:1> <EOL> self . validators = self . default_validators + validators <EOL> messages = { } <EOL> for c in reversed ( self . __class__ . __mro__ ) : <EOL> messages . update ( getattr ( c , '<STR_LIT>' , { } ) ) <EOL> messages . update ( error_messages or { } ) <EOL> self . error_messages = messages <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Field ) : <EOL> return self . creation_counter == other . creation_counter <EOL> return NotImplemented <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , Field ) : <EOL> return self . creation_counter < other . creation_counter <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> return hash ( self . creation_counter ) <EOL> def __deepcopy__ ( self , memodict ) : <EOL> obj = copy . copy ( self ) <EOL> if self . rel : <EOL> obj . rel = copy . copy ( self . rel ) <EOL> memodict [ id ( self ) ] = obj <EOL> return obj <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def run_validators ( self , value ) : <EOL> if value in validators . EMPTY_VALUES : <EOL> return <EOL> errors = [ ] <EOL> for v in self . validators : <EOL> try : <EOL> v ( value ) <EOL> except exceptions . ValidationError as e : <EOL> if hasattr ( e , '<STR_LIT:code>' ) and e . code in self . error_messages : <EOL> message = self . error_messages [ e . code ] <EOL> if e . params : <EOL> message = message % e . params <EOL> errors . append ( message ) <EOL> else : <EOL> errors . extend ( e . messages ) <EOL> if errors : <EOL> raise exceptions . ValidationError ( errors ) <EOL> def validate ( self , value , model_instance ) : <EOL> """<STR_LIT>""" <EOL> if not self . editable : <EOL> return <EOL> if self . _choices and value not in validators . EMPTY_VALUES : <EOL> for option_key , option_value in self . choices : <EOL> if isinstance ( option_value , ( list , tuple ) ) : <EOL> for optgroup_key , optgroup_value in option_value : <EOL> if value == optgroup_key : <EOL> return <EOL> elif value == option_key : <EOL> return <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> if value is None and not self . null : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT:null>' ] ) <EOL> if not self . blank and value in validators . EMPTY_VALUES : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT:blank>' ] ) <EOL> def clean ( self , value , model_instance ) : <EOL> """<STR_LIT>""" <EOL> value = self . to_python ( value ) <EOL> self . validate ( value , model_instance ) <EOL> self . run_validators ( value ) <EOL> return value <EOL> def db_type ( self , connection ) : <EOL> """<STR_LIT>""" <EOL> data = DictWrapper ( self . __dict__ , connection . ops . quote_name , "<STR_LIT>" ) <EOL> try : <EOL> return ( connection . creation . data_types [ self . get_internal_type ( ) ] <EOL> % data ) <EOL> except KeyError : <EOL> return None <EOL> @ property <EOL> def unique ( self ) : <EOL> return self . _unique or self . primary_key <EOL> def set_attributes_from_name ( self , name ) : <EOL> if not self . name : <EOL> self . name = name <EOL> self . attname , self . column = self . get_attname_column ( ) <EOL> if self . verbose_name is None and self . name : <EOL> self . verbose_name = self . name . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> self . set_attributes_from_name ( name ) <EOL> self . model = cls <EOL> cls . _meta . add_field ( self ) <EOL> if self . choices : <EOL> setattr ( cls , '<STR_LIT>' % self . name , <EOL> curry ( cls . _get_FIELD_display , field = self ) ) <EOL> def get_attname ( self ) : <EOL> return self . name <EOL> def get_attname_column ( self ) : <EOL> attname = self . get_attname ( ) <EOL> column = self . db_column or attname <EOL> return attname , column <EOL> def get_cache_name ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def get_internal_type ( self ) : <EOL> return self . __class__ . __name__ <EOL> def pre_save ( self , model_instance , add ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( model_instance , self . attname ) <EOL> def get_prep_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> """<STR_LIT>""" <EOL> if not prepared : <EOL> value = self . get_prep_value ( value ) <EOL> return value <EOL> def get_db_prep_save ( self , value , connection ) : <EOL> """<STR_LIT>""" <EOL> return self . get_db_prep_value ( value , connection = connection , <EOL> prepared = False ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value . prepare ( ) <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value . _prepare ( ) <EOL> if lookup_type in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) : <EOL> return value <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return self . get_prep_value ( value ) <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ self . get_prep_value ( v ) for v in value ] <EOL> elif lookup_type == '<STR_LIT>' : <EOL> try : <EOL> return int ( value ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise TypeError ( "<STR_LIT>" % lookup_type ) <EOL> def get_db_prep_lookup ( self , lookup_type , value , connection , <EOL> prepared = False ) : <EOL> """<STR_LIT>""" <EOL> if not prepared : <EOL> value = self . get_prep_lookup ( lookup_type , value ) <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . get_compiler ( connection = connection ) <EOL> if hasattr ( value , '<STR_LIT>' ) or hasattr ( value , '<STR_LIT>' ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> sql , params = value . as_sql ( ) <EOL> else : <EOL> sql , params = value . _as_sql ( connection = connection ) <EOL> return QueryWrapper ( ( '<STR_LIT>' % sql ) , params ) <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> return [ value ] <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ self . get_db_prep_value ( value , connection = connection , <EOL> prepared = prepared ) ] <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ self . get_db_prep_value ( v , connection = connection , <EOL> prepared = prepared ) for v in value ] <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ "<STR_LIT>" % connection . ops . prep_for_like_query ( value ) ] <EOL> elif lookup_type == '<STR_LIT>' : <EOL> return [ connection . ops . prep_for_iexact_query ( value ) ] <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ "<STR_LIT>" % connection . ops . prep_for_like_query ( value ) ] <EOL> elif lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ "<STR_LIT>" % connection . ops . prep_for_like_query ( value ) ] <EOL> elif lookup_type == '<STR_LIT>' : <EOL> return [ ] <EOL> elif lookup_type == '<STR_LIT>' : <EOL> if self . get_internal_type ( ) == '<STR_LIT>' : <EOL> return connection . ops . year_lookup_bounds_for_date_field ( value ) <EOL> else : <EOL> return connection . ops . year_lookup_bounds ( value ) <EOL> def has_default ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . default is not NOT_PROVIDED <EOL> def get_default ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . has_default ( ) : <EOL> if callable ( self . default ) : <EOL> return self . default ( ) <EOL> return force_text ( self . default , strings_only = True ) <EOL> if ( not self . empty_strings_allowed or ( self . null and <EOL> not connection . features . interprets_empty_strings_as_nulls ) ) : <EOL> return None <EOL> return "<STR_LIT>" <EOL> def get_validator_unique_lookup_type ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def get_choices ( self , include_blank = True , blank_choice = BLANK_CHOICE_DASH ) : <EOL> """<STR_LIT>""" <EOL> first_choice = include_blank and blank_choice or [ ] <EOL> if self . choices : <EOL> return first_choice + list ( self . choices ) <EOL> rel_model = self . rel . to <EOL> if hasattr ( self . rel , '<STR_LIT>' ) : <EOL> lst = [ ( getattr ( x , self . rel . get_related_field ( ) . attname ) , <EOL> smart_text ( x ) ) <EOL> for x in rel_model . _default_manager . complex_filter ( <EOL> self . rel . limit_choices_to ) ] <EOL> else : <EOL> lst = [ ( x . _get_pk_val ( ) , smart_text ( x ) ) <EOL> for x in rel_model . _default_manager . complex_filter ( <EOL> self . rel . limit_choices_to ) ] <EOL> return first_choice + lst <EOL> def get_choices_default ( self ) : <EOL> return self . get_choices ( ) <EOL> def get_flatchoices ( self , include_blank = True , <EOL> blank_choice = BLANK_CHOICE_DASH ) : <EOL> """<STR_LIT>""" <EOL> first_choice = include_blank and blank_choice or [ ] <EOL> return first_choice + list ( self . flatchoices ) <EOL> def _get_val_from_obj ( self , obj ) : <EOL> if obj is not None : <EOL> return getattr ( obj , self . attname ) <EOL> else : <EOL> return self . get_default ( ) <EOL> def value_to_string ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return smart_text ( self . _get_val_from_obj ( obj ) ) <EOL> def bind ( self , fieldmapping , original , bound_field_class ) : <EOL> return bound_field_class ( self , fieldmapping , original ) <EOL> def _get_choices ( self ) : <EOL> if isinstance ( self . _choices , collections . Iterator ) : <EOL> choices , self . _choices = tee ( self . _choices ) <EOL> return choices <EOL> else : <EOL> return self . _choices <EOL> choices = property ( _get_choices ) <EOL> def _get_flatchoices ( self ) : <EOL> """<STR_LIT>""" <EOL> flat = [ ] <EOL> for choice , value in self . choices : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> flat . extend ( value ) <EOL> else : <EOL> flat . append ( ( choice , value ) ) <EOL> return flat <EOL> flatchoices = property ( _get_flatchoices ) <EOL> def save_form_data ( self , instance , data ) : <EOL> setattr ( instance , self . name , data ) <EOL> def formfield ( self , form_class = forms . CharField , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> defaults = { '<STR_LIT>' : not self . blank , <EOL> '<STR_LIT:label>' : capfirst ( self . verbose_name ) , <EOL> '<STR_LIT>' : self . help_text } <EOL> if self . has_default ( ) : <EOL> if callable ( self . default ) : <EOL> defaults [ '<STR_LIT>' ] = self . default <EOL> defaults [ '<STR_LIT>' ] = True <EOL> else : <EOL> defaults [ '<STR_LIT>' ] = self . get_default ( ) <EOL> if self . choices : <EOL> include_blank = ( self . blank or <EOL> not ( self . has_default ( ) or '<STR_LIT>' in kwargs ) ) <EOL> defaults [ '<STR_LIT>' ] = self . get_choices ( include_blank = include_blank ) <EOL> defaults [ '<STR_LIT>' ] = self . to_python <EOL> if self . null : <EOL> defaults [ '<STR_LIT>' ] = None <EOL> form_class = forms . TypedChoiceField <EOL> for k in list ( kwargs ) : <EOL> if k not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> del kwargs [ k ] <EOL> defaults . update ( kwargs ) <EOL> return form_class ( ** defaults ) <EOL> def value_from_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( obj , self . attname ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % ( self . __class__ . __module__ , self . __class__ . __name__ ) <EOL> name = getattr ( self , '<STR_LIT:name>' , None ) <EOL> if name is not None : <EOL> return '<STR_LIT>' % ( path , name ) <EOL> return '<STR_LIT>' % path <EOL> class AutoField ( Field ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> assert kwargs . get ( '<STR_LIT:primary_key>' , False ) is True , "<STR_LIT>" % self . __class__ . __name__ <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> try : <EOL> return int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def validate ( self , value , model_instance ) : <EOL> pass <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if not prepared : <EOL> value = self . get_prep_value ( value ) <EOL> value = connection . ops . validate_autopk_value ( value ) <EOL> return value <EOL> def get_prep_value ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return int ( value ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> assert not cls . _meta . has_auto_field , "<STR_LIT>" <EOL> super ( AutoField , self ) . contribute_to_class ( cls , name ) <EOL> cls . _meta . has_auto_field = True <EOL> cls . _meta . auto_field = self <EOL> def formfield ( self , ** kwargs ) : <EOL> return None <EOL> class BooleanField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> if '<STR_LIT:default>' not in kwargs and not kwargs . get ( '<STR_LIT:null>' ) : <EOL> kwargs [ '<STR_LIT:default>' ] = False <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value in ( True , False ) : <EOL> return bool ( value ) <EOL> if value in ( '<STR_LIT:t>' , '<STR_LIT:True>' , '<STR_LIT:1>' ) : <EOL> return True <EOL> if value in ( '<STR_LIT:f>' , '<STR_LIT:False>' , '<STR_LIT:0>' ) : <EOL> return False <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if value in ( '<STR_LIT:1>' , '<STR_LIT:0>' ) : <EOL> value = bool ( int ( value ) ) <EOL> return super ( BooleanField , self ) . get_prep_lookup ( lookup_type , value ) <EOL> def get_prep_value ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return bool ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> if self . choices : <EOL> include_blank = ( self . null or <EOL> not ( self . has_default ( ) or '<STR_LIT>' in kwargs ) ) <EOL> defaults = { '<STR_LIT>' : self . get_choices ( <EOL> include_blank = include_blank ) } <EOL> else : <EOL> defaults = { '<STR_LIT>' : forms . BooleanField } <EOL> defaults . update ( kwargs ) <EOL> return super ( BooleanField , self ) . formfield ( ** defaults ) <EOL> class CharField ( Field ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CharField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . validators . append ( validators . MaxLengthValidator ( self . max_length ) ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , six . string_types ) or value is None : <EOL> return value <EOL> return smart_text ( value ) <EOL> def get_prep_value ( self , value ) : <EOL> return self . to_python ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT:max_length>' : self . max_length } <EOL> defaults . update ( kwargs ) <EOL> return super ( CharField , self ) . formfield ( ** defaults ) <EOL> class CommaSeparatedIntegerField ( CharField ) : <EOL> default_validators = [ validators . validate_comma_separated_integer_list ] <EOL> description = _ ( "<STR_LIT>" ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( CommaSeparatedIntegerField , self ) . formfield ( ** defaults ) <EOL> class DateField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , auto_now = False , <EOL> auto_now_add = False , ** kwargs ) : <EOL> self . auto_now , self . auto_now_add = auto_now , auto_now_add <EOL> if auto_now or auto_now_add : <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> Field . __init__ ( self , verbose_name , name , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> if isinstance ( value , datetime . datetime ) : <EOL> if settings . USE_TZ and timezone . is_aware ( value ) : <EOL> default_timezone = timezone . get_default_timezone ( ) <EOL> value = timezone . make_naive ( value , default_timezone ) <EOL> return value . date ( ) <EOL> if isinstance ( value , datetime . date ) : <EOL> return value <EOL> try : <EOL> parsed = parse_date ( value ) <EOL> if parsed is not None : <EOL> return parsed <EOL> except ValueError : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> if self . auto_now or ( self . auto_now_add and add ) : <EOL> value = datetime . date . today ( ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> else : <EOL> return super ( DateField , self ) . pre_save ( model_instance , add ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( DateField , self ) . contribute_to_class ( cls , name ) <EOL> if not self . null : <EOL> setattr ( cls , '<STR_LIT>' % self . name , <EOL> curry ( cls . _get_next_or_previous_by_FIELD , field = self , <EOL> is_next = True ) ) <EOL> setattr ( cls , '<STR_LIT>' % self . name , <EOL> curry ( cls . _get_next_or_previous_by_FIELD , field = self , <EOL> is_next = False ) ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return int ( value ) <EOL> return super ( DateField , self ) . get_prep_lookup ( lookup_type , value ) <EOL> def get_prep_value ( self , value ) : <EOL> return self . to_python ( value ) <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if not prepared : <EOL> value = self . get_prep_value ( value ) <EOL> return connection . ops . value_to_db_date ( value ) <EOL> def value_to_string ( self , obj ) : <EOL> val = self . _get_val_from_obj ( obj ) <EOL> return '<STR_LIT>' if val is None else val . isoformat ( ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . DateField } <EOL> defaults . update ( kwargs ) <EOL> return super ( DateField , self ) . formfield ( ** defaults ) <EOL> class DateTimeField ( DateField ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value <EOL> if isinstance ( value , datetime . date ) : <EOL> value = datetime . datetime ( value . year , value . month , value . day ) <EOL> if settings . USE_TZ : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % value , <EOL> RuntimeWarning ) <EOL> default_timezone = timezone . get_default_timezone ( ) <EOL> value = timezone . make_aware ( value , default_timezone ) <EOL> return value <EOL> try : <EOL> parsed = parse_datetime ( value ) <EOL> if parsed is not None : <EOL> return parsed <EOL> except ValueError : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> try : <EOL> parsed = parse_date ( value ) <EOL> if parsed is not None : <EOL> return datetime . datetime ( parsed . year , parsed . month , parsed . day ) <EOL> except ValueError : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> if self . auto_now or ( self . auto_now_add and add ) : <EOL> value = timezone . now ( ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> else : <EOL> return super ( DateTimeField , self ) . pre_save ( model_instance , add ) <EOL> def get_prep_value ( self , value ) : <EOL> value = self . to_python ( value ) <EOL> if value is not None and settings . USE_TZ and timezone . is_naive ( value ) : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % value , <EOL> RuntimeWarning ) <EOL> default_timezone = timezone . get_default_timezone ( ) <EOL> value = timezone . make_aware ( value , default_timezone ) <EOL> return value <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if not prepared : <EOL> value = self . get_prep_value ( value ) <EOL> return connection . ops . value_to_db_datetime ( value ) <EOL> def value_to_string ( self , obj ) : <EOL> val = self . _get_val_from_obj ( obj ) <EOL> return '<STR_LIT>' if val is None else val . isoformat ( ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . DateTimeField } <EOL> defaults . update ( kwargs ) <EOL> return super ( DateTimeField , self ) . formfield ( ** defaults ) <EOL> class DecimalField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , max_digits = None , <EOL> decimal_places = None , ** kwargs ) : <EOL> self . max_digits , self . decimal_places = max_digits , decimal_places <EOL> Field . __init__ ( self , verbose_name , name , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> try : <EOL> return decimal . Decimal ( value ) <EOL> except decimal . InvalidOperation : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def _format ( self , value ) : <EOL> if isinstance ( value , six . string_types ) or value is None : <EOL> return value <EOL> else : <EOL> return self . format_number ( value ) <EOL> def format_number ( self , value ) : <EOL> """<STR_LIT>""" <EOL> from django . db . backends import util <EOL> return util . format_number ( value , self . max_digits , self . decimal_places ) <EOL> def get_db_prep_save ( self , value , connection ) : <EOL> return connection . ops . value_to_db_decimal ( self . to_python ( value ) , <EOL> self . max_digits , self . decimal_places ) <EOL> def get_prep_value ( self , value ) : <EOL> return self . to_python ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : self . max_digits , <EOL> '<STR_LIT>' : self . decimal_places , <EOL> '<STR_LIT>' : forms . DecimalField , <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( DecimalField , self ) . formfield ( ** defaults ) <EOL> class EmailField ( CharField ) : <EOL> default_validators = [ validators . validate_email ] <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT> ) <EOL> CharField . __init__ ( self , * args , ** kwargs ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : forms . EmailField , <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( EmailField , self ) . formfield ( ** defaults ) <EOL> class FilePathField ( Field ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , path = '<STR_LIT>' , match = None , <EOL> recursive = False , allow_files = True , allow_folders = False , ** kwargs ) : <EOL> self . path , self . match , self . recursive = path , match , recursive <EOL> self . allow_files , self . allow_folders = allow_files , allow_folders <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT:100> ) <EOL> Field . __init__ ( self , verbose_name , name , ** kwargs ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT:path>' : self . path , <EOL> '<STR_LIT>' : self . match , <EOL> '<STR_LIT>' : self . recursive , <EOL> '<STR_LIT>' : forms . FilePathField , <EOL> '<STR_LIT>' : self . allow_files , <EOL> '<STR_LIT>' : self . allow_folders , <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( FilePathField , self ) . formfield ( ** defaults ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> class FloatField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_prep_value ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return float ( value ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> try : <EOL> return float ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . FloatField } <EOL> defaults . update ( kwargs ) <EOL> return super ( FloatField , self ) . formfield ( ** defaults ) <EOL> class IntegerField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_prep_value ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return int ( value ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if ( ( lookup_type == '<STR_LIT>' or lookup_type == '<STR_LIT>' ) <EOL> and isinstance ( value , float ) ) : <EOL> value = math . ceil ( value ) <EOL> return super ( IntegerField , self ) . get_prep_lookup ( lookup_type , value ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> try : <EOL> return int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . IntegerField } <EOL> defaults . update ( kwargs ) <EOL> return super ( IntegerField , self ) . formfield ( ** defaults ) <EOL> class BigIntegerField ( IntegerField ) : <EOL> empty_strings_allowed = False <EOL> description = _ ( "<STR_LIT>" ) <EOL> MAX_BIGINT = <NUM_LIT> <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : - BigIntegerField . MAX_BIGINT - <NUM_LIT:1> , <EOL> '<STR_LIT>' : BigIntegerField . MAX_BIGINT } <EOL> defaults . update ( kwargs ) <EOL> return super ( BigIntegerField , self ) . formfield ( ** defaults ) <EOL> class IPAddressField ( Field ) : <EOL> empty_strings_allowed = False <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = <NUM_LIT:15> <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . IPAddressField } <EOL> defaults . update ( kwargs ) <EOL> return super ( IPAddressField , self ) . formfield ( ** defaults ) <EOL> class GenericIPAddressField ( Field ) : <EOL> empty_strings_allowed = True <EOL> description = _ ( "<STR_LIT>" ) <EOL> default_error_messages = { } <EOL> def __init__ ( self , verbose_name = None , name = None , protocol = '<STR_LIT>' , <EOL> unpack_ipv4 = False , * args , ** kwargs ) : <EOL> self . unpack_ipv4 = unpack_ipv4 <EOL> self . default_validators , invalid_error_message = validators . ip_address_validators ( protocol , unpack_ipv4 ) <EOL> self . default_error_messages [ '<STR_LIT>' ] = invalid_error_message <EOL> kwargs [ '<STR_LIT:max_length>' ] = <NUM_LIT> <EOL> Field . __init__ ( self , verbose_name , name , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value and '<STR_LIT::>' in value : <EOL> return clean_ipv6_address ( value , <EOL> self . unpack_ipv4 , self . error_messages [ '<STR_LIT>' ] ) <EOL> return value <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if not prepared : <EOL> value = self . get_prep_value ( value ) <EOL> return value or None <EOL> def get_prep_value ( self , value ) : <EOL> if value and '<STR_LIT::>' in value : <EOL> try : <EOL> return clean_ipv6_address ( value , self . unpack_ipv4 ) <EOL> except exceptions . ValidationError : <EOL> pass <EOL> return value <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . GenericIPAddressField } <EOL> defaults . update ( kwargs ) <EOL> return super ( GenericIPAddressField , self ) . formfield ( ** defaults ) <EOL> class NullBooleanField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:null>' ] = True <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> if value in ( True , False ) : <EOL> return bool ( value ) <EOL> if value in ( '<STR_LIT:None>' , ) : <EOL> return None <EOL> if value in ( '<STR_LIT:t>' , '<STR_LIT:True>' , '<STR_LIT:1>' ) : <EOL> return True <EOL> if value in ( '<STR_LIT:f>' , '<STR_LIT:False>' , '<STR_LIT:0>' ) : <EOL> return False <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if value in ( '<STR_LIT:1>' , '<STR_LIT:0>' ) : <EOL> value = bool ( int ( value ) ) <EOL> return super ( NullBooleanField , self ) . get_prep_lookup ( lookup_type , <EOL> value ) <EOL> def get_prep_value ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return bool ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : forms . NullBooleanField , <EOL> '<STR_LIT>' : not self . blank , <EOL> '<STR_LIT:label>' : capfirst ( self . verbose_name ) , <EOL> '<STR_LIT>' : self . help_text } <EOL> defaults . update ( kwargs ) <EOL> return super ( NullBooleanField , self ) . formfield ( ** defaults ) <EOL> class PositiveIntegerField ( IntegerField ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> defaults . update ( kwargs ) <EOL> return super ( PositiveIntegerField , self ) . formfield ( ** defaults ) <EOL> class PositiveSmallIntegerField ( IntegerField ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> defaults . update ( kwargs ) <EOL> return super ( PositiveSmallIntegerField , self ) . formfield ( ** defaults ) <EOL> class SlugField ( CharField ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT:50> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( SlugField , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . SlugField } <EOL> defaults . update ( kwargs ) <EOL> return super ( SlugField , self ) . formfield ( ** defaults ) <EOL> class SmallIntegerField ( IntegerField ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> class TextField ( Field ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_prep_value ( self , value ) : <EOL> if isinstance ( value , six . string_types ) or value is None : <EOL> return value <EOL> return smart_text ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . Textarea } <EOL> defaults . update ( kwargs ) <EOL> return super ( TextField , self ) . formfield ( ** defaults ) <EOL> class TimeField ( Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , auto_now = False , <EOL> auto_now_add = False , ** kwargs ) : <EOL> self . auto_now , self . auto_now_add = auto_now , auto_now_add <EOL> if auto_now or auto_now_add : <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> Field . __init__ ( self , verbose_name , name , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> if isinstance ( value , datetime . time ) : <EOL> return value <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value . time ( ) <EOL> try : <EOL> parsed = parse_time ( value ) <EOL> if parsed is not None : <EOL> return parsed <EOL> except ValueError : <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> msg = self . error_messages [ '<STR_LIT>' ] % value <EOL> raise exceptions . ValidationError ( msg ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> if self . auto_now or ( self . auto_now_add and add ) : <EOL> value = datetime . datetime . now ( ) . time ( ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> else : <EOL> return super ( TimeField , self ) . pre_save ( model_instance , add ) <EOL> def get_prep_value ( self , value ) : <EOL> return self . to_python ( value ) <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if not prepared : <EOL> value = self . get_prep_value ( value ) <EOL> return connection . ops . value_to_db_time ( value ) <EOL> def value_to_string ( self , obj ) : <EOL> val = self . _get_val_from_obj ( obj ) <EOL> return '<STR_LIT>' if val is None else val . isoformat ( ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . TimeField } <EOL> defaults . update ( kwargs ) <EOL> return super ( TimeField , self ) . formfield ( ** defaults ) <EOL> class URLField ( CharField ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT:200> ) <EOL> CharField . __init__ ( self , verbose_name , name , ** kwargs ) <EOL> self . validators . append ( validators . URLValidator ( ) ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : forms . URLField , <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( URLField , self ) . formfield ( ** defaults ) </s>
