<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from __future__ import print_function <EOL> from builtins import object <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from lino . api import rt , dd <EOL> from lino . utils . xmlgen . html import E <EOL> from lino . core . utils import ChangeWatcher <EOL> from lino . modlib . plausibility . choicelists import Checker <EOL> from . choicelists import AddressTypes <EOL> class AddressOwner ( dd . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> abstract = True <EOL> def get_address_by_type ( self , address_type ) : <EOL> Address = rt . modules . addresses . Address <EOL> try : <EOL> return Address . objects . get ( <EOL> partner = self , address_type = address_type ) <EOL> except Address . DoesNotExist : <EOL> return self . get_primary_address ( ) <EOL> except Address . MultipleObjectsReturned : <EOL> return self . get_primary_address ( ) <EOL> def get_primary_address ( self ) : <EOL> """<STR_LIT>""" <EOL> Address = rt . modules . addresses . Address <EOL> try : <EOL> return Address . objects . get ( partner = self , primary = True ) <EOL> except Address . DoesNotExist : <EOL> pass <EOL> def sync_primary_address ( self , request ) : <EOL> watcher = ChangeWatcher ( self ) <EOL> self . sync_primary_address_ ( ) <EOL> watcher . send_update ( request ) <EOL> def sync_primary_address_ ( self ) : <EOL> Address = rt . modules . addresses . Address <EOL> kw = dict ( partner = self , primary = True ) <EOL> try : <EOL> pa = Address . objects . get ( ** kw ) <EOL> for k in Address . ADDRESS_FIELDS : <EOL> setattr ( self , k , getattr ( pa , k ) ) <EOL> except Address . DoesNotExist : <EOL> pa = None <EOL> for k in Address . ADDRESS_FIELDS : <EOL> fld = self . _meta . get_field ( k ) <EOL> setattr ( self , k , fld . get_default ( ) ) <EOL> self . save ( ) <EOL> def get_overview_elems ( self , ar ) : <EOL> if ar is None : <EOL> return [ ] <EOL> elems = super ( AddressOwner , self ) . get_overview_elems ( ar ) <EOL> sar = ar . spawn ( '<STR_LIT>' , <EOL> master_instance = self ) <EOL> btn = sar . as_button ( _ ( "<STR_LIT>" ) ) <EOL> elems . append ( E . p ( btn ) ) <EOL> return elems <EOL> class AddressOwnerChecker ( Checker ) : <EOL> """<STR_LIT>""" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> model = AddressOwner <EOL> messages = dict ( <EOL> no_address = _ ( "<STR_LIT>" ) , <EOL> unique_not_primary = _ ( "<STR_LIT>" ) , <EOL> no_primary = _ ( "<STR_LIT>" ) , <EOL> multiple_primary = _ ( "<STR_LIT>" ) , <EOL> primary_differs = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> def get_plausibility_problems ( self , obj , fix = False ) : <EOL> Address = rt . modules . addresses . Address <EOL> qs = Address . objects . filter ( partner = obj ) <EOL> num = qs . count ( ) <EOL> if num == <NUM_LIT:0> : <EOL> kw = dict ( ) <EOL> for fldname in Address . ADDRESS_FIELDS : <EOL> v = getattr ( obj , fldname ) <EOL> if v : <EOL> kw [ fldname ] = v <EOL> if kw : <EOL> yield ( True , self . messages [ '<STR_LIT>' ] ) <EOL> if fix : <EOL> kw . update ( partner = obj , primary = True ) <EOL> kw . update ( address_type = AddressTypes . official ) <EOL> addr = Address ( ** kw ) <EOL> addr . full_clean ( ) <EOL> addr . save ( ) <EOL> return <EOL> def getdiffs ( obj , addr ) : <EOL> diffs = { } <EOL> for k in Address . ADDRESS_FIELDS : <EOL> my = getattr ( addr , k ) <EOL> other = getattr ( obj , k ) <EOL> if my != other : <EOL> diffs [ k ] = ( my , other ) <EOL> return diffs <EOL> if num == <NUM_LIT:1> : <EOL> addr = qs [ <NUM_LIT:0> ] <EOL> diffs = getdiffs ( obj , addr ) <EOL> if not diffs : <EOL> if not addr . primary : <EOL> yield ( True , self . messages [ '<STR_LIT>' ] ) <EOL> if fix : <EOL> addr . primary = True <EOL> addr . full_clean ( ) <EOL> addr . save ( ) <EOL> return <EOL> else : <EOL> addr = None <EOL> qs = qs . filter ( primary = True ) <EOL> num = qs . count ( ) <EOL> if num == <NUM_LIT:0> : <EOL> yield ( False , self . messages [ '<STR_LIT>' ] ) <EOL> elif num == <NUM_LIT:1> : <EOL> addr = qs [ <NUM_LIT:0> ] <EOL> diffs = getdiffs ( obj , addr ) <EOL> else : <EOL> yield ( False , self . messages [ '<STR_LIT>' ] ) <EOL> if addr and diffs : <EOL> diffstext = [ <EOL> _ ( "<STR_LIT>" ) . format ( k , * v ) for k , v in list ( diffs . items ( ) ) ] <EOL> msg = self . messages [ '<STR_LIT>' ] . format ( '<STR_LIT:U+002CU+0020>' . join ( diffstext ) ) <EOL> yield ( False , msg ) <EOL> AddressOwnerChecker . activate ( ) </s>
