<commit_before># -*- coding: utf-8 -*-

import base64
import logging

#from django.utils.translation import ugettext_lazy as _
from django.template import RequestContext
from django.shortcuts import render
from django.http import HttpResponseNotFound
from django.views.decorators.cache import never_cache
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _

from ..landing.models import LandingUser

LOGGER = logging.getLogger(__name__)

User = get_user_model()


@never_cache
def unsubscribe(request, email, hash_code):

    email = base64.b64decode(email)

    for klass in LandingUser, User:
        try:
            user = klass.objects.get(email=email)

        except:
            continue

        if hash_code == user.hash_codes.get('unsubscribe', None):
            user.email_announcements = False
            user.renew_hash_code('unsubscribe', commit=False)
            user.save(update_fields=('hash_codes', 'email_announcements', ))

            return render(request, 'unsubscribe.html',
                          RequestContext(request, {'email': user.email}))

    return HttpResponseNotFound(_('Invalid email or unsubscribe token.'))
<commit_msg>Emit a warning when a user unregisters from email announcements. Should be catched on sentry.<commit_after># -*- coding: utf-8 -*-

import base64
import logging

#from django.utils.translation import ugettext_lazy as _
from django.template import RequestContext
from django.shortcuts import render
from django.http import HttpResponseNotFound
from django.views.decorators.cache import never_cache
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _

from ..landing.models import LandingUser

LOGGER = logging.getLogger(__name__)

User = get_user_model()


@never_cache
def unsubscribe(request, email, hash_code):

    email = base64.b64decode(email)

    for klass in LandingUser, User:
        try:
            user = klass.objects.get(email=email)

        except:
            continue

        if hash_code == user.hash_codes.get('unsubscribe', None):
            user.email_announcements = False
            user.renew_hash_code('unsubscribe', commit=False)
            user.save(update_fields=('hash_codes', 'email_announcements', ))

            LOGGER.warning(u'%s %s (%s) just unregistered from email '
                           u'announcements.', klass.__name__, user.username,
                           user.id)

            return render(request, 'unsubscribe.html',
                          RequestContext(request, {'email': user.email}))

    return HttpResponseNotFound(_('Invalid email or unsubscribe token.'))

{'fraction_non_alphanumeric': 0.08868274582560297, 'fraction_numerical': 0.005194805194805195, 'mean_word_length': 3.1540832049306626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7454894', 'n_tokens_mistral': 749, 'n_tokens_neox': 730, 'n_words': 193}