<s> import copy <EOL> import sys <EOL> from functools import update_wrapper <EOL> from itertools import izip <EOL> import django . db . models . manager <EOL> from django . conf import settings <EOL> from django . core . exceptions import ( ObjectDoesNotExist , <EOL> MultipleObjectsReturned , FieldError , ValidationError , NON_FIELD_ERRORS ) <EOL> from django . core import validators <EOL> from django . db . models . fields import AutoField , FieldDoesNotExist <EOL> from django . db . models . fields . related import ( ManyToOneRel , <EOL> OneToOneField , add_lazy_relation ) <EOL> from django . db import ( connections , router , transaction , DatabaseError , <EOL> DEFAULT_DB_ALIAS ) <EOL> from django . db . models . query import Q <EOL> from django . db . models . query_utils import DeferredAttribute <EOL> from django . db . models . deletion import Collector <EOL> from django . db . models . options import Options <EOL> from django . db . models import signals <EOL> from django . db . models . loading import register_models , get_model <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . functional import curry <EOL> from django . utils . encoding import smart_str , force_unicode <EOL> from django . utils . text import get_text_list , capfirst <EOL> class ModelBase ( type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> super_new = super ( ModelBase , cls ) . __new__ <EOL> parents = [ b for b in bases if isinstance ( b , ModelBase ) ] <EOL> if not parents : <EOL> return super_new ( cls , name , bases , attrs ) <EOL> module = attrs . pop ( '<STR_LIT>' ) <EOL> new_class = super_new ( cls , name , bases , { '<STR_LIT>' : module } ) <EOL> attr_meta = attrs . pop ( '<STR_LIT:Meta>' , None ) <EOL> abstract = getattr ( attr_meta , '<STR_LIT>' , False ) <EOL> if not attr_meta : <EOL> meta = getattr ( new_class , '<STR_LIT:Meta>' , None ) <EOL> else : <EOL> meta = attr_meta <EOL> base_meta = getattr ( new_class , '<STR_LIT>' , None ) <EOL> if getattr ( meta , '<STR_LIT>' , None ) is None : <EOL> model_module = sys . modules [ new_class . __module__ ] <EOL> kwargs = { "<STR_LIT>" : model_module . __name__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:2> ] } <EOL> else : <EOL> kwargs = { } <EOL> new_class . add_to_class ( '<STR_LIT>' , Options ( meta , ** kwargs ) ) <EOL> if not abstract : <EOL> new_class . add_to_class ( '<STR_LIT>' , subclass_exception ( '<STR_LIT>' , <EOL> tuple ( x . DoesNotExist <EOL> for x in parents if hasattr ( x , '<STR_LIT>' ) and not x . _meta . abstract ) <EOL> or ( ObjectDoesNotExist , ) , module ) ) <EOL> new_class . add_to_class ( '<STR_LIT>' , subclass_exception ( '<STR_LIT>' , <EOL> tuple ( x . MultipleObjectsReturned <EOL> for x in parents if hasattr ( x , '<STR_LIT>' ) and not x . _meta . abstract ) <EOL> or ( MultipleObjectsReturned , ) , module ) ) <EOL> if base_meta and not base_meta . abstract : <EOL> if not hasattr ( meta , '<STR_LIT>' ) : <EOL> new_class . _meta . ordering = base_meta . ordering <EOL> if not hasattr ( meta , '<STR_LIT>' ) : <EOL> new_class . _meta . get_latest_by = base_meta . get_latest_by <EOL> is_proxy = new_class . _meta . proxy <EOL> if getattr ( new_class , '<STR_LIT>' , None ) : <EOL> if not is_proxy : <EOL> new_class . _default_manager = None <EOL> new_class . _base_manager = None <EOL> else : <EOL> new_class . _default_manager = new_class . _default_manager . _copy_to_model ( new_class ) <EOL> new_class . _base_manager = new_class . _base_manager . _copy_to_model ( new_class ) <EOL> m = get_model ( new_class . _meta . app_label , name , <EOL> seed_cache = False , only_installed = False ) <EOL> if m is not None : <EOL> return m <EOL> for obj_name , obj in attrs . items ( ) : <EOL> new_class . add_to_class ( obj_name , obj ) <EOL> new_fields = new_class . _meta . local_fields + new_class . _meta . local_many_to_many + new_class . _meta . virtual_fields <EOL> field_names = set ( [ f . name for f in new_fields ] ) <EOL> if is_proxy : <EOL> base = None <EOL> for parent in [ cls for cls in parents if hasattr ( cls , '<STR_LIT>' ) ] : <EOL> if parent . _meta . abstract : <EOL> if parent . _meta . fields : <EOL> raise TypeError ( "<STR_LIT>" % name ) <EOL> else : <EOL> continue <EOL> if base is not None : <EOL> raise TypeError ( "<STR_LIT>" % name ) <EOL> else : <EOL> base = parent <EOL> if base is None : <EOL> raise TypeError ( "<STR_LIT>" % name ) <EOL> if ( new_class . _meta . local_fields or <EOL> new_class . _meta . local_many_to_many ) : <EOL> raise FieldError ( "<STR_LIT>" % name ) <EOL> new_class . _meta . setup_proxy ( base ) <EOL> new_class . _meta . concrete_model = base . _meta . concrete_model <EOL> else : <EOL> new_class . _meta . concrete_model = new_class <EOL> o2o_map = dict ( [ ( f . rel . to , f ) for f in new_class . _meta . local_fields <EOL> if isinstance ( f , OneToOneField ) ] ) <EOL> for base in parents : <EOL> original_base = base <EOL> if not hasattr ( base , '<STR_LIT>' ) : <EOL> continue <EOL> parent_fields = base . _meta . local_fields + base . _meta . local_many_to_many <EOL> for field in parent_fields : <EOL> if field . name in field_names : <EOL> raise FieldError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( field . name , name , base . __name__ ) ) <EOL> if not base . _meta . abstract : <EOL> base = base . _meta . concrete_model <EOL> if base in o2o_map : <EOL> field = o2o_map [ base ] <EOL> elif not is_proxy : <EOL> attr_name = '<STR_LIT>' % base . _meta . module_name <EOL> field = OneToOneField ( base , name = attr_name , <EOL> auto_created = True , parent_link = True ) <EOL> new_class . add_to_class ( attr_name , field ) <EOL> else : <EOL> field = None <EOL> new_class . _meta . parents [ base ] = field <EOL> else : <EOL> for field in parent_fields : <EOL> new_class . add_to_class ( field . name , copy . deepcopy ( field ) ) <EOL> new_class . _meta . parents . update ( base . _meta . parents ) <EOL> new_class . copy_managers ( base . _meta . abstract_managers ) <EOL> if is_proxy : <EOL> new_class . copy_managers ( original_base . _meta . concrete_managers ) <EOL> for field in base . _meta . virtual_fields : <EOL> if base . _meta . abstract and field . name in field_names : <EOL> raise FieldError ( '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' % ( field . name , name , base . __name__ ) ) <EOL> new_class . add_to_class ( field . name , copy . deepcopy ( field ) ) <EOL> if abstract : <EOL> attr_meta . abstract = False <EOL> new_class . Meta = attr_meta <EOL> return new_class <EOL> new_class . _prepare ( ) <EOL> register_models ( new_class . _meta . app_label , new_class ) <EOL> return get_model ( new_class . _meta . app_label , name , <EOL> seed_cache = False , only_installed = False ) <EOL> def copy_managers ( cls , base_managers ) : <EOL> base_managers . sort ( ) <EOL> for _ , mgr_name , manager in base_managers : <EOL> val = getattr ( cls , mgr_name , None ) <EOL> if not val or val is manager : <EOL> new_manager = manager . _copy_to_model ( cls ) <EOL> cls . add_to_class ( mgr_name , new_manager ) <EOL> def add_to_class ( cls , name , value ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value . contribute_to_class ( cls , name ) <EOL> else : <EOL> setattr ( cls , name , value ) <EOL> def _prepare ( cls ) : <EOL> """<STR_LIT>""" <EOL> opts = cls . _meta <EOL> opts . _prepare ( cls ) <EOL> if opts . order_with_respect_to : <EOL> cls . get_next_in_order = curry ( cls . _get_next_or_previous_in_order , is_next = True ) <EOL> cls . get_previous_in_order = curry ( cls . _get_next_or_previous_in_order , is_next = False ) <EOL> def make_foreign_order_accessors ( field , model , cls ) : <EOL> setattr ( <EOL> field . rel . to , <EOL> '<STR_LIT>' % cls . __name__ . lower ( ) , <EOL> curry ( method_get_order , cls ) <EOL> ) <EOL> setattr ( <EOL> field . rel . to , <EOL> '<STR_LIT>' % cls . __name__ . lower ( ) , <EOL> curry ( method_set_order , cls ) <EOL> ) <EOL> add_lazy_relation ( <EOL> cls , <EOL> opts . order_with_respect_to , <EOL> opts . order_with_respect_to . rel . to , <EOL> make_foreign_order_accessors <EOL> ) <EOL> if cls . __doc__ is None : <EOL> cls . __doc__ = "<STR_LIT>" % ( cls . __name__ , "<STR_LIT:U+002CU+0020>" . join ( [ f . attname for f in opts . fields ] ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . get_absolute_url = update_wrapper ( curry ( get_absolute_url , opts , cls . get_absolute_url ) , <EOL> cls . get_absolute_url ) <EOL> signals . class_prepared . send ( sender = cls ) <EOL> class ModelState ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db = None ) : <EOL> self . db = db <EOL> self . adding = True <EOL> class Model ( object ) : <EOL> __metaclass__ = ModelBase <EOL> _deferred = False <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> signals . pre_init . send ( sender = self . __class__ , args = args , kwargs = kwargs ) <EOL> self . _state = ModelState ( ) <EOL> args_len = len ( args ) <EOL> if args_len > len ( self . _meta . fields ) : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> fields_iter = iter ( self . _meta . fields ) <EOL> if not kwargs : <EOL> for val , field in izip ( args , fields_iter ) : <EOL> setattr ( self , field . attname , val ) <EOL> else : <EOL> for val , field in izip ( args , fields_iter ) : <EOL> setattr ( self , field . attname , val ) <EOL> kwargs . pop ( field . name , None ) <EOL> if isinstance ( field . rel , ManyToOneRel ) : <EOL> kwargs . pop ( field . attname , None ) <EOL> for field in fields_iter : <EOL> is_related_object = False <EOL> if ( field . attname not in kwargs and <EOL> isinstance ( self . __class__ . __dict__ . get ( field . attname ) , DeferredAttribute ) ) : <EOL> continue <EOL> if kwargs : <EOL> if isinstance ( field . rel , ManyToOneRel ) : <EOL> try : <EOL> rel_obj = kwargs . pop ( field . name ) <EOL> is_related_object = True <EOL> except KeyError : <EOL> try : <EOL> val = kwargs . pop ( field . attname ) <EOL> except KeyError : <EOL> val = field . get_default ( ) <EOL> else : <EOL> if rel_obj is None and field . null : <EOL> val = None <EOL> else : <EOL> try : <EOL> val = kwargs . pop ( field . attname ) <EOL> except KeyError : <EOL> val = field . get_default ( ) <EOL> else : <EOL> val = field . get_default ( ) <EOL> if is_related_object : <EOL> setattr ( self , field . name , rel_obj ) <EOL> else : <EOL> setattr ( self , field . attname , val ) <EOL> if kwargs : <EOL> for prop in kwargs . keys ( ) : <EOL> try : <EOL> if isinstance ( getattr ( self . __class__ , prop ) , property ) : <EOL> setattr ( self , prop , kwargs . pop ( prop ) ) <EOL> except AttributeError : <EOL> pass <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" % kwargs . keys ( ) [ <NUM_LIT:0> ] ) <EOL> super ( Model , self ) . __init__ ( ) <EOL> signals . post_init . send ( sender = self . __class__ , instance = self ) <EOL> def __repr__ ( self ) : <EOL> try : <EOL> u = unicode ( self ) <EOL> except ( UnicodeEncodeError , UnicodeDecodeError ) : <EOL> u = '<STR_LIT>' <EOL> return smart_str ( u'<STR_LIT>' % ( self . __class__ . __name__ , u ) ) <EOL> def __str__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return force_unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return '<STR_LIT>' % self . __class__ . __name__ <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , self . __class__ ) and self . _get_pk_val ( ) == other . _get_pk_val ( ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _get_pk_val ( ) ) <EOL> def __reduce__ ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . __dict__ <EOL> model = self . __class__ <EOL> defers = [ ] <EOL> pk_val = None <EOL> if self . _deferred : <EOL> from django . db . models . query_utils import deferred_class_factory <EOL> factory = deferred_class_factory <EOL> for field in self . _meta . fields : <EOL> if isinstance ( self . __class__ . __dict__ . get ( field . attname ) , <EOL> DeferredAttribute ) : <EOL> defers . append ( field . attname ) <EOL> if pk_val is None : <EOL> obj = self . __class__ . __dict__ [ field . attname ] <EOL> model = obj . model_ref ( ) <EOL> else : <EOL> factory = simple_class_factory <EOL> return ( model_unpickle , ( model , defers , factory ) , data ) <EOL> def _get_pk_val ( self , meta = None ) : <EOL> if not meta : <EOL> meta = self . _meta <EOL> return getattr ( self , meta . pk . attname ) <EOL> def _set_pk_val ( self , value ) : <EOL> return setattr ( self , self . _meta . pk . attname , value ) <EOL> pk = property ( _get_pk_val , _set_pk_val ) <EOL> def serializable_value ( self , field_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> field = self . _meta . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] <EOL> except FieldDoesNotExist : <EOL> return getattr ( self , field_name ) <EOL> return getattr ( self , field . attname ) <EOL> def save ( self , force_insert = False , force_update = False , using = None ) : <EOL> """<STR_LIT>""" <EOL> if force_insert and force_update : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . save_base ( using = using , force_insert = force_insert , force_update = force_update ) <EOL> save . alters_data = True <EOL> def save_base ( self , raw = False , cls = None , origin = None , force_insert = False , <EOL> force_update = False , using = None ) : <EOL> """<STR_LIT>""" <EOL> using = using or router . db_for_write ( self . __class__ , instance = self ) <EOL> assert not ( force_insert and force_update ) <EOL> if cls is None : <EOL> cls = self . __class__ <EOL> meta = cls . _meta <EOL> if not meta . proxy : <EOL> origin = cls <EOL> else : <EOL> meta = cls . _meta <EOL> if origin and not meta . auto_created : <EOL> signals . pre_save . send ( sender = origin , instance = self , raw = raw , using = using ) <EOL> if not raw or meta . proxy : <EOL> if meta . proxy : <EOL> org = cls <EOL> else : <EOL> org = None <EOL> for parent , field in meta . parents . items ( ) : <EOL> if field and getattr ( self , parent . _meta . pk . attname ) is None and getattr ( self , field . attname ) is not None : <EOL> setattr ( self , parent . _meta . pk . attname , getattr ( self , field . attname ) ) <EOL> self . save_base ( cls = parent , origin = org , using = using ) <EOL> if field : <EOL> setattr ( self , field . attname , self . _get_pk_val ( parent . _meta ) ) <EOL> if meta . proxy : <EOL> return <EOL> if not meta . proxy : <EOL> non_pks = [ f for f in meta . local_fields if not f . primary_key ] <EOL> pk_val = self . _get_pk_val ( meta ) <EOL> pk_set = pk_val is not None <EOL> record_exists = True <EOL> manager = cls . _base_manager <EOL> if pk_set : <EOL> if ( force_update or ( not force_insert and <EOL> manager . using ( using ) . filter ( pk = pk_val ) . exists ( ) ) ) : <EOL> if force_update or non_pks : <EOL> values = [ ( f , None , ( raw and getattr ( self , f . attname ) or f . pre_save ( self , False ) ) ) for f in non_pks ] <EOL> if values : <EOL> rows = manager . using ( using ) . filter ( pk = pk_val ) . _update ( values ) <EOL> if force_update and not rows : <EOL> raise DatabaseError ( "<STR_LIT>" ) <EOL> else : <EOL> record_exists = False <EOL> if not pk_set or not record_exists : <EOL> if meta . order_with_respect_to : <EOL> field = meta . order_with_respect_to <EOL> order_value = manager . using ( using ) . filter ( ** { field . name : getattr ( self , field . attname ) } ) . count ( ) <EOL> self . _order = order_value <EOL> fields = meta . local_fields <EOL> if not pk_set : <EOL> if force_update : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> fields = [ f for f in fields if not isinstance ( f , AutoField ) ] <EOL> record_exists = False <EOL> update_pk = bool ( meta . has_auto_field and not pk_set ) <EOL> result = manager . _insert ( [ self ] , fields = fields , return_id = update_pk , using = using , raw = raw ) <EOL> if update_pk : <EOL> setattr ( self , meta . pk . attname , result ) <EOL> transaction . commit_unless_managed ( using = using ) <EOL> self . _state . db = using <EOL> self . _state . adding = False <EOL> if origin and not meta . auto_created : <EOL> signals . post_save . send ( sender = origin , instance = self , <EOL> created = ( not record_exists ) , raw = raw , using = using ) <EOL> save_base . alters_data = True <EOL> def delete ( self , using = None ) : <EOL> using = using or router . db_for_write ( self . __class__ , instance = self ) <EOL> assert self . _get_pk_val ( ) is not None , "<STR_LIT>" % ( self . _meta . object_name , self . _meta . pk . attname ) <EOL> collector = Collector ( using = using ) <EOL> collector . collect ( [ self ] ) <EOL> collector . delete ( ) <EOL> delete . alters_data = True <EOL> def _get_FIELD_display ( self , field ) : <EOL> value = getattr ( self , field . attname ) <EOL> return force_unicode ( dict ( field . flatchoices ) . get ( value , value ) , strings_only = True ) <EOL> def _get_next_or_previous_by_FIELD ( self , field , is_next , ** kwargs ) : <EOL> if not self . pk : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> op = is_next and '<STR_LIT>' or '<STR_LIT>' <EOL> order = not is_next and '<STR_LIT:->' or '<STR_LIT>' <EOL> param = smart_str ( getattr ( self , field . attname ) ) <EOL> q = Q ( ** { '<STR_LIT>' % ( field . name , op ) : param } ) <EOL> q = q | Q ( ** { field . name : param , '<STR_LIT>' % op : self . pk } ) <EOL> qs = self . __class__ . _default_manager . using ( self . _state . db ) . filter ( ** kwargs ) . filter ( q ) . order_by ( '<STR_LIT>' % ( order , field . name ) , '<STR_LIT>' % order ) <EOL> try : <EOL> return qs [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise self . DoesNotExist ( "<STR_LIT>" % self . __class__ . _meta . object_name ) <EOL> def _get_next_or_previous_in_order ( self , is_next ) : <EOL> cachename = "<STR_LIT>" % is_next <EOL> if not hasattr ( self , cachename ) : <EOL> op = is_next and '<STR_LIT>' or '<STR_LIT>' <EOL> order = not is_next and '<STR_LIT>' or '<STR_LIT>' <EOL> order_field = self . _meta . order_with_respect_to <EOL> obj = self . _default_manager . filter ( ** { <EOL> order_field . name : getattr ( self , order_field . attname ) <EOL> } ) . filter ( ** { <EOL> '<STR_LIT>' % op : self . _default_manager . values ( '<STR_LIT>' ) . filter ( ** { <EOL> self . _meta . pk . name : self . pk <EOL> } ) <EOL> } ) . order_by ( order ) [ : <NUM_LIT:1> ] . get ( ) <EOL> setattr ( self , cachename , obj ) <EOL> return getattr ( self , cachename ) <EOL> def prepare_database_save ( self , unused ) : <EOL> return self . pk <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def validate_unique ( self , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> unique_checks , date_checks = self . _get_unique_checks ( exclude = exclude ) <EOL> errors = self . _perform_unique_checks ( unique_checks ) <EOL> date_errors = self . _perform_date_checks ( date_checks ) <EOL> for k , v in date_errors . items ( ) : <EOL> errors . setdefault ( k , [ ] ) . extend ( v ) <EOL> if errors : <EOL> raise ValidationError ( errors ) <EOL> def _get_unique_checks ( self , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> if exclude is None : <EOL> exclude = [ ] <EOL> unique_checks = [ ] <EOL> unique_togethers = [ ( self . __class__ , self . _meta . unique_together ) ] <EOL> for parent_class in self . _meta . parents . keys ( ) : <EOL> if parent_class . _meta . unique_together : <EOL> unique_togethers . append ( ( parent_class , parent_class . _meta . unique_together ) ) <EOL> for model_class , unique_together in unique_togethers : <EOL> for check in unique_together : <EOL> for name in check : <EOL> if name in exclude : <EOL> break <EOL> else : <EOL> unique_checks . append ( ( model_class , tuple ( check ) ) ) <EOL> date_checks = [ ] <EOL> fields_with_class = [ ( self . __class__ , self . _meta . local_fields ) ] <EOL> for parent_class in self . _meta . parents . keys ( ) : <EOL> fields_with_class . append ( ( parent_class , parent_class . _meta . local_fields ) ) <EOL> for model_class , fields in fields_with_class : <EOL> for f in fields : <EOL> name = f . name <EOL> if name in exclude : <EOL> continue <EOL> if f . unique : <EOL> unique_checks . append ( ( model_class , ( name , ) ) ) <EOL> if f . unique_for_date and f . unique_for_date not in exclude : <EOL> date_checks . append ( ( model_class , '<STR_LIT:date>' , name , f . unique_for_date ) ) <EOL> if f . unique_for_year and f . unique_for_year not in exclude : <EOL> date_checks . append ( ( model_class , '<STR_LIT>' , name , f . unique_for_year ) ) <EOL> if f . unique_for_month and f . unique_for_month not in exclude : <EOL> date_checks . append ( ( model_class , '<STR_LIT>' , name , f . unique_for_month ) ) <EOL> return unique_checks , date_checks <EOL> def _perform_unique_checks ( self , unique_checks ) : <EOL> errors = { } <EOL> for model_class , unique_check in unique_checks : <EOL> lookup_kwargs = { } <EOL> for field_name in unique_check : <EOL> f = self . _meta . get_field ( field_name ) <EOL> lookup_value = getattr ( self , f . attname ) <EOL> if lookup_value is None : <EOL> continue <EOL> if f . primary_key and not self . _state . adding : <EOL> continue <EOL> lookup_kwargs [ str ( field_name ) ] = lookup_value <EOL> if len ( unique_check ) != len ( lookup_kwargs . keys ( ) ) : <EOL> continue <EOL> qs = model_class . _default_manager . filter ( ** lookup_kwargs ) <EOL> if not self . _state . adding and self . pk is not None : <EOL> qs = qs . exclude ( pk = self . pk ) <EOL> if qs . exists ( ) : <EOL> if len ( unique_check ) == <NUM_LIT:1> : <EOL> key = unique_check [ <NUM_LIT:0> ] <EOL> else : <EOL> key = NON_FIELD_ERRORS <EOL> errors . setdefault ( key , [ ] ) . append ( self . unique_error_message ( model_class , unique_check ) ) <EOL> return errors <EOL> def _perform_date_checks ( self , date_checks ) : <EOL> errors = { } <EOL> for model_class , lookup_type , field , unique_for in date_checks : <EOL> lookup_kwargs = { } <EOL> date = getattr ( self , unique_for ) <EOL> if date is None : <EOL> continue <EOL> if lookup_type == '<STR_LIT:date>' : <EOL> lookup_kwargs [ '<STR_LIT>' % unique_for ] = date . day <EOL> lookup_kwargs [ '<STR_LIT>' % unique_for ] = date . month <EOL> lookup_kwargs [ '<STR_LIT>' % unique_for ] = date . year <EOL> else : <EOL> lookup_kwargs [ '<STR_LIT>' % ( unique_for , lookup_type ) ] = getattr ( date , lookup_type ) <EOL> lookup_kwargs [ field ] = getattr ( self , field ) <EOL> qs = model_class . _default_manager . filter ( ** lookup_kwargs ) <EOL> if not self . _state . adding and self . pk is not None : <EOL> qs = qs . exclude ( pk = self . pk ) <EOL> if qs . exists ( ) : <EOL> errors . setdefault ( field , [ ] ) . append ( <EOL> self . date_error_message ( lookup_type , field , unique_for ) <EOL> ) <EOL> return errors <EOL> def date_error_message ( self , lookup_type , field , unique_for ) : <EOL> opts = self . _meta <EOL> return _ ( u"<STR_LIT>" ) % { <EOL> '<STR_LIT>' : unicode ( capfirst ( opts . get_field ( field ) . verbose_name ) ) , <EOL> '<STR_LIT>' : unicode ( capfirst ( opts . get_field ( unique_for ) . verbose_name ) ) , <EOL> '<STR_LIT>' : lookup_type , <EOL> } <EOL> def unique_error_message ( self , model_class , unique_check ) : <EOL> opts = model_class . _meta <EOL> model_name = capfirst ( opts . verbose_name ) <EOL> if len ( unique_check ) == <NUM_LIT:1> : <EOL> field_name = unique_check [ <NUM_LIT:0> ] <EOL> field = opts . get_field ( field_name ) <EOL> field_label = capfirst ( field . verbose_name ) <EOL> return field . error_messages [ '<STR_LIT>' ] % { <EOL> '<STR_LIT>' : unicode ( model_name ) , <EOL> '<STR_LIT>' : unicode ( field_label ) <EOL> } <EOL> else : <EOL> field_labels = map ( lambda f : capfirst ( opts . get_field ( f ) . verbose_name ) , unique_check ) <EOL> field_labels = get_text_list ( field_labels , _ ( '<STR_LIT>' ) ) <EOL> return _ ( u"<STR_LIT>" ) % { <EOL> '<STR_LIT>' : unicode ( model_name ) , <EOL> '<STR_LIT>' : unicode ( field_labels ) <EOL> } <EOL> def full_clean ( self , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> errors = { } <EOL> if exclude is None : <EOL> exclude = [ ] <EOL> try : <EOL> self . clean_fields ( exclude = exclude ) <EOL> except ValidationError , e : <EOL> errors = e . update_error_dict ( errors ) <EOL> try : <EOL> self . clean ( ) <EOL> except ValidationError , e : <EOL> errors = e . update_error_dict ( errors ) <EOL> for name in errors . keys ( ) : <EOL> if name != NON_FIELD_ERRORS and name not in exclude : <EOL> exclude . append ( name ) <EOL> try : <EOL> self . validate_unique ( exclude = exclude ) <EOL> except ValidationError , e : <EOL> errors = e . update_error_dict ( errors ) <EOL> if errors : <EOL> raise ValidationError ( errors ) <EOL> def clean_fields ( self , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> if exclude is None : <EOL> exclude = [ ] <EOL> errors = { } <EOL> for f in self . _meta . fields : <EOL> if f . name in exclude : <EOL> continue <EOL> raw_value = getattr ( self , f . attname ) <EOL> if f . blank and raw_value in validators . EMPTY_VALUES : <EOL> continue <EOL> try : <EOL> setattr ( self , f . attname , f . clean ( raw_value , self ) ) <EOL> except ValidationError , e : <EOL> errors [ f . name ] = e . messages <EOL> if errors : <EOL> raise ValidationError ( errors ) <EOL> def method_set_order ( ordered_obj , self , id_list , using = None ) : <EOL> if using is None : <EOL> using = DEFAULT_DB_ALIAS <EOL> rel_val = getattr ( self , ordered_obj . _meta . order_with_respect_to . rel . field_name ) <EOL> order_name = ordered_obj . _meta . order_with_respect_to . name <EOL> for i , j in enumerate ( id_list ) : <EOL> ordered_obj . objects . filter ( ** { '<STR_LIT>' : j , order_name : rel_val } ) . update ( _order = i ) <EOL> transaction . commit_unless_managed ( using = using ) <EOL> def method_get_order ( ordered_obj , self ) : <EOL> rel_val = getattr ( self , ordered_obj . _meta . order_with_respect_to . rel . field_name ) <EOL> order_name = ordered_obj . _meta . order_with_respect_to . name <EOL> pk_name = ordered_obj . _meta . pk . name <EOL> return [ r [ pk_name ] for r in <EOL> ordered_obj . objects . filter ( ** { order_name : rel_val } ) . values ( pk_name ) ] <EOL> def get_absolute_url ( opts , func , self , * args , ** kwargs ) : <EOL> return settings . ABSOLUTE_URL_OVERRIDES . get ( '<STR_LIT>' % ( opts . app_label , opts . module_name ) , func ) ( self , * args , ** kwargs ) <EOL> class Empty ( object ) : <EOL> pass <EOL> def simple_class_factory ( model , attrs ) : <EOL> """<STR_LIT>""" <EOL> return model <EOL> def model_unpickle ( model , attrs , factory ) : <EOL> """<STR_LIT>""" <EOL> cls = factory ( model , attrs ) <EOL> return cls . __new__ ( cls ) <EOL> model_unpickle . __safe_for_unpickle__ = True <EOL> def subclass_exception ( name , parents , module ) : <EOL> return type ( name , parents , { '<STR_LIT>' : module } ) </s>
