<s> """<STR_LIT>""" <EOL> from django . contrib . auth . models import User <EOL> from django . db import models <EOL> from django . utils . translation import ugettext , ugettext_lazy as _ <EOL> from l10n . models import Country <EOL> from satchmo_store . contact import CUSTOMER_ID <EOL> import datetime <EOL> import logging <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class ContactRole ( models . Model ) : <EOL> key = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:30> , unique = True , primary_key = True ) <EOL> name = models . CharField ( _ ( '<STR_LIT:Name>' ) , max_length = <NUM_LIT> ) <EOL> def __unicode__ ( self ) : <EOL> return ugettext ( self . name ) <EOL> class ContactOrganization ( models . Model ) : <EOL> key = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:30> , unique = True , primary_key = True ) <EOL> name = models . CharField ( _ ( '<STR_LIT:Name>' ) , max_length = <NUM_LIT> ) <EOL> def __unicode__ ( self ) : <EOL> return ugettext ( self . name ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> class ContactOrganizationRole ( models . Model ) : <EOL> key = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:30> , unique = True , primary_key = True ) <EOL> name = models . CharField ( _ ( '<STR_LIT:Name>' ) , max_length = <NUM_LIT> ) <EOL> def __unicode__ ( self ) : <EOL> return ugettext ( self . name ) <EOL> class ContactInteractionType ( models . Model ) : <EOL> key = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:30> , unique = True , primary_key = True ) <EOL> name = models . CharField ( _ ( '<STR_LIT:Name>' ) , max_length = <NUM_LIT> ) <EOL> def __unicode__ ( self ) : <EOL> return ugettext ( self . name ) <EOL> class OrganizationManager ( models . Manager ) : <EOL> def by_name ( self , name , create = False , role = '<STR_LIT>' , orgtype = '<STR_LIT>' ) : <EOL> org = None <EOL> orgs = self . filter ( name = name , role__key = role , type__key = orgtype ) <EOL> if orgs . count ( ) > <NUM_LIT:0> : <EOL> org = orgs [ <NUM_LIT:0> ] <EOL> if not org : <EOL> if not create : <EOL> raise Organization . DoesNotExist ( ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' , name ) <EOL> role = ContactOrganizationRole . objects . get ( pk = role ) <EOL> orgtype = ContactOrganization . objects . get ( pk = orgtype ) <EOL> org = Organization ( name = name , role = role , type = orgtype ) <EOL> org . save ( ) <EOL> return org <EOL> class Organization ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( _ ( "<STR_LIT:Name>" ) , max_length = <NUM_LIT:50> , ) <EOL> type = models . ForeignKey ( ContactOrganization , verbose_name = _ ( "<STR_LIT>" ) , null = True ) <EOL> role = models . ForeignKey ( ContactOrganizationRole , verbose_name = _ ( "<STR_LIT>" ) , null = True ) <EOL> create_date = models . DateField ( _ ( "<STR_LIT>" ) ) <EOL> notes = models . TextField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:200> , blank = True , null = True ) <EOL> objects = OrganizationManager ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def save ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . pk : <EOL> self . create_date = datetime . date . today ( ) <EOL> super ( Organization , self ) . save ( ** kwargs ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class ContactManager ( models . Manager ) : <EOL> def from_request ( self , request , create = False ) : <EOL> """<STR_LIT>""" <EOL> contact = None <EOL> if request . user . is_authenticated ( ) : <EOL> try : <EOL> contact = Contact . objects . get ( user = request . user . id ) <EOL> request . session [ CUSTOMER_ID ] = contact . id <EOL> except Contact . DoesNotExist : <EOL> pass <EOL> else : <EOL> create = False <EOL> if request . session . get ( CUSTOMER_ID ) : <EOL> try : <EOL> contactBySession = Contact . objects . get ( id = request . session [ CUSTOMER_ID ] ) <EOL> if contact is None : <EOL> contact = contactBySession <EOL> elif contact != contactBySession : <EOL> log . debug ( "<STR_LIT>" % <EOL> ( contact . user . get_full_name ( ) , contact . id , Contact . objects . get ( id = request . session [ CUSTOMER_ID ] ) . full_name , request . session [ CUSTOMER_ID ] ) ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> del request . session [ CUSTOMER_ID ] <EOL> except Contact . DoesNotExist : <EOL> log . debug ( "<STR_LIT>" % request . session [ CUSTOMER_ID ] ) <EOL> del request . session [ CUSTOMER_ID ] <EOL> if contact is None : <EOL> if create : <EOL> contact = Contact ( user = request . user ) <EOL> else : <EOL> raise Contact . DoesNotExist ( ) <EOL> return contact <EOL> class Contact ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> title = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> , blank = True , null = True ) <EOL> first_name = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> , ) <EOL> last_name = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> , ) <EOL> user = models . ForeignKey ( User , blank = True , null = True , unique = True ) <EOL> role = models . ForeignKey ( ContactRole , verbose_name = _ ( "<STR_LIT>" ) , null = True ) <EOL> organization = models . ForeignKey ( Organization , verbose_name = _ ( "<STR_LIT>" ) , blank = True , null = True ) <EOL> dob = models . DateField ( _ ( "<STR_LIT>" ) , blank = True , null = True ) <EOL> email = models . EmailField ( _ ( "<STR_LIT>" ) , blank = True , max_length = <NUM_LIT> ) <EOL> notes = models . TextField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT> , blank = True ) <EOL> create_date = models . DateField ( _ ( "<STR_LIT>" ) ) <EOL> objects = ContactManager ( ) <EOL> def _get_full_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return u'<STR_LIT>' % ( self . first_name , self . last_name ) <EOL> full_name = property ( _get_full_name ) <EOL> def _shipping_address ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . addressbook_set . get ( is_default_shipping = True ) <EOL> except AddressBook . DoesNotExist : <EOL> return None <EOL> shipping_address = property ( _shipping_address ) <EOL> def _billing_address ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . addressbook_set . get ( is_default_billing = True ) <EOL> except AddressBook . DoesNotExist : <EOL> return None <EOL> billing_address = property ( _billing_address ) <EOL> def _primary_phone ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . phonenumber_set . get ( primary = True ) <EOL> except PhoneNumber . DoesNotExist : <EOL> return None <EOL> primary_phone = property ( _primary_phone ) <EOL> def __unicode__ ( self ) : <EOL> return self . full_name <EOL> def save ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . pk : <EOL> self . create_date = datetime . date . today ( ) <EOL> if self . user : <EOL> dirty = False <EOL> user = self . user <EOL> if user . email != self . email : <EOL> user . email = self . email <EOL> dirty = True <EOL> if user . first_name != self . first_name : <EOL> user . first_name = self . first_name <EOL> dirty = True <EOL> if user . last_name != self . last_name : <EOL> user . last_name = self . last_name <EOL> dirty = True <EOL> if dirty : <EOL> self . user = user <EOL> self . user . save ( ) <EOL> super ( Contact , self ) . save ( ** kwargs ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> PHONE_CHOICES = ( <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> class Interaction ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> contact = models . ForeignKey ( Contact , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> type = models . ForeignKey ( ContactInteractionType , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> date_time = models . DateTimeField ( _ ( "<STR_LIT>" ) , ) <EOL> description = models . TextField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:200> ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . contact . full_name , self . type ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class PhoneNumber ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> contact = models . ForeignKey ( Contact ) <EOL> type = models . CharField ( _ ( "<STR_LIT>" ) , choices = PHONE_CHOICES , <EOL> max_length = <NUM_LIT:20> , blank = True ) <EOL> phone = models . CharField ( _ ( "<STR_LIT>" ) , blank = True , max_length = <NUM_LIT:30> , <EOL> ) <EOL> primary = models . BooleanField ( _ ( "<STR_LIT>" ) , default = False ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . type , self . phone ) <EOL> def save ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> existing_number = self . contact . primary_phone <EOL> if existing_number : <EOL> if self . primary : <EOL> existing_number . primary = False <EOL> super ( PhoneNumber , existing_number ) . save ( ) <EOL> else : <EOL> self . primary = True <EOL> super ( PhoneNumber , self ) . save ( ** kwargs ) <EOL> class Meta : <EOL> ordering = [ '<STR_LIT>' ] <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class AddressBook ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> contact = models . ForeignKey ( Contact ) <EOL> description = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:20> , blank = True , <EOL> help_text = _ ( '<STR_LIT>' , ) ) <EOL> addressee = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT> ) <EOL> street1 = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT> ) <EOL> street2 = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT> , blank = True ) <EOL> state = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:50> , blank = True ) <EOL> city = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:50> ) <EOL> postal_code = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:30> ) <EOL> country = models . ForeignKey ( Country , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> is_default_shipping = models . BooleanField ( _ ( "<STR_LIT>" ) , <EOL> default = False ) <EOL> is_default_billing = models . BooleanField ( _ ( "<STR_LIT>" ) , <EOL> default = False ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . contact . full_name , self . description ) <EOL> def save ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> existing_billing = self . contact . billing_address <EOL> if existing_billing : <EOL> if self . is_default_billing : <EOL> existing_billing . is_default_billing = False <EOL> super ( AddressBook , existing_billing ) . save ( ) <EOL> else : <EOL> self . is_default_billing = True <EOL> existing_shipping = self . contact . shipping_address <EOL> if existing_shipping : <EOL> if self . is_default_shipping : <EOL> existing_shipping . is_default_shipping = False <EOL> super ( AddressBook , existing_shipping ) . save ( ) <EOL> else : <EOL> self . is_default_shipping = True <EOL> super ( AddressBook , self ) . save ( ** kwargs ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> import config </s>
