<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from builtins import str <EOL> from builtins import object <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from django . db import models <EOL> from django . core . exceptions import ValidationError <EOL> from django . conf import settings <EOL> from lino . api import dd , _ , pgettext <EOL> from lino import mixins <EOL> from lino . utils import join_words <EOL> from lino . utils import join_elems <EOL> from lino . modlib . countries . mixins import AddressLocation <EOL> from lino . utils . xmlgen . html import E <EOL> from lino . utils . addressable import Addressable <EOL> from . mixins import ContactRelated , PartnerDocument , OldCompanyContact <EOL> from . roles import ContactsUser , ContactsStaff <EOL> from lino . mixins . human import name2kw , Human , Born <EOL> PARTNER_NUMBERS_START_AT = <NUM_LIT:100> <EOL> @ dd . python_2_unicode_compatible <EOL> class Partner ( mixins . Polymorphic , AddressLocation , Addressable ) : <EOL> """<STR_LIT>""" <EOL> preferred_foreignkey_width = <NUM_LIT:20> <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> name = models . CharField ( max_length = <NUM_LIT:200> , verbose_name = _ ( '<STR_LIT:Name>' ) ) <EOL> language = dd . LanguageField ( ) <EOL> email = models . EmailField ( _ ( '<STR_LIT>' ) , blank = True ) <EOL> url = models . URLField ( _ ( '<STR_LIT>' ) , blank = True ) <EOL> phone = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:200> , blank = True ) <EOL> gsm = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:200> , blank = True ) <EOL> fax = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:200> , blank = True ) <EOL> remarks = models . TextField ( _ ( "<STR_LIT>" ) , blank = True ) <EOL> quick_search_fields = "<STR_LIT:name>" <EOL> def on_create ( self , ar ) : <EOL> self . language = ar . get_user ( ) . language <EOL> if not self . country : <EOL> sc = settings . SITE . site_config <EOL> if sc . site_company : <EOL> self . country = sc . site_company . country <EOL> super ( Partner , self ) . on_create ( ar ) <EOL> def save ( self , * args , ** kw ) : <EOL> if self . id is None : <EOL> sc = settings . SITE . site_config <EOL> if sc . next_partner_id is not None : <EOL> try : <EOL> self . __class__ . objects . get ( id = sc . next_partner_id ) <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> sc . next_partner_id ) ) <EOL> except self . __class__ . DoesNotExist : <EOL> self . id = sc . next_partner_id <EOL> sc . next_partner_id += <NUM_LIT:1> <EOL> sc . save ( ) <EOL> super ( Partner , self ) . save ( * args , ** kw ) <EOL> def __str__ ( self ) : <EOL> return self . get_full_name ( ) <EOL> def address_person_lines ( self ) : <EOL> yield self . get_full_name ( ) <EOL> def get_full_name ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . name <EOL> full_name = property ( get_full_name ) <EOL> @ dd . displayfield ( _ ( "<STR_LIT:Name>" ) ) <EOL> def name_column ( self , request ) : <EOL> return str ( self ) <EOL> def get_partner_instance ( self ) : <EOL> return self <EOL> @ dd . displayfield ( ) <EOL> def overview ( self , ar ) : <EOL> return E . div ( * self . get_overview_elems ( ar ) ) <EOL> def get_overview_elems ( self , ar ) : <EOL> elems = [ ] <EOL> if ar is None : <EOL> return elems <EOL> buttons = self . get_mti_buttons ( ar ) <EOL> elems . append ( E . p ( str ( _ ( "<STR_LIT>" ) ) , * buttons , <EOL> style = "<STR_LIT>" ) ) <EOL> elems += self . get_name_elems ( ar ) <EOL> elems . append ( E . br ( ) ) <EOL> elems += join_elems ( list ( self . address_location_lines ( ) ) , sep = E . br ) <EOL> elems = [ <EOL> E . div ( * elems , <EOL> style = "<STR_LIT>" <EOL> "<STR_LIT>" ) ] <EOL> return elems <EOL> def get_name_elems ( self , ar ) : <EOL> return [ E . b ( self . name ) ] <EOL> def get_print_language ( self ) : <EOL> return self . language <EOL> class PartnerDetail ( dd . DetailLayout ) : <EOL> main = """<STR_LIT>""" <EOL> address_box = dd . Panel ( """<STR_LIT>""" , label = _ ( "<STR_LIT>" ) ) <EOL> contact_box = dd . Panel ( """<STR_LIT>""" , label = _ ( "<STR_LIT>" ) ) <EOL> bottom_box = """<STR_LIT>""" <EOL> name_box = "<STR_LIT:name>" <EOL> info_box = "<STR_LIT>" <EOL> class Partners ( dd . Table ) : <EOL> required_roles = dd . required ( ContactsUser ) <EOL> model = '<STR_LIT>' <EOL> column_names = "<STR_LIT>" <EOL> order_by = [ '<STR_LIT:name>' , '<STR_LIT:id>' ] <EOL> detail_layout = PartnerDetail ( ) <EOL> insert_layout = """<STR_LIT>""" <EOL> @ classmethod <EOL> def get_queryset ( self , ar ) : <EOL> qs = super ( Partners , self ) . get_queryset ( ar ) <EOL> return qs . select_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class PartnersByCity ( Partners ) : <EOL> master_key = '<STR_LIT>' <EOL> order_by = '<STR_LIT>' . split ( ) <EOL> column_names = "<STR_LIT>" <EOL> class PartnersByCountry ( Partners ) : <EOL> master_key = '<STR_LIT>' <EOL> column_names = "<STR_LIT>" <EOL> order_by = "<STR_LIT>" . split ( ) <EOL> class Person ( Human , Born , Partner ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def full_clean ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = join_words ( self . last_name , self . first_name ) <EOL> if name : <EOL> self . name = name <EOL> else : <EOL> for k , v in list ( name2kw ( self . name ) . items ( ) ) : <EOL> setattr ( self , k , v ) <EOL> super ( Person , self ) . full_clean ( * args , ** kw ) <EOL> def address_person_lines ( self , * args , ** kw ) : <EOL> "<STR_LIT>" <EOL> yield self . get_full_name ( * args , ** kw ) <EOL> def get_name_elems ( self , ar ) : <EOL> elems = [ self . get_salutation ( nominative = True ) , '<STR_LIT:U+0020>' , <EOL> self . title , E . br ( ) ] <EOL> elems += [ self . first_name , '<STR_LIT:U+0020>' , E . b ( self . last_name ) ] <EOL> return elems <EOL> class PersonDetail ( PartnerDetail ) : <EOL> name_box = "<STR_LIT>" <EOL> info_box = "<STR_LIT>" <EOL> bottom_box = "<STR_LIT>" <EOL> class Persons ( Partners ) : <EOL> """<STR_LIT>""" <EOL> required_roles = dd . required ( ContactsUser ) <EOL> model = "<STR_LIT>" <EOL> order_by = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:id>" ] <EOL> column_names = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> detail_layout = PersonDetail ( ) <EOL> insert_layout = """<STR_LIT>""" <EOL> class CompanyType ( mixins . BabelNamed ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> abbr = dd . BabelCharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> , blank = True ) <EOL> class CompanyTypes ( dd . Table ) : <EOL> required_roles = dd . required ( ContactsStaff ) <EOL> model = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> class Company ( Partner ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> prefix = models . CharField ( <EOL> _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:200> , blank = True ) <EOL> type = models . ForeignKey ( '<STR_LIT>' , blank = True , null = True ) <EOL> def get_full_name ( self , salutation = True , ** salutation_options ) : <EOL> """<STR_LIT>""" <EOL> if self . type : <EOL> return join_words ( self . prefix , self . type . abbr , self . name ) <EOL> return join_words ( self . prefix , self . name ) <EOL> full_name = property ( get_full_name ) <EOL> def get_name_elems ( self , ar ) : <EOL> elems = [ ] <EOL> if self . prefix : <EOL> elems += [ self . prefix , '<STR_LIT:U+0020>' ] <EOL> elems += [ E . b ( self . name ) ] <EOL> return elems <EOL> class CompanyDetail ( PartnerDetail ) : <EOL> bottom_box = """<STR_LIT>""" <EOL> name_box = "<STR_LIT>" <EOL> class Companies ( Partners ) : <EOL> required_roles = dd . required ( ContactsUser ) <EOL> model = "<STR_LIT>" <EOL> order_by = [ "<STR_LIT:name>" ] <EOL> column_names = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> detail_layout = CompanyDetail ( ) <EOL> insert_layout = """<STR_LIT>""" <EOL> class RoleType ( mixins . BabelNamed ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class RoleTypes ( dd . Table ) : <EOL> required_roles = dd . required ( ContactsStaff ) <EOL> model = '<STR_LIT>' <EOL> @ dd . python_2_unicode_compatible <EOL> class Role ( dd . Model , Addressable ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> type = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> person = models . ForeignKey ( <EOL> "<STR_LIT>" , related_name = '<STR_LIT>' ) <EOL> company = models . ForeignKey ( <EOL> "<STR_LIT>" , related_name = '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> if self . person_id is None : <EOL> return super ( Role , self ) . __str__ ( ) <EOL> if self . type is None : <EOL> return str ( self . person ) <EOL> return u"<STR_LIT>" % ( self . person , self . type ) <EOL> def address_person_lines ( self ) : <EOL> if self . company : <EOL> for ln in self . company . address_person_lines ( ) : <EOL> yield ln <EOL> for ln in self . person . address_person_lines ( ) : <EOL> yield ln <EOL> def address_location_lines ( self ) : <EOL> if self . company_id : <EOL> return self . company . address_location_lines ( ) <EOL> if self . person_id : <EOL> return self . person . address_location_lines ( ) <EOL> return super ( Role , self ) . __str__ ( ) <EOL> def get_print_language ( self ) : <EOL> if self . company_id : <EOL> return self . company . language <EOL> if self . person_id : <EOL> return self . person . language <EOL> return super ( Role , self ) . get_print_language ( ) <EOL> class Roles ( dd . Table ) : <EOL> required_roles = dd . required ( ContactsStaff ) <EOL> model = '<STR_LIT>' <EOL> class RolesByCompany ( Roles ) : <EOL> required_roles = dd . required ( ContactsUser ) <EOL> auto_fit_column_widths = True <EOL> label = _ ( "<STR_LIT>" ) <EOL> master_key = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> hidden_columns = '<STR_LIT:id>' <EOL> class RolesByPerson ( Roles ) : <EOL> required_roles = dd . required ( ContactsUser ) <EOL> label = _ ( "<STR_LIT>" ) <EOL> master_key = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> auto_fit_column_widths = True <EOL> hidden_columns = '<STR_LIT:id>' <EOL> dd . inject_field ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> models . IntegerField ( <EOL> default = PARTNER_NUMBERS_START_AT , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" ) ) ) <EOL> dd . inject_field ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> models . ForeignKey ( <EOL> "<STR_LIT>" , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> related_name = '<STR_LIT>' , <EOL> help_text = _ ( """<STR_LIT>""" ) ) ) <EOL> def site_setup ( site ) : <EOL> site . modules . countries . Places . set_detail_layout ( """<STR_LIT>""" ) <EOL> @ dd . receiver ( dd . pre_analyze ) <EOL> def company_model_alias ( sender , ** kw ) : <EOL> """<STR_LIT>""" <EOL> sender . modules . contacts . Organisation = sender . modules . contacts . Company <EOL> @ dd . receiver ( dd . post_analyze ) <EOL> def company_tables_alias ( sender , ** kw ) : <EOL> """<STR_LIT>""" <EOL> sender . modules . contacts . Organisations = sender . modules . contacts . Companies <EOL> def PartnerField ( ** kw ) : <EOL> return models . ForeignKey ( Partner , ** kw ) </s>
