<s> from __future__ import unicode_literals <EOL> from django . core . exceptions import ValidationError <EOL> from django . db import models <EOL> from django . db . migrations . state import ModelState <EOL> from django . utils . six import string_types <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . . import logger <EOL> from . . compat import get_remote_field_model , StateApps <EOL> from . . state import handler as state_handler <EOL> class MutableModel ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> abstract = True <EOL> @ classmethod <EOL> def definition ( cls ) : <EOL> definition_cls , definition_pk = cls . _definition <EOL> return definition_cls . objects . get ( pk = definition_pk ) <EOL> @ classmethod <EOL> def checksum ( cls ) : <EOL> return cls . _checksum <EOL> @ classmethod <EOL> def is_obsolete ( cls ) : <EOL> return ( <EOL> cls . _is_obsolete or <EOL> cls . _checksum != state_handler . get_checksum ( cls . _definition [ <NUM_LIT:1> ] ) <EOL> ) <EOL> @ classmethod <EOL> def get_model_state ( cls , ** kwargs ) : <EOL> return ModelState . from_model ( cls , ** kwargs ) <EOL> @ classmethod <EOL> def get_related_model_states ( cls , model_state ) : <EOL> model_states = { } <EOL> for _name , field in model_state . fields : <EOL> related_model_reference = get_remote_field_model ( field ) <EOL> if related_model_reference : <EOL> related_model = cls . _meta . apps . get_model ( related_model_reference ) <EOL> if issubclass ( related_model , MutableModel ) : <EOL> related_model_state = related_model . get_model_state ( exclude_rels = True ) <EOL> else : <EOL> related_model_state = ModelState . from_model ( related_model , exclude_rels = True ) <EOL> model_states [ related_model_state . app_label , related_model_state . name ] = related_model_state <EOL> for base in related_model_state . bases : <EOL> if isinstance ( base , string_types ) : <EOL> base_model = cls . _meta . apps . get_model ( base ) <EOL> if issubclass ( base_model , MutableModel ) : <EOL> base_model_state = base_model . get_model_state ( exclude_rels = True ) <EOL> else : <EOL> base_model_state = ModelState . from_model ( base_model , exclude_rels = True ) <EOL> model_states [ base_model_state . app_label , base_model_state . name ] = base_model_state <EOL> return list ( model_states . values ( ) ) <EOL> @ classmethod <EOL> def render_state ( cls ) : <EOL> state = cls . get_model_state ( ) <EOL> related_states = cls . get_related_model_states ( state ) <EOL> apps = StateApps ( [ ] , { } ) <EOL> apps . render_multiple ( related_states + [ state ] ) <EOL> return apps . all_models [ state . app_label ] [ state . name . lower ( ) ] <EOL> @ classmethod <EOL> def mark_as_obsolete ( cls , origin = None ) : <EOL> cls . _is_obsolete = True <EOL> logger . debug ( <EOL> "<STR_LIT>" , <EOL> cls , cls . _dependencies <EOL> ) <EOL> if origin is None : <EOL> origin = cls . _definition <EOL> for definition_cls , definition_pk in cls . _dependencies : <EOL> if ( definition_cls , definition_pk ) == origin : <EOL> continue <EOL> try : <EOL> definition = definition_cls . objects . get ( pk = definition_pk ) <EOL> except definition_cls . DoesNotExist : <EOL> pass <EOL> else : <EOL> definition . model_class ( ) . mark_as_obsolete ( origin ) <EOL> def clean ( self ) : <EOL> if self . is_obsolete ( ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> return super ( MutableModel , self ) . clean ( ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> if self . is_obsolete ( ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise ValidationError ( msg ) <EOL> return super ( MutableModel , self ) . save ( * args , ** kwargs ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> if self . is_obsolete ( ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise ValidationError ( msg ) <EOL> return super ( MutableModel , self ) . delete ( * args , ** kwargs ) </s>
