Ejemplo n.º 1
0
    def test_base_contact(self):
        """Test creating a contact"""

        contact1 = Contact.objects.create(first_name="Jim",
                                          last_name="Tester",
                                          role="Customer",
                                          email="*****@*****.**")

        self.assertEqual(contact1.full_name, u'Jim Tester')

        # Add a phone number for this person and make sure that it's the default
        phone1 = PhoneNumber.objects.create(contact=contact1,
                                            type='Home',
                                            phone="800-111-9900")
        self.assert_(contact1.primary_phone)
        self.assertEqual(contact1.primary_phone.phone, '800-111-9900')
        self.assertEqual(phone1.type, 'Home')

        # Make sure that new primary phones become the default, and that
        # non-primary phones don't become the default when a default already exists.
        phone2 = PhoneNumber.objects.create(contact=contact1,
                                            type='Work',
                                            phone="800-222-9901",
                                            primary=True)
        PhoneNumber.objects.create(contact=contact1,
                                   type="Mobile",
                                   phone="800-333-9902")
        self.assertEqual(contact1.primary_phone, phone2)

        #Add an address & make sure it is default billing and shipping
        add1 = AddressBook.objects.create(contact=contact1,
                                          description="Home Address",
                                          street1="56 Cool Lane",
                                          city="Niftyville",
                                          state="IA",
                                          postal_code="12344",
                                          country=self.us)
        self.assert_(contact1.billing_address)
        self.assertEqual(contact1.billing_address, add1)
        self.assertEqual(contact1.billing_address.description, "Home Address")
        self.assertEqual(add1.street1, "56 Cool Lane")

        self.assertEqual(contact1.billing_address, contact1.shipping_address)
        self.assertEqual(contact1.billing_address.street1, "56 Cool Lane")

        #Add a new shipping address
        add2 = AddressBook(description="Work Address",
                           street1="56 Industry Way",
                           city="Niftytown",
                           state="IA",
                           postal_code="12366",
                           country=self.us,
                           is_default_shipping=True)
        add2.contact = contact1
        add2.save()
        contact1.save()
        self.assertNotEqual(contact1.billing_address,
                            contact1.shipping_address)
        self.assertEqual(contact1.billing_address.description, "Home Address")
        self.assertEqual(contact1.shipping_address.description, "Work Address")
Ejemplo n.º 2
0
    def test_base_contact(self):
        """Test creating a contact"""

        contact1 = Contact.objects.create(first_name="Jim", last_name="Tester",
                                          role="Customer", email="*****@*****.**")

        self.assertEqual(contact1.full_name, u'Jim Tester')

        # Add a phone number for this person and make sure that it's the default
        phone1 = PhoneNumber.objects.create(contact=contact1, type='Home', phone="800-111-9900")
        self.assert_(contact1.primary_phone)
        self.assertEqual(contact1.primary_phone.phone, '800-111-9900')
        self.assertEqual(phone1.type, 'Home')

        # Make sure that new primary phones become the default, and that
        # non-primary phones don't become the default when a default already exists.
        phone2 = PhoneNumber.objects.create(contact=contact1, type='Work', phone="800-222-9901", primary=True)
        PhoneNumber.objects.create(contact=contact1, type="Mobile", phone="800-333-9902")
        self.assertEqual(contact1.primary_phone, phone2)

        #Add an address & make sure it is default billing and shipping
        add1 = AddressBook.objects.create(contact=contact1, description="Home Address",
                                          street1="56 Cool Lane", city="Niftyville",
                                          state="IA", postal_code="12344",
                                          country=self.us)
        self.assert_(contact1.billing_address)
        self.assertEqual(contact1.billing_address, add1)
        self.assertEqual(contact1.billing_address.description, "Home Address")
        self.assertEqual(add1.street1, "56 Cool Lane")

        self.assertEqual(contact1.billing_address, contact1.shipping_address)
        self.assertEqual(contact1.billing_address.street1, "56 Cool Lane")

        #Add a new shipping address
        add2 = AddressBook(description="Work Address", street1="56 Industry Way", city="Niftytown",
                           state="IA", postal_code="12366", country=self.us, is_default_shipping=True)
        add2.contact = contact1
        add2.save()
        contact1.save()
        self.assertNotEqual(contact1.billing_address, contact1.shipping_address)
        self.assertEqual(contact1.billing_address.description, "Home Address")
        self.assertEqual(contact1.shipping_address.description, "Work Address")
Ejemplo n.º 3
0
    def save(self, contact=None, update_newsletter=True):
        """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()
        else:
            customer = contact

        data = self.cleaned_data

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

        if update_newsletter and config_get_group('NEWSLETTER'):
            from satchmo.newsletter import update_subscription
            if 'newsletter' not in data:
                subscribed = False
            else:
                subscribed = data['newsletter']
            
            update_subscription(contact, subscribed)

        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)
                
        address_keys = bill_address.__dict__.keys()
        for field in address_keys:
            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:
                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.country = bill_address.country
            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()
        
        return customer.id
Ejemplo n.º 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

        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
Ejemplo n.º 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