def unsubscribe(request): id = request.GET['id'] id_type = request.GET.get('id_type', 'user') code = request.GET['code'] pub = request.GET.get('pub') pub_id = request.GET.get('pub_id') if id_type == 'user': try: user = User.objects.get(id=id, is_active=True) except ObjectDoesNotExist: raise Http404 elif id_type == 'donor': try: user = Donor.objects.get(id=id) except ObjectDoesNotExist: raise Http404 else: raise Http404 expected_code = salted_hash(user.email) if code <> expected_code: raise Http404 if pub: pub = pubs[pub] publication_id = pub.publication_id elif pub_id: publication_id = pub_id else: raise Http404 pub = Publication.objects.get(id=publication_id) unsub, created = Unsubscribe.objects.get_or_create(email=user.email, publication_id=pub.id) if pub.user_settings_field: # Update user table anyway to allow for resubscription, etc. setattr(user, pub.user_settings_field, 0) user.save() cache.bust_on_handle(user, user.username) else: try: if id_type == 'donor': sub = Subscription.objects.get(donor=user.id, publication=pub.id) elif id_type == 'user': sub = Subscription.objects.get(user=user.id, publication=pub.id) sub.subscribed = False sub.save() #cache.bust([user, sub]) except ObjectDoesNotExist: raise Http404 return render(request, 'etc/unsubscribe.html', { 'created': created, 'user': user, })
def unsubscribe(request): id = request.GET['id'] id_type = request.GET.get('id_type', 'user') code = request.GET['code'] pub = request.GET.get('pub') pub_id = request.GET.get('pub_id') if id_type == 'user': try: user = User.objects.get(id=id, is_active=True) except ObjectDoesNotExist: raise Http404 elif id_type == 'donor': try: user = Donor.objects.get(id=id) except ObjectDoesNotExist: raise Http404 else: raise Http404 expected_code = salted_hash(user.email) if code <> expected_code: raise Http404 if pub: pub = pubs[pub] publication_id = pub.publication_id elif pub_id: publication_id = pub_id else: raise Http404 pub = Publication.objects.get(id=publication_id) unsub, created = Unsubscribe.objects.get_or_create(email=user.email, publication_id=pub.id) if pub.user_settings_field: # Update user table anyway to allow for resubscription, etc. setattr(user, pub.user_settings_field, 0) user.save() cache.bust_on_handle(user, user.username) else: try: if id_type=='donor': sub = Subscription.objects.get(donor=user.id, publication=pub.id) elif id_type=='user': sub = Subscription.objects.get(user=user.id, publication=pub.id) sub.subscribed=False sub.save() #cache.bust([user, sub]) except ObjectDoesNotExist: raise Http404 return render(request, 'etc/unsubscribe.html', {'created': created, 'user': user,})
def __init__(self, type, user=None, email_address=None, subject='', update_next_email_time=False, **kw): hostname = settings.HTTP_HOST email_address = email_address if email_address else user.email unsub_code = salted_hash(email_address) kw.update(dict(hostname=hostname, subject=subject, user=user, unsub_code=unsub_code)) self.subject = subject self.template_args = kw self.text_content = render_to_string('email/txt/%s.txt' % type, kw) self.html_content = render_to_string('email/html/%s.html' % type, kw) self.user = user self.update_next_email_time=update_next_email_time # Lock that down if settings.EMAIL_REAL_PEOPLE or email_address.endswith('@jumo.com'): self.to = email_address else: self.to = '*****@*****.**' self.from_address = 'Jumo <*****@*****.**>'
def email_hash(self): return salted_hash(self.email)