示例#1
0
 def save(self, contact, address_entry=None, **kwargs):
     data = self.cleaned_data.copy()
     if not address_entry:
         address_entry = AddressBook()
         log.debug('creating new AddressBook entry')
     else:
         address_entry = address_entry
         log.debug('Saving Addressbook info for %s', address_entry)
     for field in data.keys():
         # Getting around the issue where we normally want this auto created on the front end
         if not field == 'addressee_name':
             setattr(address_entry, field, data[field])
     address_entry.addressee = data['addressee_name']
     address_entry.contact = contact
     address_entry.save()
示例#2
0
 def save(self, contact, address_entry=None, **kwargs):
     data = self.cleaned_data.copy()
     if not address_entry:
         address_entry = AddressBook()
         log.debug('creating new AddressBook entry')
     else:
         address_entry = address_entry
         log.debug('Saving Addressbook info for %s', address_entry)
     for field in data.keys():
         # Getting around the issue where we normally want this auto created on the front end
         if field <> 'addressee_name':
             setattr(address_entry, field, data[field])
     address_entry.addressee = data['addressee_name']
     address_entry.contact = contact
     address_entry.save()
示例#3
0
文件: forms.py 项目: ringemup/satchmo
    def save_info(self, contact=None, **kwargs):
        """Save the contact info into the database.
        Checks to see if contact exists. If not, creates a contact
        and copies in the address and phone number."""

        if not contact:
            customer = Contact()
            log.debug('creating new contact')
        else:
            customer = contact
            log.debug('Saving contact info for %s', contact)

        data = self.cleaned_data.copy()

        country = data['country']
        if not isinstance(country, Country):
            country = Country.objects.get(pk=country)
            data['country'] = country
        data['country_id'] = country.id

        shipcountry = data['ship_country']
        if not isinstance(shipcountry, Country):
            shipcountry = Country.objects.get(pk=shipcountry)
            data['ship_country'] = shipcountry

        data['ship_country_id'] = shipcountry.id

        organization_name = data.pop('organization', None)
        if organization_name:
            org = Organization.objects.by_name(organization_name, create=True)
            customer.organization = org
        else:
            # in case customer wants to remove organization name from their profile
            customer.organization = None

        for field in customer.__dict__.keys():
            try:
                setattr(customer, field, data[field])
            except KeyError:
                pass

        if not customer.role:
            customer.role = ContactRole.objects.get(pk='Customer')

        customer.save()

        # we need to make sure we don't blindly add new addresses
        # this isn't ideal, but until we have a way to manage addresses
        # this will force just the two addresses, shipping and billing
        # TODO: add address management like Amazon.

        bill_address = customer.billing_address
        if not bill_address:
            bill_address = AddressBook(contact=customer)

        changed_location = False
        address_keys = bill_address.__dict__.keys()
        for field in address_keys:
            if (not changed_location) and field in ('state', 'country_id',
                                                    'city'):
                if getattr(bill_address, field) != data[field]:
                    changed_location = True
            try:
                setattr(bill_address, field, data[field])
            except KeyError:
                pass

        bill_address.is_default_billing = True

        copy_address = data['copy_address']

        ship_address = customer.shipping_address

        if copy_address:
            # make sure we don't have any other default shipping address
            if ship_address and ship_address.id != bill_address.id:
                ship_address.delete()
            bill_address.is_default_shipping = True

        bill_address.save()

        if not copy_address:
            if not ship_address or ship_address.id == bill_address.id:
                ship_address = AddressBook()

            for field in address_keys:
                ship_field = 'ship_' + field
                if (not changed_location) and field in ('state', 'country_id',
                                                        'city'):
                    if getattr(ship_address, field) != data[ship_field]:
                        changed_location = True
                try:
                    setattr(ship_address, field, data[ship_field])
                except KeyError:
                    pass
            ship_address.is_default_shipping = True
            ship_address.is_default_billing = False
            ship_address.contact = customer
            ship_address.save()

        if not customer.primary_phone:
            phone = PhoneNumber()
            phone.primary = True
        else:
            phone = customer.primary_phone
        phone.phone = data['phone']
        phone.contact = customer
        phone.save()

        form_postsave.send(ContactInfoForm,
                           object=customer,
                           formdata=data,
                           form=self)

        if changed_location:
            signals.satchmo_contact_location_changed.send(self,
                                                          contact=customer)

        return customer.id
示例#4
0
    def save_info(self, contact=None, **kwargs):
        """Save the contact info into the database.
        Checks to see if contact exists. If not, creates a contact
        and copies in the address and phone number."""

        if not contact:
            customer = Contact()
            log.debug('creating new contact')
        else:
            customer = contact
            log.debug('Saving contact info for %s', contact)

        data = self.cleaned_data.copy()

        country = data['country']
        if not isinstance(country, Country):
            country = Country.objects.get(pk=country)
            data['country'] = country
        data['country_id'] = country.id

        shipcountry = data['ship_country']
        if not isinstance(shipcountry, Country):
            shipcountry = Country.objects.get(pk=shipcountry)
            data['ship_country'] = shipcountry

        data['ship_country_id'] = shipcountry.id

        organization_name = data.pop('organization', None)
        if organization_name:
            org = Organization.objects.by_name(organization_name, create=True)
            customer.organization = org
        else:
            # in case customer wants to remove organization name from their profile
            customer.organization = None

        for field in customer.__dict__.keys():
            try:
                setattr(customer, field, data[field])
            except KeyError:
                pass

        if not customer.role:
            customer.role = ContactRole.objects.get(pk='Customer')

        customer.save()

        # we need to make sure we don't blindly add new addresses
        # this isn't ideal, but until we have a way to manage addresses
        # this will force just the two addresses, shipping and billing
        # TODO: add address management like Amazon.

        bill_address = customer.billing_address
        if not bill_address:
            bill_address = AddressBook(contact=customer)

        changed_location = False
        address_keys = bill_address.__dict__.keys()
        for field in address_keys:
            if (not changed_location) and field in ('state', 'country_id', 'city'):
                if getattr(bill_address, field) != data[field]:
                    changed_location = True
            try:
                setattr(bill_address, field, data[field])
            except KeyError:
                pass

        bill_address.is_default_billing = True

        copy_address = data['copy_address']

        ship_address = customer.shipping_address
        try:
            setattr(ship_address, "addressee",data.get('ship_addressee', ""))
            setattr(bill_address, "addressee",data.get('addressee', ""))
        except AttributeError:
            pass
        # If we are copying the address and one isn't in place for shipping
        # copy it
        if not getattr(ship_address, "addressee", False) and copy_address:
            try:
                ship_address.addressee = bill_address.addressee
            except AttributeError:
                pass
                
        # Make sure not to overwrite a custom ship to name
        if copy_address and getattr(ship_address, "addressee", "") == getattr(bill_address, "addressee", ""):
            # make sure we don't have any other default shipping address
            if ship_address and ship_address.id != bill_address.id:
                ship_address.delete()
            bill_address.is_default_shipping = True

        bill_address.save()
        # If we have different ship to and bill to names, preserve them
        if not copy_address or getattr(ship_address, "addressee", "") != getattr(bill_address, "addressee", ""):
            if not ship_address or ship_address.id == bill_address.id:
                ship_address = AddressBook()

            for field in address_keys:
                ship_field = 'ship_' + field
                if (not changed_location) and field in ('state', 'country_id', 'city'):
                    if getattr(ship_address, field) != data[ship_field]:
                        changed_location = True
                try:
                    setattr(ship_address, field, data[ship_field])
                except KeyError:
                    pass
            ship_address.is_default_shipping = True
            ship_address.is_default_billing = False
            ship_address.contact = customer
            ship_address.save()

        if not customer.primary_phone:
            phone = PhoneNumber()
            phone.primary = True
        else:
            phone = customer.primary_phone
        phone.phone = data['phone']
        phone.contact = customer
        phone.save()

        form_postsave.send(ContactInfoForm, object=customer, formdata=data, form=self)

        if changed_location:
            signals.satchmo_contact_location_changed.send(self, contact=customer)

        return customer.id
示例#5
0
    def save_info(self, contact=None, **kwargs):
        """Save the contact info into the database.
        Checks to see if contact exists. If not, creates a contact
        and copies in the address and phone number."""
        
        if not contact:
            customer = Contact()
            log.debug('creating new contact')
        else:
            customer = contact
            log.debug('Saving contact info for %s', contact)

        data = self.cleaned_data.copy()

        country = data['country']
        if not isinstance(country, Country):
            country = Country.objects.get(pk=country)
            data['country'] = country
        data['country_id'] = country.id

        shipcountry = data['ship_country']
        if not isinstance(shipcountry, Country):
            shipcountry = Country.objects.get(pk=shipcountry)
            data['ship_country'] = shipcountry
        
        data['ship_country_id'] = shipcountry.id
        
        companyname = data.pop('company', None)
        if companyname:
            org = Organization.objects.by_name(companyname, create=True)            
            customer.organization = org
        
        for field in customer.__dict__.keys():
            try:
                setattr(customer, field, data[field])
            except KeyError:
                pass

        if not customer.role:
            customer.role = "Customer"

        customer.save()
        
        # we need to make sure we don't blindly add new addresses
        # this isn't ideal, but until we have a way to manage addresses
        # this will force just the two addresses, shipping and billing
        # TODO: add address management like Amazon.
        
        bill_address = customer.billing_address
        if not bill_address:
            bill_address = AddressBook(contact=customer)
                
        changed_location = False
        address_keys = bill_address.__dict__.keys()
        for field in address_keys:
            if (not changed_location) and field in ('state', 'country', 'city'):
                if getattr(bill_address, field) != data[field]:
                    changed_location = True
            try:
                setattr(bill_address, field, data[field])
            except KeyError:
                pass

        bill_address.is_default_billing = True
        
        copy_address = data['copy_address']

        ship_address = customer.shipping_address
        
        if copy_address:
            # make sure we don't have any other default shipping address
            if ship_address and ship_address.id != bill_address.id:
                ship_address.delete()
            bill_address.is_default_shipping = True

        bill_address.save()
        
        if not copy_address:
            if not ship_address or ship_address.id == bill_address.id:
                ship_address = AddressBook()
            
            for field in address_keys:
                if (not changed_location) and field in ('state', 'country', 'city'):
                    if getattr(ship_address, field) != data[field]:
                        changed_location = True
                try:
                    setattr(ship_address, field, data['ship_' + field])
                except KeyError:
                    pass
            ship_address.is_default_shipping = True
            ship_address.is_default_billing = False
            ship_address.contact = customer
            ship_address.save()
            
        if not customer.primary_phone:
            phone = PhoneNumber()
            phone.primary = True
        else:
            phone = customer.primary_phone
        phone.phone = data['phone']
        phone.contact = customer
        phone.save()
        
        signals.form_save.send(ContactInfoForm, object=customer, formdata=data, form=self)
        
        if changed_location:
            signals.satchmo_contact_location_changed.send(self, contact=customer)
        
        return customer.id