def view_newsletter_tracking(request, slug, uidb36, token, format):
    """Track the opening of the newsletter by requesting a blank img"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    ContactMailingStatus.objects.create(newsletter=newsletter,
                                        contact=contact,
                                        status=ContactMailingStatus.OPENED)
    return HttpResponse(base64.b64decode(TRACKING_IMAGE),
                        mimetype='image/%s' % format)
def view_newsletter_tracking(request, slug, uidb36, token, format):
    """Track the opening of the newsletter by requesting a blank img"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    ContactMailingStatus.objects.create(newsletter=newsletter,
                                        contact=contact,
                                        status=ContactMailingStatus.OPENED)
    return HttpResponse(base64.b64decode(TRACKING_IMAGE),
                        mimetype='image/%s' % format)
def view_newsletter_contact(request, slug, uidb36, token):
    """Visualization of a newsletter by an user"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    ContactMailingStatus.objects.create(
        newsletter=newsletter, contact=contact, status=ContactMailingStatus.OPENED_ON_SITE
    )
    context = {'contact': contact, 'uidb36': uidb36, 'token': token}

    return render_newsletter(request, slug, context)
def view_newsletter_contact(request, slug, uidb36, token):
    """Visualization of a newsletter by an user"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    ContactMailingStatus.objects.create(
        newsletter=newsletter,
        contact=contact,
        status=ContactMailingStatus.OPENED_ON_SITE)
    context = {'contact': contact, 'uidb36': uidb36, 'token': token}

    return render_newsletter(request, slug, context)
def view_mailinglist_unsubscribe(request, slug, uidb36, token):
    """Unsubscribe a contact to a mailing list"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)

    already_unsubscribed = contact in newsletter.mailing_list.unsubscribers.all()

    if request.POST.get('email') and not already_unsubscribed:
        newsletter.mailing_list.unsubscribers.add(contact)
        newsletter.mailing_list.save()
        already_unsubscribed = True
        ContactMailingStatus.objects.create(newsletter=newsletter, contact=contact,
                                            status=ContactMailingStatus.UNSUBSCRIPTION)

    return render_to_response('newsletter/mailing_list_unsubscribe.html',
                              {'email': contact.email,
                               'already_unsubscribed': already_unsubscribed},
                              context_instance=RequestContext(request))
def view_newsletter_tracking_link(request, slug, uidb36, token, link_id):
    """Track the opening of a link on the website"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    link = get_object_or_404(Link, pk=link_id)
    ContactMailingStatus.objects.create(newsletter=newsletter,
                                        contact=contact,
                                        status=ContactMailingStatus.LINK_OPENED,
                                        link=link)
    if not USE_UTM_TAGS:
        return HttpResponseRedirect(link.url)

    url_parts = urlparse(link.url)
    query_dict = parse_qs(url_parts.query)
    query_dict.update({'utm_source': 'newsletter_%s' % newsletter.pk,
                       'utm_medium': 'mail',
                       'utm_campaign': smart_str(newsletter.title)})
    url = urlunparse((url_parts.scheme, url_parts.netloc, url_parts.path,
                      url_parts.params, urlencode(query_dict), url_parts.fragment))
    return HttpResponseRedirect(url)
def view_newsletter_tracking_link(request, slug, uidb36, token, link_id):
    """Track the opening of a link on the website"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    link = get_object_or_404(Link, pk=link_id)
    ContactMailingStatus.objects.create(
        newsletter=newsletter,
        contact=contact,
        status=ContactMailingStatus.LINK_OPENED,
        link=link)
    if not USE_UTM_TAGS:
        return HttpResponseRedirect(link.url)

    url_parts = urlparse(link.url)
    query_dict = dict(parse_qsl(url_parts.query))
    query_dict.update({
        'utm_source': 'newsletter_%s' % newsletter.pk,
        'utm_medium': 'mail',
        'utm_campaign': smart_str(newsletter.title)
    })
    url = urlunparse(
        (url_parts.scheme, url_parts.netloc, url_parts.path, url_parts.params,
         urlencode(query_dict), url_parts.fragment))
    return HttpResponseRedirect(url)
def view_mailinglist_unsubscribe(request, slug, uidb36, token):
    """
    Unsubscribe a contact to one or all mailing lists
    """
    newsletter = get_object_or_404(Newsletter, slug=slug)
    contact = untokenize(uidb36, token)
    if UNSUBSCRIBE_ALL:
        mailing_lists = MailingList.objects.all()
        contact.subscriber = False
        contact.save()
    else:
        mailing_lists = [newsletter.mailing_list]

    unsubscribed = 0
    already_unsubscribed = False

    if request.POST.get('email'):
        for mailing_list in mailing_lists:
            mailing_list.unsubscribers.add(contact)
            mailing_list.subscribers.remove(contact)
            contact_unsubscribed.send(sender=contact, mailing_list=mailing_list)
            unsubscribed += 1

    if unsubscribed > 0:
        already_unsubscribed = True
        ContactMailingStatus.objects.create(
            newsletter=newsletter,
            contact=contact,
            status=ContactMailingStatus.UNSUBSCRIPTION
        )

    return render_to_response(
        'newsletter/mailing_list_unsubscribe.html',
        {'email': contact.email, 'unsubscribed_count':unsubscribed, 'already_unsubscribed': already_unsubscribed},
        context_instance=RequestContext(request)
    )
 def test_tokenize_untokenize(self):
     uidb36, token = tokenize(self.contact)
     self.assertEquals(untokenize(uidb36, token), self.contact)
     self.assertRaises(Http404, untokenize, 'toto', token)
     self.assertRaises(Http404, untokenize, uidb36, 'toto')
 def test_tokenize_untokenize(self):
     uidb36, token = tokenize(self.contact)
     self.assertEquals(untokenize(uidb36, token), self.contact)
     self.assertRaises(Http404, untokenize, 'toto', token)
     self.assertRaises(Http404, untokenize, uidb36, 'toto')