<s> """<STR_LIT>""" <EOL> from builtins import object <EOL> from django . db import models <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from lino . api import dd , rt <EOL> from lino . utils . xmlgen . html import E , lines2p <EOL> class ContactRelated ( dd . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> abstract = True <EOL> company = models . ForeignKey ( <EOL> "<STR_LIT>" , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> blank = True , null = True ) <EOL> contact_person = models . ForeignKey ( <EOL> "<STR_LIT>" , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> contact_role = models . ForeignKey ( <EOL> "<STR_LIT>" , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> @ dd . chooser ( ) <EOL> def contact_person_choices ( cls , company ) : <EOL> """<STR_LIT>""" <EOL> if company is not None : <EOL> return cls . contact_person_choices_queryset ( company ) <EOL> return rt . modules . contacts . Person . objects . order_by ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_contact ( self ) : <EOL> if self . contact_person is not None : <EOL> if self . company is not None : <EOL> roles = rt . modules . contacts . Role . objects . filter ( <EOL> company = self . company , person = self . contact_person ) <EOL> if roles . count ( ) == <NUM_LIT:1> : <EOL> return roles [ <NUM_LIT:0> ] <EOL> def get_recipient ( self ) : <EOL> contact = self . get_contact ( ) <EOL> if contact is not None : <EOL> return contact <EOL> if self . contact_person is not None : <EOL> if self . company is not None : <EOL> return rt . modules . contacts . Role ( <EOL> company = self . company , person = self . contact_person ) <EOL> return self . contact_person <EOL> return self . company <EOL> recipient = property ( get_recipient ) <EOL> def get_partner ( self ) : <EOL> return self . company or self . contact_person <EOL> partner = property ( get_partner ) <EOL> def get_excerpt_options ( self , ar , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw = super ( ContactRelated , self ) . get_excerpt_options ( ar , ** kw ) <EOL> kw . update ( company = self . company ) <EOL> kw . update ( contact_person = self . contact_person ) <EOL> kw . update ( contact_role = self . contact_role ) <EOL> return kw <EOL> def has_address ( self ) : <EOL> """<STR_LIT>""" <EOL> rec = self . recipient <EOL> if rec is not None : <EOL> return rec . has_address ( ) <EOL> return False <EOL> def get_address_html ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> rec = self . get_recipient ( ) <EOL> if rec is None : <EOL> return E . tostring ( lines2p ( [ ] , * args , ** kwargs ) ) <EOL> return rec . get_address_html ( * args , ** kwargs ) <EOL> def contact_person_changed ( self , ar ) : <EOL> if self . company and not self . contact_person_id : <EOL> roles = rt . modules . contacts . Role . objects . filter ( <EOL> company = self . company ) <EOL> if roles . count ( ) == <NUM_LIT:1> : <EOL> self . contact_person = roles [ <NUM_LIT:0> ] . person <EOL> self . contact_role = roles [ <NUM_LIT:0> ] . type <EOL> return <EOL> @ classmethod <EOL> def contact_person_choices_queryset ( cls , company ) : <EOL> """<STR_LIT>""" <EOL> return settings . SITE . modules . contacts . Person . objects . filter ( <EOL> rolesbyperson__company = company ) . distinct ( ) <EOL> def full_clean ( self , * args , ** kw ) : <EOL> if not settings . SITE . loading_from_dump : <EOL> if self . company is not None and self . contact_person is None : <EOL> qs = self . contact_person_choices_queryset ( self . company ) <EOL> if qs . count ( ) == <NUM_LIT:1> : <EOL> self . contact_person = qs [ <NUM_LIT:0> ] <EOL> else : <EOL> self . contact_person = None <EOL> contact = self . get_contact ( ) <EOL> if contact is not None : <EOL> self . contact_role = contact . type <EOL> super ( ContactRelated , self ) . full_clean ( * args , ** kw ) <EOL> class PartnerDocument ( dd . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> abstract = True <EOL> company = models . ForeignKey ( "<STR_LIT>" , <EOL> blank = True , null = True ) <EOL> person = models . ForeignKey ( "<STR_LIT>" , blank = True , null = True ) <EOL> def get_partner ( self ) : <EOL> if self . company is not None : <EOL> return self . company <EOL> return self . person <EOL> def get_mailable_recipients ( self ) : <EOL> for p in self . company , self . person : <EOL> if p is not None and p . email : <EOL> yield ( '<STR_LIT:to>' , p ) <EOL> def get_postable_recipients ( self ) : <EOL> for p in self . company , self . person : <EOL> if p is not None : <EOL> yield p <EOL> def summary_row ( self , ar , ** kw ) : <EOL> """<STR_LIT>""" <EOL> href_to = ar . obj2html <EOL> s = [ href_to ( self ) ] <EOL> if self . person : <EOL> if self . company : <EOL> s += [ "<STR_LIT>" , href_to ( self . person ) , <EOL> "<STR_LIT:/>" , href_to ( self . company ) , "<STR_LIT:)>" ] <EOL> else : <EOL> s += [ "<STR_LIT>" , href_to ( self . person ) , "<STR_LIT:)>" ] <EOL> elif self . company : <EOL> s += [ "<STR_LIT>" , href_to ( self . company ) , "<STR_LIT:)>" ] <EOL> return s <EOL> def update_owned_instance ( self , other ) : <EOL> if isinstance ( other , dd . ProjectRelated ) : <EOL> if isinstance ( self . person , rt . modules . contacts . Person ) : <EOL> other . project = self . person <EOL> elif isinstance ( self . company , rt . modules . contacts . Person ) : <EOL> other . project = self . company <EOL> other . person = self . person <EOL> other . company = self . company <EOL> super ( PartnerDocument , self ) . update_owned_instance ( other ) <EOL> class OldCompanyContact ( dd . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> abstract = True <EOL> company = models . ForeignKey ( <EOL> "<STR_LIT>" , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> blank = True , null = True ) <EOL> contact = models . ForeignKey ( <EOL> "<STR_LIT>" , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> @ dd . chooser ( ) <EOL> def contact_choices ( cls , company ) : <EOL> if company is not None : <EOL> return cls . contact_choices_queryset ( company ) <EOL> return [ ] <EOL> @ classmethod <EOL> def contact_choices_queryset ( cls , company ) : <EOL> return rt . modules . contacts . Role . objects . filter ( company = company ) <EOL> def full_clean ( self , * args , ** kw ) : <EOL> if self . company : <EOL> if self . contact is None or self . contact . company is None or self . contact . company . pk != self . company . pk : <EOL> qs = self . contact_choices_queryset ( self . company ) <EOL> if qs . count ( ) == <NUM_LIT:1> : <EOL> self . contact = qs [ <NUM_LIT:0> ] <EOL> else : <EOL> self . contact = None <EOL> super ( OldCompanyContact , self ) . full_clean ( * args , ** kw ) </s>
