1. Chris Moffitt
  2. satchmo
  3. Issues
Issue #1264 open

Cannot subscribe to newsletter if contact buys anonymously more than once

Anonymous created an issue

When a customer checks out anonymously multiple times with the same email the customer will not be able to subscribe or unsubscribe the newsletter since get_contact_or_fake in /satchmo_ext/newsletter/models.py will fail with a MultipleObjectsReturned-error. get_contact_or_fake should also return a fake-contact if a MultipleObjectsReturned error is raised.

Comments (2)

  1. Hynek Cernoch

    It is important that cookie expires between both orders or is deleted or different browsers are used for these orders, to reproduce the bug.

    Danilo,
    I modified your test, but my modification only shows, that the test is incorrect. The test should be rewritten: result condition, method name, changeset summary and also moved to "neswletter" module because the correct regression test is possible only if newsletter is enabled.

    diff -r 7e612cc6a21a satchmo/apps/satchmo_store/shop/tests.py
    --- a/satchmo/apps/satchmo_store/shop/tests.py  Wed Mar 30 01:16:22 2011 -0300
    +++ b/satchmo/apps/satchmo_store/shop/tests.py  Tue Apr 05 05:00:09 2011 +0200
    @@ -350,6 +350,9 @@
             self.assertRedirects(response, url('DUMMY_satchmo_checkout-success'),
                 status_code=302, target_status_code=200)
     
    +        # The cookies expire or are deleted or the user continues in other browser.
    +        self.client.cookies.clear()
    +
             # Second checkout
             self.test_cart_adding()
             response = self.client.post(url('satchmo_checkout-step1'), get_step1_post_data(self.US))
    

    Fix of the original bug

    Two possible solutions:
    Ignore both contacts, if one is not unique

    --- a/satchmo/apps/satchmo_ext/newsletter/models.py     Wed Mar 30 01:16:22 2011 -0300
    +++ b/satchmo/apps/satchmo_ext/newsletter/models.py     Tue Apr 05 03:42:25 2011 +0200
    @@ -26,7 +26,7 @@
         try:
             contact = Contact.objects.get(email=email)
     
    -    except Contact.DoesNotExist:
    +    except (Contact.DoesNotExist, Contact.MultipleObjectsReturned):
             contact = NullContact(full_name, email)
     
         return contact
    

    or use the last contact

    --- a/satchmo/apps/satchmo_ext/newsletter/models.py     Wed Mar 30 01:16:22 2011 -0300
    +++ b/satchmo/apps/satchmo_ext/newsletter/models.py     Tue Apr 05 03:46:41 2011 +0200
    @@ -24,9 +24,9 @@
     def get_contact_or_fake(full_name, email):
         """Get a `Contact` by email or if it doesn't exist, then a `NullContact`"""
         try:
    -        contact = Contact.objects.get(email=email)
    +        contact = list(Contact.objects.filter(email=email))[-1]
     
    -    except Contact.DoesNotExist:
    +    except IndexError:
             contact = NullContact(full_name, email)
     
         return contact
    
  2. Log in to comment