コード例 #1
0
def add_invited(user):
    print "ADD INVITED SUBSCRIPTIONS"
    ## Add to subscription list

    # get newsletter
    try:
        newsl = Newsletter.objects.get(slug='emif-catalogue-newsletter')

        # create subscription
        user_sub = Subscription(user=user, newsletter=newsl)

        user_sub.subscribe()

        user_sub.save()

    except Newsletter.DoesNotExist:
        print "Problem registering new user to emif default newsletter"

    # add invited dbs if any
    sps = InvitePending.objects.filter(email=user.email)

    for sp in sps:

        fingerprint = sp.fingerprint

        fingerprint.save()

        fingerprint.shared.add(user)

        sp.delete()

        # must reindex, because databases lists come from solr, to update user_t
        fingerprint.indexFingerprint()
コード例 #2
0
ファイル: 8pm.py プロジェクト: zdanozdan/django-newsletter
    def import_auth_users(self, *args, **options):
        auth_list, created = List.objects.get_or_create(name="8pm_leak")
        file = open("newsletter/management/commands/runners.txt", "r")

        EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")

        runners = []
        for email in file:
            if not EMAIL_REGEX.match(email):
                print "Error matching regex"
            else:
                runners.append(email.replace(',','').rstrip())

        users = []
        for u in User.objects.all():
            users.append(u.email)

        diff = [x for x in runners if x not in users]

        for u in diff:
            try:
                s = Subscription.objects.get(email_field=u)
                print "skipping, user alread loaded from auth: ",s
            except Subscription.DoesNotExist:
                print "adding new user from list intersection: ",u
                subscriber = Subscription(email_field=u,list=auth_list,subscribed=True)
                subscriber.save()

        print "Runners.txt len: ", len(runners)
        print "Current DB users len: ",len(users)
        print "Diff len: ",len(diff)
コード例 #3
0
ファイル: views.py プロジェクト: kushiahu/djangomx
def subscribe_request(request):
    """ Adds a new subscription to mailchimp list. """
    if request.POST:
        mail_chimp = mailchimp.Mailchimp(
            settings.SECRETS['mailchimp_api_key']
        )
        form = NewsletterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            subscription = Subscription(email=email)
            subscription.save()
            try:
                mail_chimp.lists.subscribe(
                    settings.SECRETS['mailchimp_list_id'],
                    {'email': email},
                    double_optin=False,
                    send_welcome=True
                )
            except mailchimp.ListAlreadySubscribedError:
                return {
                    'success': False, 'error': 'Email is already subscribed'
                }
            return {'success': True}
        else:
            return {
                'success': False,
                'error': form.errors.get('email')
            }
    return {'success': False, 'error': 'Request not valid'}
コード例 #4
0
    def test_subscribe_request_activate_form_loophole(self):
        """
        Prevent updating to unconfirmed email address. (#108)
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        activate_url = subscription.subscribe_activate_url()

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        testname2 = 'Test Name2'
        testemail2 = '*****@*****.**'
        response = self.client.post(activate_url, {
            'name_field': testname2,
            'email_field': testemail2,
            'user_activation_code': subscription.activation_code
        })

        # Assure we are redirected to "update activated" page.
        self.assertRedirects(response, self.subscribe_activated_url)

        subscription = Subscription.objects.get(pk=subscription.pk)

        self.assertTrue(subscription)
        self.assertTrue(subscription.subscribed)
        self.assertEqual(subscription.email, self.testemail)
コード例 #5
0
    def test_unsubscribe_request_post_no_email(self):
        """
        Post the unsubscribe request form
        with confirmation email switched off in settings.
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(
            self.unsubscribe_url, {'email_field': self.testemail}
        )

        # Assure we are redirected to "unsubscribe activated" page.
        self.assertRedirects(response, self.unsubscribe_activated_url)

        changed_subscription = self.get_only_subscription(
            email_field__exact=self.testemail
        )

        # email confirmation is switched off,
        # so after unsubscribe request user should be unsubscribed
        self.assertFalse(changed_subscription.subscribed)
        self.assertTrue(changed_subscription.unsubscribed)

        """ Check the subscription email. """
        # no email should be send
        self.assertEqual(len(mail.outbox), 0)
コード例 #6
0
def importUsers():

    try:
        newsl = Newsletter.objects.get(slug='emif-catalogue-newsletter')


        users = User.objects.all()

        for user in users:
            try:
                subscription = Subscription.objects.get(user=user,  newsletter=newsl)

            except Subscription.DoesNotExist:
                # create subscription if doesnt exist yet
                user_sub = Subscription(user=user,  newsletter=newsl)

                user_sub.subscribe()

                user_sub.save()

                print "-- Created subscription for emif newsletter to user "+str(user.username)

                pass

    except Newsletter.DoesNotExist:
        print "Problem finding default newsletter for emif"
コード例 #7
0
    def test_unsubscribe_request_post_error(self):
        """
        Test whether a failing unsubscribe request email generated an error in
        the context.

        We do this by overriding the default mail backend to a settings which
        we know pretty sure is bound to fail.
        """
        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        with override_settings(
            EMAIL_BACKEND='tests.utils.FailingEmailBackend'
        ):
            with patch_logger('newsletter.views', 'error') as messages:
                response = self.client.post(
                    self.unsubscribe_url, {'email_field': self.testemail}
                )
            self.assertEqual(len(messages), 1)
            self.assertIn("Connection refused", messages[0])

        self.assertTrue(response.context['error'])
コード例 #8
0
    def test_unsubscribe_view(self):
        """ Test the unsubscription form. """
        subscription = Subscription(user=self.user, newsletter=self.n)
        subscription.subscribed = True
        subscription.unsubscribed = False
        subscription.save()

        self.assertLessThan(
            subscription.subscribe_date, timezone.now() + timedelta(seconds=1)
        )

        response = self.client.get(self.unsubscribe_url)

        self.assertContains(response, self.n.title, status_code=200)

        self.assertEqual(response.context['newsletter'], self.n)
        self.assertEqual(response.context['user'], self.user)

        self.assertContains(
            response, 'action="%s"' % self.unsubscribe_confirm_url)
        self.assertContains(response, 'id="id_submit"')

        subscription = self.get_user_subscription()
        self.assertTrue(subscription.subscribed)
        self.assertFalse(subscription.unsubscribed)
コード例 #9
0
ファイル: models.py プロジェクト: bastiao/catalogue
def add_invited(user):
    print "ADD INVITED SUBSCRIPTIONS"
    ## Add to subscription list

    # get newsletter
    try:
        newsl = Newsletter.objects.get(slug='emif-catalogue-newsletter')

        # create subscription
        user_sub = Subscription(user=user,  newsletter=newsl)

        user_sub.subscribe()

        user_sub.save()

    except Newsletter.DoesNotExist:
        print "Problem registering new user to emif default newsletter"

    # add invited dbs if any
    sps = InvitePending.objects.filter(email=user.email)

    for sp in sps:

        fingerprint = sp.fingerprint

        fingerprint.save()

        fingerprint.shared.add(user)

        sp.delete()

        # must reindex, because databases lists come from solr, to update user_t
        fingerprint.indexFingerprint()
コード例 #10
0
    def test_unsubscribe_request_activate(self):
        """ Update a request. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        activate_url = subscription.unsubscribe_activate_url()

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        testname2 = 'Test Name2'
        response = self.client.post(activate_url, {
            'name_field': testname2,
            'user_activation_code': subscription.activation_code
        })

        # Assure we are redirected to "unsubscribe activated" page.
        self.assertRedirects(response, self.unsubscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail
        )

        self.assertTrue(subscription.unsubscribed)
        self.assertEqual(subscription.name, testname2)
        self.assertEqual(subscription.email, self.testemail)

        dt = (timezone.now() - subscription.unsubscribe_date).seconds
        self.assertLessThan(dt, 2)
コード例 #11
0
    def test_update_request_activate(self):
        """ Update a request. """

        subscription = Subscription(newsletter=self.n,
                                    name='Test Name',
                                    email='*****@*****.**')
        subscription.save()

        activate_url = subscription.update_activate_url()

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        testname2 = 'Test Name2'
        testemail2 = '*****@*****.**'
        response = self.client.post(
            activate_url, {
                'name_field': testname2,
                'email_field': testemail2,
                'user_activation_code': subscription.activation_code
            })

        # Assure we are redirected to "update activated" page.
        self.assertRedirects(response, self.update_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=testemail2)

        self.assertTrue(subscription)
        self.assertTrue(subscription.subscribed)
        self.assertEqual(subscription.name, testname2)
        self.assertEqual(subscription.email, testemail2)
コード例 #12
0
    def test_subscribe_request_activate_form_loophole(self):
        """
        Prevent updating to unconfirmed email address. (#108)
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        activate_url = subscription.subscribe_activate_url()

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        testname2 = 'Test Name2'
        testemail2 = '*****@*****.**'
        response = self.client.post(
            activate_url, {
                'name_field': testname2,
                'email_field': testemail2,
                'user_activation_code': subscription.activation_code
            })

        # Assure we are redirected to "update activated" page.
        self.assertRedirects(response, self.subscribe_activated_url)

        subscription = Subscription.objects.get(pk=subscription.pk)

        self.assertTrue(subscription)
        self.assertTrue(subscription.subscribed)
        self.assertEqual(subscription.email, self.testemail)
コード例 #13
0
    def test_unsubscribe_request_post_error(self):
        """
        Test whether a failing unsubscribe request email generated an error in
        the context.

        We do this by overriding the default mail backend to a settings which
        we know pretty sure is bound to fail.
        """
        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        with override_settings(
            EMAIL_BACKEND='tests.utils.FailingEmailBackend'
        ):
            with self.assertLogs('newsletter.views', 'ERROR') as messages:
                response = self.client.post(
                    self.unsubscribe_url, {'email_field': self.testemail}
                )
            self.assertEqual(len(messages.output), 1)
            self.assertIn("Connection refused", messages.output[0])

        self.assertTrue(response.context['error'])
コード例 #14
0
    def test_unsubscribe_request_post_no_email(self):
        """
        Post the unsubscribe request form
        with confirmation email switched off in settings.
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(self.unsubscribe_url,
                                    {'email_field': self.testemail})

        # Assure we are redirected to "unsubscribe activated" page.
        self.assertRedirects(response, self.unsubscribe_activated_url)

        changed_subscription = self.get_only_subscription(
            email_field__exact=self.testemail)

        # email confirmation is switched off,
        # so after unsubscribe request user should be unsubscribed
        self.assertFalse(changed_subscription.subscribed)
        self.assertTrue(changed_subscription.unsubscribed)
        """ Check the subscription email. """
        # no email should be send
        self.assertEqual(len(mail.outbox), 0)
コード例 #15
0
    def test_unsubscribe_view(self):
        """ Test the unsubscription form. """
        subscription = Subscription(user=self.user, newsletter=self.n)
        subscription.subscribed = True
        subscription.unsubscribed = False
        subscription.save()

        self.assertLessThan(
            subscription.subscribe_date, timezone.now() + timedelta(seconds=1)
        )

        response = self.client.get(self.unsubscribe_url)

        self.assertContains(response, self.n.title, status_code=200)

        self.assertEqual(response.context['newsletter'], self.n)
        self.assertEqual(response.context['user'], self.user)

        self.assertContains(
            response, 'action="%s"' % self.unsubscribe_confirm_url)
        self.assertContains(response, 'id="id_submit"')

        subscription = self.get_user_subscription()
        self.assertTrue(subscription.subscribed)
        self.assertFalse(subscription.unsubscribed)
コード例 #16
0
    def test_unsubscribe_request_activate(self):
        """ Update a request. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        activate_url = subscription.unsubscribe_activate_url()

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        testname2 = 'Test Name2'
        response = self.client.post(
            activate_url, {
                'name_field': testname2,
                'user_activation_code': subscription.activation_code
            })

        # Assure we are redirected to "unsubscribe activated" page.
        self.assertRedirects(response, self.unsubscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail)

        self.assertTrue(subscription.unsubscribed)
        self.assertEqual(subscription.name, testname2)
        self.assertEqual(subscription.email, self.testemail)

        dt = (timezone.now() - subscription.unsubscribe_date).seconds
        self.assertLessThan(dt, 2)
コード例 #17
0
ファイル: test_web.py プロジェクト: sha-red/django-newsletter
    def test_update_request_activate(self):
        """ Update a request. """

        subscription = Subscription(newsletter=self.n,
                                    name='Test Name',
                                    email='*****@*****.**')
        subscription.save()

        activate_url = subscription.update_activate_url()

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        testname2 = 'Test Name2'
        testemail2 = '*****@*****.**'
        response = self.client.post(activate_url, {
            'name_field': testname2,
            'email_field': testemail2,
            'user_activation_code': subscription.activation_code
        })

        # Assure we are redirected to "update activated" page.
        self.assertRedirects(response, self.update_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=testemail2
        )

        self.assertTrue(subscription)
        self.assertTrue(subscription.subscribed)
        self.assertEqual(subscription.name, testname2)
        self.assertEqual(subscription.email, testemail2)
コード例 #18
0
ファイル: views.py プロジェクト: weijian0955/ryabina
 def form_valid(self, form):
     newsletter = self.get_services_newsletter()
     email = form.cleaned_data['email']
     try:
         subscription = Subscription.objects.get(email_field=email,
                                                 newsletter=newsletter)
     except Subscription.DoesNotExist:
         subscription = Subscription()
         subscription.email = form.cleaned_data['email']
         subscription.name = form.cleaned_data['name']
         subscription.newsletter = self.get_services_newsletter()
         subscription.save()
         subscription.send_activation_email(action='subscribe')
         messages.success(
             self.request,
             u'Вы подписались на уведомления о новых акциях стоматологического центра "Рябина"!'
         )
     else:
         if subscription.subscribed:
             messages.success(
                 self.request,
                 u'Вы уже подписаны на уведомления о новых акциях стоматологического центра "Рябина"!'
             )
         else:
             subscription.send_activation_email(action='subscribe')
             messages.success(
                 self.request,
                 u'Мы выслали вам письмо активации на уведомления о новых акциях стоматологического центра "Рябина"!'
             )
     return HttpResponseRedirect(self.get_success_url())
コード例 #19
0
    def test_subscribe_unsubscribed(self):
        """
        After having been unsubscribed, a user should be able to subscribe
        again.

        This relates to #5 on GitHub.
        """

        # Create a subscription
        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        # Unsubscribe
        response = self.client.post(
            subscription.unsubscribe_activate_url(), {
                'name_field': subscription.name,
                'email_field': subscription.email,
                'user_activation_code': subscription.activation_code
            })

        # Assure we are redirected to "unsubscribe activated" page.
        self.assertRedirects(response, self.unsubscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail)

        self.assertFalse(subscription.subscribed)
        self.assertTrue(subscription.unsubscribed)

        # Resubscribe request
        response = self.client.post(self.subscribe_url, {
            'name_field': subscription.name,
            'email_field': subscription.email,
        })

        # Assure we are redirected to "email sent page"
        self.assertRedirects(response, self.subscribe_email_sent_url)

        # self.assertFalse(subscription.subscribed)
        self.assertTrue(subscription.unsubscribed)

        # Activate subscription
        response = self.client.post(
            subscription.subscribe_activate_url(), {
                'name_field': subscription.name,
                'email_field': subscription.email,
                'user_activation_code': subscription.activation_code
            })

        # Assure we are redirected to "subscribe activated" page.
        self.assertRedirects(response, self.subscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail)

        self.assertTrue(subscription.subscribed)
        self.assertFalse(subscription.unsubscribed)
コード例 #20
0
 def import_auth_users(self, *args, **options):
     auth_list, created = List.objects.get_or_create(name="django_auth")
     users = User.objects.all()
     for u in users:
         try:
             s = Subscription.objects.get(user=u,list=auth_list)
             print "skipping, user alread loaded from auth: ",s
         except Subscription.DoesNotExist:
             print "adding new user from auth: ",u
             subscriber = Subscription(user=u,list=auth_list,subscribed=True)
             subscriber.save()
コード例 #21
0
ファイル: views.py プロジェクト: LittleForker/django-minishop
def subscribe(request):
  if request.method == 'POST':
    from django.forms.fields import email_re
    if email_re.match(request.POST['email']):
      sub = Subscription(email=request.POST['email'])
      sub.save()
      create_message(request, "Dodano do newslettera")
      return conditional_redirect(request, '/')
    else:
      create_message(request, "Błędny adres e-mail")
      return conditional_redirect(request, '/')
  else:
      return conditional_redirect(request, '/')
コード例 #22
0
    def test_twosubmissions_unsubscried(self):
        """ Test submission with two subscribers, one unactivated. """

        s2 = Subscription(name='Test Name 2',
                          email='*****@*****.**',
                          newsletter=self.n,
                          subscribed=False)
        s2.save()

        sub = Submission.from_message(self.m)

        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [self.s])
コード例 #23
0
    def test_twosubmissions_unsubscried(self):
        """ Test submission with two subscribers, one unactivated. """

        s2 = Subscription(
            name='Test Name 2', email='*****@*****.**',
            newsletter=self.n, subscribed=False
        )
        s2.save()

        sub = Submission.from_message(self.m)

        subscriptions = sub.subscriptions.all()
        self.assertEqual(list(subscriptions), [self.s])
コード例 #24
0
ファイル: views.py プロジェクト: akilraj1255/django-minishop
def subscribe(request):
    if request.method == 'POST':
        from django.forms.fields import email_re
        if email_re.match(request.POST['email']):
            sub = Subscription(email=request.POST['email'])
            sub.save()
            create_message(request, "Dodano do newslettera")
            return conditional_redirect(request, '/')
        else:
            create_message(request, "Błędny adres e-mail")
            return conditional_redirect(request, '/')
    else:
        return conditional_redirect(request, '/')
コード例 #25
0
    def test_twosubmissions(self):
        """ Test submission with two activated subscribers. """

        s2 = Subscription(
            name='Test Name 2', email='*****@*****.**',
            newsletter=self.n, subscribed=True
        )
        s2.save()

        sub = Submission.from_message(self.m)

        subscriptions = sub.subscriptions.all()
        self.assertTrue(self.s in list(subscriptions))
        self.assertTrue(s2 in list(subscriptions))
コード例 #26
0
ファイル: views.py プロジェクト: retro/kset-org
def subscribe(request):
    if 'subscription' in request.POST:
        subscription = Subscription(email = request.POST['subscription'])
        subscriptions = Subscription.objects.filter(email = request.POST['subscription'])

        try:
            subscription.full_clean()
            subscription.save()
            return HttpResponse('Uspješno ste se pretplatili!')
        except ValidationError, e:
            if (subscriptions.count() != 0):
              return HttpResponse('E-mail adresa je vec pretplacena!')
            else:
              return HttpResponse('Došlo je do pogreške!')
コード例 #27
0
    def test_unsubscribe_update_unactivated(self):
        """ Test updating unsubscribed subscriptions view. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=False)
        subscription.save()

        for url in (self.update_url, self.unsubscribe_url):
            response = self.client.post(url, {'email_field': self.testemail})

            self.assertContains(
                response, "This subscription has not yet been activated.")
コード例 #28
0
    def test_twosubmissions(self):
        """ Test submission with two activated subscribers. """

        s2 = Subscription(name='Test Name 2',
                          email='*****@*****.**',
                          newsletter=self.n,
                          subscribed=True)
        s2.save()

        sub = Submission.from_message(self.m)

        subscriptions = sub.subscriptions.all()
        self.assertTrue(self.s in list(subscriptions))
        self.assertTrue(s2 in list(subscriptions))
コード例 #29
0
class SubscriptionTestCase(UserTestCase, MailingTestCase):
    def setUp(self):
        super().setUp()

        self.us = Subscription(user=self.user, newsletter=self.n)
        self.us.save()

        self.ns = Subscription(
            name='Test susbcriber', newsletter=self.n,
            email='*****@*****.**'
        )
        self.ns.save()

        self.ss = [self.us, self.ns]

    def test_usersubscription(self):
        self.assertEqual(self.us.name, self.user.get_full_name())
        self.assertEqual(self.us.email, self.user.email)

    def test_subscribe_unsubscribe(self):
        for s in self.ss:
            self.assertFalse(s.subscribed)
            self.assertFalse(s.unsubscribed)
            self.assertFalse(s.subscribe_date)
            self.assertFalse(s.unsubscribe_date)

            # Repeat this to ensure consequencentness
            for x in range(2):
                s.subscribed = True
                s.save()

                self.assertTrue(s.subscribed)
                self.assertTrue(s.subscribe_date)
                self.assertFalse(s.unsubscribed)
                old_subscribe_date = s.subscribe_date

                s.unsubscribed = True
                s.save()

                self.assertFalse(s.subscribed)
                self.assertTrue(s.unsubscribed)
                self.assertTrue(s.unsubscribe_date)

                s.unsubscribed = False
                s.save()

                self.assertFalse(s.unsubscribed)
                self.assertTrue(s.subscribed)
                self.assertNotEqual(s.subscribe_date, old_subscribe_date)
コード例 #30
0
class SubscriptionTestCase(UserTestCase, MailingTestCase):
    def setUp(self):
        super(SubscriptionTestCase, self).setUp()

        self.us = Subscription(user=self.user, newsletter=self.n)
        self.us.save()

        self.ns = Subscription(
            name='Test susbcriber', newsletter=self.n,
            email='*****@*****.**'
        )
        self.ns.save()

        self.ss = [self.us, self.ns]

    def test_usersubscription(self):
        self.assertEqual(self.us.name, self.user.get_full_name())
        self.assertEqual(self.us.email, self.user.email)

    def test_subscribe_unsubscribe(self):
        for s in self.ss:
            self.assertFalse(s.subscribed)
            self.assertFalse(s.unsubscribed)
            self.assertFalse(s.subscribe_date)
            self.assertFalse(s.unsubscribe_date)

            # Repeat this to ensure consequencentness
            for x in range(2):
                s.subscribed = True
                s.save()

                self.assertTrue(s.subscribed)
                self.assertTrue(s.subscribe_date)
                self.assertFalse(s.unsubscribed)
                old_subscribe_date = s.subscribe_date

                s.unsubscribed = True
                s.save()

                self.assertFalse(s.subscribed)
                self.assertTrue(s.unsubscribed)
                self.assertTrue(s.unsubscribe_date)

                s.unsubscribed = False
                s.save()

                self.assertFalse(s.unsubscribed)
                self.assertTrue(s.subscribed)
                self.assertNotEqual(s.subscribe_date, old_subscribe_date)
コード例 #31
0
ファイル: models.py プロジェクト: bastiao/catalogue
    def setNewsletterSubs(self, new_status):
        newsl = self.getNewsletter()

        newsl_sub = None
        try:
            newsl_sub = Subscription.objects.get(user=self.user,  newsletter=newsl)
        except Subscription.DoesNotExist:
            newsl_sub = Subscription(user=self.user, newsletter = newsl)

        if(new_status):
            newsl_sub.subscribe()
        else:
            newsl_sub.unsubscribe()

        newsl_sub.save()
コード例 #32
0
    def setNewsletterSubs(self, new_status):
        newsl = self.getNewsletter()

        newsl_sub = None
        try:
            newsl_sub = Subscription.objects.get(user=self.user,
                                                 newsletter=newsl)
        except Subscription.DoesNotExist:
            newsl_sub = Subscription(user=self.user, newsletter=newsl)

        if (new_status):
            newsl_sub.subscribe()
        else:
            newsl_sub.unsubscribe()

        newsl_sub.save()
コード例 #33
0
    def test_unsubscribe_twice(self):
        subscription = Subscription(user=self.user, newsletter=self.n)
        subscription.subscribed = False
        subscription.unsubscribed = True
        subscription.save()

        response = self.client.get(self.unsubscribe_url)

        self.assertContains(response, self.n.title, status_code=200)

        self.assertEqual(response.context['newsletter'], self.n)
        self.assertEqual(response.context['user'], self.user)

        self.assertNotContains(response,
                               'action="%s"' % self.unsubscribe_confirm_url)
        self.assertNotContains(response, 'id="id_submit"')
コード例 #34
0
    def test_subscribe_twice(self):
        """ Subscribing twice should not be possible. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(self.subscribe_url, {
            'name_field': self.testname,
            'email_field': self.testemail
        })

        self.assertContains(response,
                            "already been subscribed to",
                            status_code=200)
コード例 #35
0
    def test_unsubscribe_update_unactivated(self):
        """ Test updating unsubscribed subscriptions view. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=False)
        subscription.save()

        for url in (self.update_url, self.unsubscribe_url):
            response = self.client.post(
                url, {'email_field': self.testemail}
            )

            self.assertContains(
                response, "This subscription has not yet been activated."
            )
コード例 #36
0
    def test_subscribe_twice(self):
        # After subscribing we should not be able to subscribe again
        subscription = Subscription(user=self.user, newsletter=self.n)
        subscription.subscribed = True
        subscription.unsubscribed = False
        subscription.save()

        response = self.client.get(self.subscribe_url)

        self.assertContains(response, self.n.title, status_code=200)

        self.assertEqual(response.context['newsletter'], self.n)
        self.assertEqual(response.context['user'], self.user)

        self.assertNotContains(
            response, 'action="%s"' % self.subscribe_confirm_url)
        self.assertNotContains(response, 'id="id_submit"')
コード例 #37
0
    def test_subscribe_twice(self):
        """ Subscribing twice should not be possible. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(
            self.subscribe_url, {
                'name_field': self.testname,
                'email_field': self.testemail
            }
        )

        self.assertContains(response, "already been subscribed to",
                            status_code=200)
コード例 #38
0
    def test_update_request_post_no_email(self):
        """
        Test the update request post view
        with confirmation email switched off in settings.
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(self.update_url,
                                    {'email_field': self.testemail})

        self.assertRedirects(response, subscription.update_activate_url())
        """ Check the subscription email. """
        # no email should be send
        self.assertEqual(len(mail.outbox), 0)
コード例 #39
0
class MailingTestCase(MailTestCase):

    def get_newsletter_kwargs(self):
        """ Returns the keyword arguments for instanciating the newsletter. """

        return {
            'title': 'Test newsletter',
            'slug': 'test-newsletter',
            'sender': 'Test Sender',
            'email': '*****@*****.**'
        }

    def setUp(self):
        self.n = Newsletter(**self.get_newsletter_kwargs())
        self.n.save()
        self.n.site = get_default_sites()

        self.m = Message(title='Test message',
                         newsletter=self.n,
                         slug='test-message')
        self.m.save()

        self.s = Subscription(
            name='Test Name', email='*****@*****.**',
            newsletter=self.n, subscribed=True
        )
        self.s.save()

    def send_email(self, action):
        assert action in ACTIONS + ('message', ), 'Unknown action: %s' % action

        if action == 'message':
            # Create submission
            sub = Submission.from_message(self.m)
            sub.prepared = True
            sub.publish_date = now() - timedelta(seconds=1)
            sub.save()

            # Send message email
            Submission.submit_queue()
        else:
            self.s.send_activation_email(action)
コード例 #40
0
ファイル: views.py プロジェクト: sinyawskiy/ryabina
 def form_valid(self, form):
     newsletter = self.get_services_newsletter()
     email = form.cleaned_data['email']
     try:
         subscription = Subscription.objects.get(email_field=email, newsletter=newsletter)
     except Subscription.DoesNotExist:
         subscription = Subscription()
         subscription.email = form.cleaned_data['email']
         subscription.name = form.cleaned_data['name']
         subscription.newsletter = self.get_services_newsletter()
         subscription.save()
         subscription.send_activation_email(action='subscribe')
         messages.success(self.request, u'Вы подписались на уведомления о новых акциях стоматологического центра "Рябина"!')
     else:
         if subscription.subscribed:
             messages.success(self.request, u'Вы уже подписаны на уведомления о новых акциях стоматологического центра "Рябина"!')
         else:
             subscription.send_activation_email(action='subscribe')
             messages.success(self.request, u'Мы выслали вам письмо активации на уведомления о новых акциях стоматологического центра "Рябина"!')
     return HttpResponseRedirect(self.get_success_url())
コード例 #41
0
class MailingTestCase(MailTestCase):
    def get_newsletter_kwargs(self):
        """ Returns the keyword arguments for instanciating the newsletter. """

        return {
            'title': 'Test newsletter',
            'slug': 'test-newsletter',
            'sender': 'Test Sender',
            'email': '*****@*****.**'
        }

    def setUp(self):
        self.n = Newsletter(**self.get_newsletter_kwargs())
        self.n.save()
        self.n.site = get_default_sites()

        self.m = Message(title='Test message',
                         newsletter=self.n,
                         slug='test-message')
        self.m.save()

        self.s = Subscription(name='Test Name',
                              email='*****@*****.**',
                              newsletter=self.n,
                              subscribed=True)
        self.s.save()

    def send_email(self, action):
        assert action in ACTIONS + ('message', ), 'Unknown action: %s' % action

        if action == 'message':
            # Create submission
            sub = Submission.from_message(self.m)
            sub.prepared = True
            sub.publish_date = now() - timedelta(seconds=1)
            sub.save()

            # Send message email
            Submission.submit_queue()
        else:
            self.s.send_activation_email(action)
コード例 #42
0
    def test_update_request_post_no_email(self):
        """
        Test the update request post view
        with confirmation email switched off in settings.
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(
            self.update_url, {'email_field': self.testemail}
        )

        self.assertRedirects(response, subscription.update_activate_url())

        """ Check the subscription email. """
        # no email should be send
        self.assertEqual(len(mail.outbox), 0)
コード例 #43
0
    def test_update_request_activate_form(self):
        """
        Test requesting a form for activating an update without activation
        code in the URL.
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        activate_url = reverse('newsletter_update', kwargs={
            'newsletter_slug': self.n.slug,
            'action': 'update',
            'email': subscription.email
        })

        response = self.client.get(activate_url)

        # Make sure the form is there
        self.assertInContext(response, 'form', UpdateForm)
コード例 #44
0
    def test_update_request_activate_form(self):
        """
        Test requesting a form for activating an update without activation
        code in the URL.
        """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        activate_url = reverse('newsletter_update', kwargs={
            'newsletter_slug': self.n.slug,
            'action': 'update',
            'email': subscription.email
        })

        response = self.client.get(activate_url)

        # Make sure the form is there
        self.assertInContext(response, 'form', UpdateForm)
コード例 #45
0
    def test_update_request_post(self):
        """ Test the update request post view. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(self.update_url,
                                    {'email_field': self.testemail})

        # Assure we are redirected to "update activation email sent" page.
        self.assertRedirects(response, self.update_email_sent_url)
        """ Check the subscription email. """
        self.assertEqual(len(mail.outbox), 1)

        activate_url = subscription.update_activate_url()
        full_activate_url = 'http://%s%s' % (self.site.domain, activate_url)

        self.assertEmailContains(full_activate_url)
コード例 #46
0
    def test_subscribe_request_activate(self):
        """ Test subscription activation. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        time.sleep(WAIT_TIME)

        self.assertFalse(subscription.subscribed)

        activate_url = subscription.subscribe_activate_url()
        self.assertTrue(activate_url)

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        response = self.client.post(
            activate_url, {
                'name_field': 'Test Name',
                'email_field': self.testemail,
                'user_activation_code': subscription.activation_code
            }
        )

        # Assure we are redirected to "subscribe activated" page.
        self.assertRedirects(response, self.subscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail
        )

        self.assertTrue(subscription.subscribed)
        self.assertFalse(subscription.unsubscribed)

        dt = (subscription.subscribe_date - subscription.create_date).seconds
        self.assertBetween(dt, WAIT_TIME, WAIT_TIME + 1)
コード例 #47
0
    def test_subscribe_request_activate(self):
        """ Test subscription activation. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail)
        subscription.save()

        time.sleep(WAIT_TIME)

        self.assertFalse(subscription.subscribed)

        activate_url = subscription.subscribe_activate_url()
        self.assertTrue(activate_url)

        response = self.client.get(activate_url)
        self.assertInContext(response, 'form', UpdateForm)
        self.assertContains(response, subscription.activation_code)

        response = self.client.post(
            activate_url, {
                'name_field': 'Test Name',
                'email_field': self.testemail,
                'user_activation_code': subscription.activation_code
            }
        )

        # Assure we are redirected to "subscribe activated" page.
        self.assertRedirects(response, self.subscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail
        )

        self.assertTrue(subscription.subscribed)
        self.assertFalse(subscription.unsubscribed)

        dt = (subscription.subscribe_date - subscription.create_date).seconds
        self.assertBetween(dt, WAIT_TIME, WAIT_TIME + 1)
コード例 #48
0
    def test_unsubscribe_request_post(self):
        """ Post the unsubscribe request form. """

        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        response = self.client.post(
            self.unsubscribe_url, {'email_field': self.testemail}
        )

        # Assure we are redirected to "unsubscribe activation email sent" page.
        self.assertRedirects(response, self.unsubscribe_email_sent_url)

        """ Check the subscription email. """
        self.assertEqual(len(mail.outbox), 1)

        activate_url = subscription.unsubscribe_activate_url()
        full_activate_url = 'http://%s%s' % (self.site.domain, activate_url)

        self.assertEmailContains(full_activate_url)
コード例 #49
0
ファイル: test_web.py プロジェクト: sha-red/django-newsletter
    def test_update_request_post(self):
        """ Test the update request post view. """

        subscription = Subscription(newsletter=self.n,
                                    name='Test Name',
                                    email='*****@*****.**',
                                    subscribed=True)
        subscription.save()

        response = self.client.post(
            self.update_url, {'email_field': '*****@*****.**'}
        )

        # Assure we are redirected to "update activation email sent" page.
        self.assertRedirects(response, self.update_email_sent_url)

        """ Check the subscription email. """
        self.assertEqual(len(mail.outbox), 1)

        activate_url = subscription.update_activate_url()
        full_activate_url = 'http://%s%s' % (self.site.domain, activate_url)

        self.assertEmailContains(full_activate_url)
コード例 #50
0
    def test_subscribe_unsubscribed(self):
        """
        After having been unsubscribed, a user should be able to subscribe
        again.

        This relates to #5 on GitHub.
        """

        # Create a subscription
        subscription = Subscription(newsletter=self.n,
                                    name=self.testname,
                                    email=self.testemail,
                                    subscribed=True)
        subscription.save()

        # Unsubscribe
        response = self.client.post(
            subscription.unsubscribe_activate_url(),
            {
                'name_field': subscription.name,
                'email_field': subscription.email,
                'user_activation_code': subscription.activation_code
            }
        )

        # Assure we are redirected to "unsubscribe activated" page.
        self.assertRedirects(response, self.unsubscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail
        )

        self.assertFalse(subscription.subscribed)
        self.assertTrue(subscription.unsubscribed)

        # Resubscribe request
        response = self.client.post(
            self.subscribe_url,
            {
                'name_field': subscription.name,
                'email_field': subscription.email,
            }
        )

        # Assure we are redirected to "email sent page"
        self.assertRedirects(response, self.subscribe_email_sent_url)

        # self.assertFalse(subscription.subscribed)
        self.assertTrue(subscription.unsubscribed)

        # Activate subscription
        response = self.client.post(
            subscription.subscribe_activate_url(),
            {
                'name_field': subscription.name,
                'email_field': subscription.email,
                'user_activation_code': subscription.activation_code
            }
        )

        # Assure we are redirected to "subscribe activated" page.
        self.assertRedirects(response, self.subscribe_activated_url)

        subscription = self.get_only_subscription(
            email_field__exact=self.testemail
        )

        self.assertTrue(subscription.subscribed)
        self.assertFalse(subscription.unsubscribed)