<s> from __future__ import unicode_literals <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import ValidationError <EOL> from django . db . models import deletion , fields <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from picklefield . fields import PickledObjectField <EOL> from ... compat import get_remote_field <EOL> from ... db . fields import PythonIdentifierField <EOL> from ... db . models import MutableModel <EOL> from ... models import FieldDefinition , FieldDefinitionManager , ModelDefinition <EOL> from . managers import ForeignKeyDefinitionManager <EOL> related_name_help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class RelatedFieldDefinition ( FieldDefinition ) : <EOL> to = fields . related . ForeignKey ( <EOL> ContentType , on_delete = deletion . CASCADE , verbose_name = _ ( '<STR_LIT:to>' ) , related_name = '<STR_LIT:+>' <EOL> ) <EOL> related_name = PythonIdentifierField ( _ ( '<STR_LIT>' ) , <EOL> blank = True , null = True , <EOL> help_text = related_name_help_text ) <EOL> objects = FieldDefinitionManager ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> abstract = True <EOL> defined_field_options = ( '<STR_LIT:related_name>' , ) <EOL> defined_field_category = _ ( '<STR_LIT>' ) <EOL> def clone ( self ) : <EOL> clone = super ( RelatedFieldDefinition , self ) . clone ( ) <EOL> clone . to = self . to <EOL> return clone <EOL> @ property <EOL> def is_recursive_relationship ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> model_def = self . model_def <EOL> except ModelDefinition . DoesNotExist : <EOL> return False <EOL> else : <EOL> return self . to_id == model_def . contenttype_ptr_id <EOL> @ property <EOL> def to_model_class_is_mutable ( self ) : <EOL> to_model_class = self . to . model_class ( ) <EOL> if to_model_class is None : <EOL> try : <EOL> getattr ( self . to , '<STR_LIT>' ) <EOL> except ModelDefinition . DoesNotExist : <EOL> return False <EOL> else : <EOL> return True <EOL> else : <EOL> return issubclass ( to_model_class , MutableModel ) <EOL> @ property <EOL> def to_model_class ( self ) : <EOL> if self . to_model_class_is_mutable : <EOL> return self . to . modeldefinition . model_class ( ) <EOL> else : <EOL> return self . to . model_class ( ) <EOL> def clean ( self ) : <EOL> if ( None not in ( self . related_name , self . to_id ) and <EOL> not self . to_model_class_is_mutable ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise ValidationError ( { '<STR_LIT:related_name>' : [ msg ] } ) <EOL> def get_field_options ( self , ** overrides ) : <EOL> options = super ( RelatedFieldDefinition , self ) . get_field_options ( ** overrides ) <EOL> if self . to_model_class_is_mutable : <EOL> if self . is_recursive_relationship : <EOL> options [ '<STR_LIT:to>' ] = fields . related . RECURSIVE_RELATIONSHIP_CONSTANT <EOL> else : <EOL> model_def = self . to . modeldefinition <EOL> options [ '<STR_LIT:to>' ] = "<STR_LIT>" % ( <EOL> model_def . app_label , model_def . object_name <EOL> ) <EOL> else : <EOL> opts = self . to . _meta <EOL> options . update ( <EOL> to = "<STR_LIT>" % ( opts . app_label , opts . object_name ) , <EOL> related_name = '<STR_LIT:+>' <EOL> ) <EOL> return options <EOL> def construct_for_migrate ( self ) : <EOL> return self . construct ( to = self . to . model_class ( ) ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> save = super ( RelatedFieldDefinition , self ) . save ( ) <EOL> if self . to_model_class_is_mutable : <EOL> self . to_model_class . mark_as_obsolete ( ) <EOL> return save <EOL> to_field_help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> on_delete_help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class SET ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> self . callable = callable ( self . value ) <EOL> def __call__ ( self , collector , field , sub_objs , using ) : <EOL> value = self . value <EOL> if self . callable : <EOL> value = value ( ) <EOL> collector . add_field_update ( field , value , sub_objs ) <EOL> def deconstruct ( self ) : <EOL> return ( "<STR_LIT>" % ( self . __module__ , self . __name__ ) , ( self . value , ) , { } ) <EOL> SET_NULL = SET ( None ) <EOL> class ForeignKeyDefinition ( RelatedFieldDefinition ) : <EOL> ON_DELETE_CASCADE = '<STR_LIT>' <EOL> ON_DELETE_PROTECT = '<STR_LIT>' <EOL> ON_DELETE_SET_NULL = '<STR_LIT>' <EOL> ON_DELETE_SET_DEFAULT = '<STR_LIT>' <EOL> ON_DELETE_SET_VALUE = '<STR_LIT>' <EOL> ON_DELETE_DO_NOTHING = '<STR_LIT>' <EOL> ON_DELETE_CHOICES = ( <EOL> ( ON_DELETE_CASCADE , _ ( '<STR_LIT>' ) ) , <EOL> ( ON_DELETE_PROTECT , _ ( '<STR_LIT>' ) ) , <EOL> ( ON_DELETE_SET_NULL , _ ( '<STR_LIT>' ) ) , <EOL> ( ON_DELETE_SET_DEFAULT , _ ( '<STR_LIT>' ) ) , <EOL> ( ON_DELETE_SET_VALUE , _ ( '<STR_LIT>' ) ) , <EOL> ( ON_DELETE_DO_NOTHING , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> to_field = PythonIdentifierField ( _ ( '<STR_LIT>' ) , blank = True , null = True , <EOL> help_text = to_field_help_text ) <EOL> one_to_one = fields . BooleanField ( editable = False , default = False ) <EOL> on_delete = fields . CharField ( _ ( '<STR_LIT>' ) , blank = True , null = True , <EOL> max_length = <NUM_LIT:11> , choices = ON_DELETE_CHOICES , <EOL> default = ON_DELETE_CASCADE , <EOL> help_text = on_delete_help_text ) <EOL> on_delete_set_value = PickledObjectField ( _ ( '<STR_LIT>' ) , null = True ) <EOL> objects = ForeignKeyDefinitionManager ( one_to_one = False ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> defined_field_class = fields . related . ForeignKey <EOL> defined_field_options = ( '<STR_LIT>' , ) <EOL> def clean ( self ) : <EOL> try : <EOL> super ( ForeignKeyDefinition , self ) . clean ( ) <EOL> except ValidationError as e : <EOL> messages = e . message_dict <EOL> else : <EOL> messages = { } <EOL> if self . on_delete == self . ON_DELETE_SET_NULL : <EOL> if not self . null : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> messages [ '<STR_LIT>' ] = [ msg ] <EOL> elif ( self . on_delete == self . ON_DELETE_SET_DEFAULT and <EOL> self . default == fields . NOT_PROVIDED ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> messages [ '<STR_LIT>' ] = [ msg ] <EOL> elif ( self . on_delete == self . ON_DELETE_SET_VALUE and <EOL> self . on_delete_set_value is None ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> messages [ '<STR_LIT>' ] = [ msg ] <EOL> if messages : <EOL> raise ValidationError ( messages ) <EOL> def get_field_options ( self , ** overrides ) : <EOL> options = super ( ForeignKeyDefinition , self ) . get_field_options ( ** overrides ) <EOL> if self . on_delete == self . ON_DELETE_SET_VALUE : <EOL> on_delete = SET ( self . on_delete_set_value ) <EOL> elif self . on_delete == self . ON_DELETE_SET_NULL : <EOL> on_delete = SET_NULL <EOL> else : <EOL> on_delete = getattr ( deletion , self . on_delete , None ) <EOL> options [ '<STR_LIT>' ] = on_delete <EOL> return options <EOL> class OneToOneFieldDefinition ( ForeignKeyDefinition ) : <EOL> objects = ForeignKeyDefinitionManager ( one_to_one = True ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> proxy = True <EOL> defined_field_class = fields . related . OneToOneField <EOL> def save ( self , * args , ** kwargs ) : <EOL> self . one_to_one = True <EOL> return super ( OneToOneFieldDefinition , self ) . save ( * args , ** kwargs ) <EOL> through_help_text = _ ( '<STR_LIT>' ) <EOL> db_table_help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class ManyToManyFieldDefinition ( RelatedFieldDefinition ) : <EOL> symmetrical = fields . NullBooleanField ( _ ( '<STR_LIT>' ) ) <EOL> through = fields . related . ForeignKey ( <EOL> ContentType , on_delete = deletion . CASCADE , blank = True , null = True , related_name = '<STR_LIT:+>' , help_text = through_help_text <EOL> ) <EOL> db_table = fields . SlugField ( max_length = <NUM_LIT:30> , blank = True , null = True , <EOL> help_text = db_table_help_text ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> defined_field_class = fields . related . ManyToManyField <EOL> defined_field_options = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def clean ( self ) : <EOL> try : <EOL> super ( ManyToManyFieldDefinition , self ) . clean ( ) <EOL> except ValidationError as e : <EOL> messages = e . message_dict <EOL> else : <EOL> messages = { } <EOL> if ( self . symmetrical is not None and <EOL> not self . is_recursive_relationship ) : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> messages [ '<STR_LIT>' ] = [ msg ] <EOL> if self . through : <EOL> if self . db_table : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> messages [ '<STR_LIT>' ] = [ msg ] <EOL> if self . symmetrical : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> messages . setdefault ( '<STR_LIT>' , [ ] ) . append ( msg ) <EOL> seen_from , seen_to = <NUM_LIT:0> , <NUM_LIT:0> <EOL> to_model = self . to . model_class ( ) <EOL> through_class = self . through . model_class ( ) <EOL> from_model = self . model_def . cached_model <EOL> for field in through_class . _meta . fields : <EOL> rel_to = getattr ( get_remote_field ( field ) , '<STR_LIT:to>' , None ) <EOL> if rel_to == from_model : <EOL> seen_from += <NUM_LIT:1> <EOL> elif rel_to == to_model : <EOL> seen_to += <NUM_LIT:1> <EOL> if self . is_recursive_relationship ( ) : <EOL> if seen_from > <NUM_LIT:2> : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> formated_msg = msg % ( through_class . _meta . object_name , <EOL> from_model . _meta . object_name ) <EOL> messages . setdefault ( '<STR_LIT>' , [ ] ) . append ( formated_msg ) <EOL> else : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if seen_from > <NUM_LIT:1> : <EOL> formated_msg = msg % ( through_class . _meta . object_name , <EOL> from_model . _meta . object_name ) <EOL> messages . setdefault ( '<STR_LIT>' , [ ] ) . append ( formated_msg ) <EOL> if seen_to > <NUM_LIT:1> : <EOL> formated_msg = msg % ( through_class . _meta . object_name , <EOL> to_model . _meta . object_name ) <EOL> messages . setdefault ( '<STR_LIT>' , [ ] ) . append ( formated_msg ) <EOL> if messages : <EOL> raise ValidationError ( messages ) <EOL> def get_field_options ( self , ** overrides ) : <EOL> options = super ( ManyToManyFieldDefinition , self ) . get_field_options ( ** overrides ) <EOL> if self . through : <EOL> options [ '<STR_LIT>' ] = self . through . model_class ( ) <EOL> return options <EOL> def get_bound_field ( self ) : <EOL> opts = self . model_def . model_class ( ) . _meta <EOL> for field in opts . many_to_many : <EOL> if field . name == self . _saved_name : <EOL> return field </s>
