<s> from django . utils . encoding import smart_unicode , force_unicode <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . text import capfirst <EOL> from django . utils import formats <EOL> from django . core import validators <EOL> from django . core . exceptions import ObjectDoesNotExist , ValidationError <EOL> from django . db . models . fields import BLANK_CHOICE_DASH <EOL> from django import forms <EOL> from decimal import Decimal <EOL> import datetime <EOL> import copy <EOL> from dockit . schema . serializer import PRIMITIVE_PROCESSOR <EOL> from dockit . schema . exceptions import DotPathNotFound <EOL> from dockit . schema . common import DotPathList , DotPathDict , DotPathSet , UnSet <EOL> from dockit . forms . fields import HiddenSchemaField , HiddenListField , HiddenDictField , SchemaChoiceField , PrimitiveListField , SchemaMultipleChoiceField <EOL> class NOT_PROVIDED : <EOL> pass <EOL> class BaseField ( object ) : <EOL> form_field_class = forms . CharField <EOL> form_field_choices_class = forms . ChoiceField <EOL> form_widget_class = None <EOL> default_validators = [ ] <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT:null>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT:blank>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) , <EOL> } <EOL> creation_counter = <NUM_LIT:0> <EOL> auto_creation_counter = - <NUM_LIT:1> <EOL> def __init__ ( self , verbose_name = None , name = None , blank = False , null = False , <EOL> default = NOT_PROVIDED , editable = True , <EOL> serialize = True , choices = None , help_text = '<STR_LIT>' , <EOL> validators = [ ] , <EOL> db_index = False , unique = False , <EOL> error_messages = None , auto_created = False ) : <EOL> self . verbose_name = verbose_name <EOL> self . name = name <EOL> self . blank = blank <EOL> self . null = null <EOL> self . default = default <EOL> self . editable = editable <EOL> self . serialize = serialize , <EOL> self . choices = choices <EOL> self . help_text = help_text <EOL> self . validators = validators <EOL> self . db_index = db_index <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> self . rel = None <EOL> self . flatchoices = None <EOL> self . unique = unique <EOL> if auto_created : <EOL> self . creation_counter = BaseField . auto_creation_counter <EOL> BaseField . auto_creation_counter -= <NUM_LIT:1> <EOL> else : <EOL> self . creation_counter = BaseField . creation_counter <EOL> BaseField . creation_counter += <NUM_LIT:1> <EOL> def contribute_to_class ( self , cls , name ) : <EOL> self . name = name <EOL> if not self . verbose_name : <EOL> self . verbose_name = name . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> cls . _meta . fields [ name ] = self <EOL> setattr ( cls , name , self ) <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_unicode ( self . default , strings_only = True ) <EOL> if not self . blank or self . null : <EOL> return None <EOL> return "<STR_LIT>" <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> choices = self . choices <EOL> if callable ( choices ) : <EOL> choices = choices ( ) <EOL> return first_choice + list ( choices ) <EOL> return first_choice <EOL> def get_choices_default ( self ) : <EOL> return self . get_choices ( ) <EOL> def to_primitive ( self , val ) : <EOL> """<STR_LIT>""" <EOL> return val <EOL> def to_portable_primitive ( self , val ) : <EOL> return self . to_primitive ( val ) <EOL> def to_python ( self , val , parent = None ) : <EOL> return val <EOL> def normalize_portable_primitives ( self , val , parent = None ) : <EOL> return val <EOL> def get_form_field_class ( self ) : <EOL> if self . choices : <EOL> return self . form_field_choices_class <EOL> else : <EOL> return self . form_field_class <EOL> def formfield ( self , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> default = self . get_form_field_class ( ) <EOL> form_class = kwargs . pop ( '<STR_LIT>' , default ) <EOL> defaults = self . formfield_kwargs ( ** kwargs ) <EOL> return form_class ( ** defaults ) <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : not self . blank , <EOL> '<STR_LIT:label>' : capfirst ( self . verbose_name ) , <EOL> '<STR_LIT>' : self . help_text , <EOL> '<STR_LIT>' : self . form_widget_class , } <EOL> if self . has_default ( ) : <EOL> defaults [ '<STR_LIT>' ] = self . get_default ( ) <EOL> for key , value in defaults . items ( ) : <EOL> if value is None : <EOL> del defaults [ key ] <EOL> if self . choices : <EOL> include_blank = self . blank or not ( self . has_default ( ) or '<STR_LIT>' in kwargs ) <EOL> defaults [ '<STR_LIT>' ] = self . get_choices ( include_blank = include_blank ) <EOL> for k in kwargs . keys ( ) : <EOL> if k not in ( '<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 defaults <EOL> def traverse_dot_path ( self , traverser ) : <EOL> traverser . end ( field = self ) <EOL> def is_instance ( self , value ) : <EOL> return False <EOL> def set_value ( self , parent , attr , value ) : <EOL> raise DotPathNotFound <EOL> def __deepcopy__ ( self , memodict ) : <EOL> obj = copy . copy ( self ) <EOL> memodict [ id ( self ) ] = obj <EOL> return obj <EOL> def _get_val_from_obj ( self , obj ) : <EOL> if self . name in obj . _python_data : <EOL> return obj . _python_data [ self . name ] <EOL> if self . name in obj . _primitive_data : <EOL> raw_val = obj . _primitive_data [ self . name ] <EOL> val = self . to_python ( raw_val , parent = obj ) <EOL> obj . _python_data [ self . name ] = val <EOL> return val <EOL> val = self . get_default ( ) <EOL> if self . has_default ( ) : <EOL> obj . _python_data [ self . name ] = val <EOL> return val <EOL> def value_to_string ( self , obj ) : <EOL> val = self . _get_val_from_obj ( obj ) <EOL> return self . to_primitive ( val ) <EOL> @ property <EOL> def attname ( self ) : <EOL> return self . name <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 ValidationError , 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 ValidationError ( errors ) <EOL> def validate ( self , value , obj ) : <EOL> """<STR_LIT>""" <EOL> if not self . editable : <EOL> return <EOL> if self . choices and value : <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 ValidationError ( msg ) <EOL> if value is None and not self . null : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT:null>' ] ) <EOL> if not self . blank and value in validators . EMPTY_VALUES : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT:blank>' ] ) <EOL> def clean ( self , value , obj ) : <EOL> """<STR_LIT>""" <EOL> self . validate ( value , obj ) <EOL> self . run_validators ( value ) <EOL> return value <EOL> class BaseTypedField ( BaseField ) : <EOL> coerce_function = None <EOL> def to_primitive ( self , val ) : <EOL> if isinstance ( self . coerce_function , type ) and isinstance ( val , self . coerce_function ) : <EOL> return val <EOL> if val is None : <EOL> return val <EOL> return self . coerce_function ( val ) <EOL> def is_instance ( self , val ) : <EOL> if isinstance ( self . coerce_function , type ) and isinstance ( val , self . coerce_function ) : <EOL> return True <EOL> if val is None : <EOL> return True <EOL> return False <EOL> class CharField ( BaseTypedField ) : <EOL> coerce_function = unicode <EOL> data_type = '<STR_LIT>' <EOL> class TextField ( BaseTypedField ) : <EOL> coerce_function = unicode <EOL> form_widget_class = forms . Textarea <EOL> data_type = '<STR_LIT:text>' <EOL> class IntegerField ( BaseTypedField ) : <EOL> coerce_function = int <EOL> form_field_class = forms . IntegerField <EOL> data_type = '<STR_LIT:int>' <EOL> class BigIntegerField ( BaseTypedField ) : <EOL> coerce_function = long <EOL> form_field_class = forms . IntegerField <EOL> data_type = '<STR_LIT>' <EOL> class BooleanField ( BaseTypedField ) : <EOL> coerce_function = bool <EOL> form_field_class = forms . BooleanField <EOL> data_type = '<STR_LIT:bool>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BooleanField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . blank = True <EOL> class DateField ( BaseField ) : <EOL> form_field_class = forms . DateField <EOL> data_type = '<STR_LIT:date>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . input_formats = kwargs . pop ( '<STR_LIT>' , list ( ) ) <EOL> super ( DateField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_python ( self , value , parent = None ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value . date ( ) <EOL> if isinstance ( value , datetime . date ) : <EOL> return value <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> return datetime . datetime . strptime ( value , format ) . date ( ) <EOL> except ValueError : <EOL> continue <EOL> return datetime . datetime . strptime ( value ) . date ( ) <EOL> class DateTimeField ( BaseField ) : <EOL> form_field_class = forms . DateTimeField <EOL> data_type = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . input_formats = kwargs . pop ( '<STR_LIT>' , list ( ) ) <EOL> super ( DateTimeField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_python ( self , value , parent = None ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value <EOL> if isinstance ( value , datetime . date ) : <EOL> return value <EOL> for format in self . input_formats or formats . get_format ( '<STR_LIT>' ) : <EOL> try : <EOL> return datetime . datetime . strptime ( value , format ) <EOL> except ValueError : <EOL> continue <EOL> return datetime . datetime . strptime ( value ) <EOL> class DecimalField ( BaseField ) : <EOL> form_field_class = forms . DecimalField <EOL> data_type = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . max_digits = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . decimal_places = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( DecimalField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_primitive ( self , val ) : <EOL> return str ( val ) <EOL> def to_python ( self , val , parent = None ) : <EOL> if val is None : <EOL> return None <EOL> return Decimal ( val ) <EOL> class EmailField ( CharField ) : <EOL> form_field_class = forms . EmailField <EOL> pass <EOL> class FloatField ( BaseTypedField ) : <EOL> coerce_function = float <EOL> form_field_class = forms . FloatField <EOL> data_type = '<STR_LIT:float>' <EOL> class IPAddressField ( CharField ) : <EOL> form_field_class = forms . IPAddressField <EOL> class PositiveIntegerField ( IntegerField ) : <EOL> form_field_class = forms . IntegerField <EOL> class SlugField ( CharField ) : <EOL> form_field_class = forms . SlugField <EOL> class TimeField ( BaseTypedField ) : <EOL> coerce_function = datetime . time <EOL> form_field_class = forms . TimeField <EOL> data_type = '<STR_LIT:time>' <EOL> class SchemaTypeField ( CharField ) : <EOL> form_widget_class = forms . HiddenInput <EOL> def __init__ ( self , schemas , * args , ** kwargs ) : <EOL> self . schemas = schemas <EOL> super ( SchemaTypeField , self ) . __init__ ( * args , ** kwargs ) <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 . schemas : <EOL> return first_choice + list ( zip ( self . schemas . keys ( ) , self . schemas . keys ( ) ) ) <EOL> return first_choice <EOL> def get_schema_choices ( self ) : <EOL> return self . get_choices ( ) <EOL> class SchemaField ( BaseField ) : <EOL> form_field_class = HiddenSchemaField <EOL> def __init__ ( self , schema , * args , ** kwargs ) : <EOL> from dockit . schema import Schema <EOL> assert issubclass ( schema , Schema ) , '<STR_LIT>' % repr ( schema ) <EOL> self . schema = schema <EOL> super ( SchemaField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_primitive ( self , val ) : <EOL> return self . schema . to_primitive ( val ) <EOL> def to_python ( self , val , parent = None ) : <EOL> if val is None : <EOL> return None <EOL> if not isinstance ( val , dict ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> return self . schema . to_python ( val , parent ) <EOL> def to_portable_primitive ( self , val ) : <EOL> if val is None : <EOL> return None <EOL> if self . is_instance ( val ) : <EOL> return val . to_portable_primitive ( val ) <EOL> assert isinstance ( val , dict ) <EOL> return val <EOL> def normalize_portable_primitives ( self , val , parent = None ) : <EOL> if val is not None and isinstance ( val , dict ) : <EOL> entry = self . to_python ( val , parent ) <EOL> entry . normalize_portable_primitives ( ) <EOL> return self . to_primitive ( entry ) <EOL> return val <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> return isinstance ( val , self . schema ) <EOL> def get_schema_choices ( self ) : <EOL> field_name = self . schema . _meta . typed_field <EOL> return self . schema . _meta . fields [ field_name ] . schemas . items ( ) <EOL> def traverse_dot_path ( self , traverser ) : <EOL> if traverser . remaining_paths : <EOL> name = traverser . next_part <EOL> value = traverser . current_value <EOL> next_value = field = None <EOL> schema = self . schema <EOL> if value : <EOL> try : <EOL> next_value = value [ name ] <EOL> except KeyError : <EOL> pass <EOL> schema = type ( value ) <EOL> if name in schema . _meta . fields : <EOL> field = schema . _meta . fields [ name ] <EOL> traverser . next ( field = field , value = next_value ) <EOL> else : <EOL> traverser . end ( field = self ) <EOL> def set_value ( self , parent , attr , value ) : <EOL> if value is UnSet : <EOL> del parent [ attr ] <EOL> else : <EOL> parent [ attr ] = value <EOL> class GenericSchemaField ( BaseField ) : <EOL> def __init__ ( self , field_name = '<STR_LIT>' , ** kwargs ) : <EOL> self . type_field_name = field_name <EOL> super ( GenericSchemaField , self ) . __init__ ( ** kwargs ) <EOL> def lookup_schema ( self , key ) : <EOL> raise NotImplementedError <EOL> def get_schema_type ( self , val ) : <EOL> return val . _meta . schema_key <EOL> def get_schema_choices ( self ) : <EOL> raise NotImplementedError <EOL> def set_schema_type ( self , val ) : <EOL> val [ self . type_field_name ] = self . get_schema_type ( val ) <EOL> def to_primitive ( self , val ) : <EOL> if hasattr ( val , '<STR_LIT>' ) : <EOL> self . set_schema_type ( val ) <EOL> return val . to_primitive ( val ) <EOL> return val <EOL> def to_python ( self , val , parent = None ) : <EOL> if hasattr ( val , '<STR_LIT>' ) : <EOL> return val . to_python ( val , parent ) <EOL> if not self . is_instance ( val ) : <EOL> key = val [ self . type_field_name ] <EOL> schema_cls = self . lookup_schema ( key ) <EOL> return schema_cls ( _primitive_data = val ) <EOL> return val <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> from dockit . schema . schema import Schema <EOL> return isinstance ( val , Schema ) <EOL> def traverse_dot_path ( self , traverser ) : <EOL> if traverser . remaining_paths : <EOL> name = traverser . next_part <EOL> value = traverser . current_value <EOL> next_value = field = None <EOL> if value : <EOL> try : <EOL> next_value = value [ name ] <EOL> except KeyError : <EOL> pass <EOL> if hasattr ( value , '<STR_LIT>' ) and hasattr ( value . _meta , '<STR_LIT>' ) and name in value . _meta . fields : <EOL> field = value . _meta . fields [ name ] <EOL> traverser . next ( field = field , value = next_value ) <EOL> else : <EOL> if traverser . current_value : <EOL> traverser . end ( field = SchemaField ( schema = type ( traverser . current_value ) ) ) <EOL> else : <EOL> traverser . end ( field = self ) <EOL> def set_value ( self , parent , attr , value ) : <EOL> if value is UnSet : <EOL> del parent [ attr ] <EOL> else : <EOL> parent [ attr ] = value <EOL> class TypedSchemaField ( GenericSchemaField ) : <EOL> def __init__ ( self , schemas , field_name = '<STR_LIT>' , ** kwargs ) : <EOL> self . schemas = schemas <EOL> super ( TypedSchemaField , self ) . __init__ ( field_name , ** kwargs ) <EOL> def lookup_schema ( self , key ) : <EOL> return self . schemas [ key ] <EOL> def get_schema_type ( self , val ) : <EOL> return val . _meta . schema_key <EOL> def get_schema_choices ( self ) : <EOL> return self . schemas . items ( ) <EOL> class ListField ( BaseField ) : <EOL> form_field_class = HiddenListField <EOL> def __init__ ( self , subfield = None , * args , ** kwargs ) : <EOL> self . subfield = subfield <EOL> kwargs . setdefault ( '<STR_LIT:default>' , list ) <EOL> super ( ListField , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_form_field_class ( self ) : <EOL> if not hasattr ( self . subfield , '<STR_LIT>' ) : <EOL> return PrimitiveListField <EOL> else : <EOL> return self . form_field_class <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> kwargs = super ( ListField , self ) . formfield_kwargs ( ** kwargs ) <EOL> if not hasattr ( self . subfield , '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = self . subfield . formfield ( ) <EOL> return kwargs <EOL> def to_primitive ( self , val ) : <EOL> if self . subfield : <EOL> ret = list ( ) <EOL> if val is None : <EOL> return ret <EOL> for item in val : <EOL> ret . append ( self . subfield . to_primitive ( item ) ) <EOL> return PRIMITIVE_PROCESSOR . to_primitive ( ret ) <EOL> return PRIMITIVE_PROCESSOR . to_primitive ( val ) <EOL> def to_portable_primitive ( self , val ) : <EOL> if self . subfield : <EOL> ret = list ( ) <EOL> if val is None : <EOL> return ret <EOL> for item in val : <EOL> ret . append ( self . subfield . to_portable_primitive ( item ) ) <EOL> return PRIMITIVE_PROCESSOR . to_primitive ( ret ) <EOL> return PRIMITIVE_PROCESSOR . to_primitive ( val ) <EOL> def to_python ( self , val , parent = None ) : <EOL> if self . subfield : <EOL> ret = DotPathList ( ) <EOL> if val is None : <EOL> return ret <EOL> for item in val : <EOL> if not self . subfield . is_instance ( item ) : <EOL> try : <EOL> item = self . subfield . to_python ( item ) <EOL> except ObjectDoesNotExist : <EOL> continue <EOL> ret . append ( item ) <EOL> return PRIMITIVE_PROCESSOR . to_python ( ret ) <EOL> return PRIMITIVE_PROCESSOR . to_python ( val ) <EOL> def normalize_portable_primitives ( self , val , parent = None ) : <EOL> ret = list ( ) <EOL> for entry in val : <EOL> if self . subfield : <EOL> ret . append ( self . subfield . normalize_portable_primitive ( entry ) ) <EOL> else : <EOL> ret . append ( entry ) <EOL> return ret <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> if not isinstance ( val , DotPathList ) : <EOL> return False <EOL> if self . subfield : <EOL> for item in val : <EOL> if not self . subfield . is_instance ( item ) : <EOL> return False <EOL> return True <EOL> def traverse_dot_path ( self , traverser ) : <EOL> if traverser . remaining_paths : <EOL> new_value = None <EOL> value = traverser . current_value <EOL> name = traverser . next_part <EOL> if value and name != '<STR_LIT:*>' : <EOL> try : <EOL> new_value = value [ int ( name ) ] <EOL> except ValueError : <EOL> raise DotPathNotFound ( u"<STR_LIT>" % name ) <EOL> except IndexError : <EOL> pass <EOL> elif value and name == '<STR_LIT:*>' : <EOL> new_value = value <EOL> traverser . next ( field = self . subfield , value = new_value ) <EOL> else : <EOL> traverser . end ( field = self ) <EOL> def set_value ( self , parent , attr , value ) : <EOL> index = int ( attr ) <EOL> if value is UnSet : <EOL> parent . pop ( index ) <EOL> else : <EOL> if self . subfield and not self . subfield . is_instance ( value ) : <EOL> value = self . subfield . to_python ( value ) <EOL> if index == len ( parent ) : <EOL> parent . append ( value ) <EOL> else : <EOL> parent [ index ] = value <EOL> class SetField ( ListField ) : <EOL> form_field_choices_class = forms . MultipleChoiceField <EOL> def get_form_field_class ( self ) : <EOL> if self . choices : <EOL> return self . form_field_choices_class <EOL> return ListField . get_form_field_class ( self ) <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> if self . choices : <EOL> return BaseField . formfield_kwargs ( self , ** kwargs ) <EOL> else : <EOL> return ListField . formfield_kwargs ( self , ** kwargs ) <EOL> def to_python ( self , val , parent = None ) : <EOL> if self . subfield : <EOL> ret = DotPathSet ( ) <EOL> if val is None : <EOL> return ret <EOL> for item in val : <EOL> if not self . subfield . is_instance ( item ) : <EOL> try : <EOL> item = self . subfield . to_python ( item ) <EOL> except ObjectDoesNotExist : <EOL> continue <EOL> ret . add ( item ) <EOL> return PRIMITIVE_PROCESSOR . to_python ( ret ) <EOL> return PRIMITIVE_PROCESSOR . to_python ( val ) <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> if not isinstance ( val , DotPathSet ) : <EOL> return False <EOL> if self . subfield : <EOL> for item in val : <EOL> if not self . subfield . is_instance ( item ) : <EOL> return False <EOL> return True <EOL> class DictField ( BaseField ) : <EOL> form_field_class = HiddenDictField <EOL> def __init__ ( self , key_subfield = None , value_subfield = None , ** kwargs ) : <EOL> self . key_subfield = key_subfield <EOL> self . value_subfield = value_subfield <EOL> super ( DictField , self ) . __init__ ( ** kwargs ) <EOL> def to_primitive ( self , val ) : <EOL> ret = dict ( ) <EOL> if val is None : <EOL> return ret <EOL> for key , value in val . iteritems ( ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = type ( value ) . to_primitive ( value ) <EOL> if hasattr ( key , '<STR_LIT>' ) : <EOL> key = type ( key ) . to_primitive ( key ) <EOL> ret [ key ] = value <EOL> ret = PRIMITIVE_PROCESSOR . to_primitive ( ret ) <EOL> return ret <EOL> def to_portable_primitive ( self , val ) : <EOL> ret = dict ( ) <EOL> if val is None : <EOL> return ret <EOL> for key , value in val . iteritems ( ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = type ( value ) . to_portable_primitive ( value ) <EOL> if hasattr ( key , '<STR_LIT>' ) : <EOL> key = type ( key ) . to_portable_primitive ( key ) <EOL> ret [ key ] = value <EOL> ret = PRIMITIVE_PROCESSOR . to_primitive ( ret ) <EOL> return ret <EOL> def to_python ( self , val , parent = None ) : <EOL> ret = DotPathDict ( ) <EOL> if val is None : <EOL> return ret <EOL> for key , value in val . iteritems ( ) : <EOL> if self . value_subfield : <EOL> if not self . value_subfield . is_instance ( value ) : <EOL> value = self . value_subfield . to_python ( value ) <EOL> if self . key_subfield : <EOL> if not self . key_subfield . is_instance ( key ) : <EOL> key = self . key_subfield . to_python ( key ) <EOL> ret [ key ] = value <EOL> ret = PRIMITIVE_PROCESSOR . to_python ( ret ) <EOL> return ret <EOL> def normalize_portable_primitives ( self , val , parent = None ) : <EOL> ret = dict ( ) <EOL> if val is None : <EOL> return ret <EOL> for key , value in val . iteritems ( ) : <EOL> if self . value_subfield : <EOL> value = self . value_subfield . normalize_portable_primitives ( value ) <EOL> if self . key_subfield : <EOL> key = self . key_subfield . normalize_portable_primitives ( key ) <EOL> ret [ key ] = value <EOL> return ret <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> if not isinstance ( val , DotPathDict ) : <EOL> return False <EOL> if self . value_subfield : <EOL> for item in val . itervalues ( ) : <EOL> if not self . value_subfield . is_instance ( item ) : <EOL> return False <EOL> if self . key_subfield : <EOL> for item in val . iterkeys ( ) : <EOL> if not self . key_subfield . is_instance ( item ) : <EOL> return False <EOL> return True <EOL> def traverse_dot_path ( self , traverser ) : <EOL> if traverser . remaining_paths : <EOL> value = traverser . current_value <EOL> name = traverser . next_part <EOL> new_value = None <EOL> if value and name != '<STR_LIT:*>' : <EOL> try : <EOL> new_value = value [ name ] <EOL> except KeyError : <EOL> pass <EOL> if new_value and not hasattr ( new_value , '<STR_LIT>' ) : <EOL> from dockit . schema . common import GenericDotPathObject <EOL> new_value = GenericDotPathObject ( new_value ) <EOL> traverser . next ( field = self . value_subfield , value = new_value ) <EOL> else : <EOL> traverser . end ( field = self ) <EOL> def set_value ( self , parent , attr , value ) : <EOL> if value is UnSet : <EOL> del parent [ attr ] <EOL> else : <EOL> if self . value_subfield and not self . value_subfield . is_instance ( value ) : <EOL> value = self . value_subfield . to_python ( value ) <EOL> parent [ attr ] = value <EOL> class ReferenceField ( BaseField ) : <EOL> form_field_class = SchemaChoiceField <EOL> data_type = '<STR_LIT>' <EOL> def __init__ ( self , document , * args , ** kwargs ) : <EOL> if document == '<STR_LIT>' : <EOL> self . self_reference = True <EOL> document = None <EOL> else : <EOL> self . self_reference = False <EOL> assert hasattr ( document , '<STR_LIT>' ) <EOL> assert hasattr ( document , '<STR_LIT>' ) <EOL> self . document = document <EOL> super ( ReferenceField , self ) . __init__ ( * args , ** kwargs ) <EOL> @ property <EOL> def _meta ( self ) : <EOL> return self . document . _meta <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( ReferenceField , self ) . contribute_to_class ( cls , name ) <EOL> if self . self_reference : <EOL> self . document = cls <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> return isinstance ( val , self . document ) <EOL> def to_primitive ( self , val ) : <EOL> if isinstance ( val , basestring ) : <EOL> return val <EOL> if val is None : <EOL> return val <EOL> return val . get_id ( ) <EOL> def to_portable_primitive ( self , val ) : <EOL> if isinstance ( val , basestring ) : <EOL> return val <EOL> if val is None : <EOL> return val <EOL> return val . natural_key <EOL> def to_python ( self , val , parent = None ) : <EOL> if self . self_reference : <EOL> if val is None : <EOL> return val <EOL> document = type ( parent ) <EOL> if isinstance ( val , document ) : <EOL> return val <EOL> else : <EOL> if self . is_instance ( val ) : <EOL> return val <EOL> document = self . document <EOL> try : <EOL> if isinstance ( val , dict ) : <EOL> return document . objects . get_by_natural_key ( ** val ) <EOL> return document . objects . get ( pk = val ) <EOL> except ObjectDoesNotExist : <EOL> if self . null : <EOL> return None <EOL> raise <EOL> def normalize_portable_primitives ( self , val , parent = None ) : <EOL> if isinstance ( val , dict ) : <EOL> try : <EOL> obj = self . to_python ( val , parent ) <EOL> except ObjectDoesNotExist : <EOL> return val <EOL> else : <EOL> return obj . pk <EOL> return val <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> kwargs = BaseField . formfield_kwargs ( self , ** kwargs ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . document . objects . all ( ) ) <EOL> return kwargs <EOL> def traverse_dot_path ( self , traverser ) : <EOL> if traverser . remaining_paths : <EOL> name = traverser . next_part <EOL> value = traverser . current_value <EOL> next_value = field = None <EOL> if value : <EOL> try : <EOL> next_value = value [ name ] <EOL> except KeyError : <EOL> pass <EOL> if name in self . document . _meta . fields : <EOL> field = self . document . _meta . fields [ name ] <EOL> traverser . next ( field = field , value = next_value ) <EOL> else : <EOL> traverser . end ( field = self ) <EOL> def set_value ( self , parent , attr , value ) : <EOL> parent [ attr ] = value <EOL> class DocumentSetField ( SetField ) : <EOL> form_field_choices_class = SchemaMultipleChoiceField <EOL> def __init__ ( self , document , * args , ** kwargs ) : <EOL> subfield = ReferenceField ( document , null = kwargs . pop ( '<STR_LIT:null>' , True ) ) <EOL> kwargs [ '<STR_LIT>' ] = subfield <EOL> super ( DocumentSetField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . document = document <EOL> self . queryset = document . objects . all ( ) <EOL> def get_choices ( self , include_blank = True , blank_choice = BLANK_CHOICE_DASH ) : <EOL> choices = list ( include_blank and blank_choice or [ ] ) <EOL> lst = [ ( x . pk , smart_unicode ( x ) ) for x in self . queryset ] <EOL> return choices + lst <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> kwargs = BaseField . formfield_kwargs ( self , ** kwargs ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . queryset ) <EOL> return kwargs <EOL> def get_form_field_class ( self ) : <EOL> return self . form_field_choices_class <EOL> class ModelReferenceField ( BaseField ) : <EOL> form_field_class = forms . ModelChoiceField <EOL> form_field_choices_class = forms . ModelChoiceField <EOL> data_type = '<STR_LIT>' <EOL> def __init__ ( self , model , * args , ** kwargs ) : <EOL> self . model = model <EOL> super ( ModelReferenceField , self ) . __init__ ( * args , ** kwargs ) <EOL> def is_instance ( self , val ) : <EOL> if val is None : <EOL> return True <EOL> return isinstance ( val , self . model ) <EOL> def to_primitive ( self , val ) : <EOL> if val is None : <EOL> return None <EOL> return val . pk <EOL> def to_portable_primitive ( self , val ) : <EOL> if val is None : <EOL> return None <EOL> if hasattr ( val , '<STR_LIT>' ) : <EOL> return val . natural_key ( ) <EOL> return val . pk <EOL> def to_python ( self , val , parent = None ) : <EOL> if val is None : <EOL> return None <EOL> try : <EOL> if isinstance ( val , ( tuple , list ) ) : <EOL> if hasattr ( self . model . objects , '<STR_LIT>' ) : <EOL> return self . model . objects . get_by_natural_key ( * val ) <EOL> return self . model . objects . get ( pk = val ) <EOL> except ObjectDoesNotExist : <EOL> if self . null : <EOL> return None <EOL> raise <EOL> def normalize_portable_primitives ( self , val , parent = None ) : <EOL> if isinstance ( val , ( tuple , list ) ) : <EOL> try : <EOL> obj = self . to_python ( val , parent ) <EOL> except ObjectDoesNotExist : <EOL> return val <EOL> else : <EOL> return obj . pk <EOL> return val <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> kwargs = BaseField . formfield_kwargs ( self , ** kwargs ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . model . objects ) <EOL> return kwargs <EOL> class ModelSetField ( SetField ) : <EOL> form_field_choices_class = forms . ModelMultipleChoiceField <EOL> def __init__ ( self , model , * args , ** kwargs ) : <EOL> subfield = ModelReferenceField ( model , null = kwargs . pop ( '<STR_LIT:null>' , True ) ) <EOL> kwargs [ '<STR_LIT>' ] = subfield <EOL> super ( ModelSetField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . model = model <EOL> self . queryset = model . objects . all ( ) <EOL> def get_form_field_class ( self ) : <EOL> return self . form_field_choices_class <EOL> def formfield_kwargs ( self , ** kwargs ) : <EOL> kwargs = BaseField . formfield_kwargs ( self , ** kwargs ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . model . objects ) <EOL> return kwargs <EOL> def get_choices ( self , include_blank = True , blank_choice = BLANK_CHOICE_DASH ) : <EOL> choices = list ( include_blank and blank_choice or [ ] ) <EOL> lst = [ ( x . _get_pk_val ( ) , smart_unicode ( x ) ) for x in self . queryset ] <EOL> return choices + lst </s>
