<commit_before>from functools import wraps
import json

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.conf import settings
import re

from tasks import (add_sms_user, update_user, confirm_user,
                   update_student_reps, SUBSCRIBE, UNSUBSCRIBE)
from newsletters import *
from models import Subscriber
from backends.exacttarget import (ExactTargetDataExt, NewsletterException,
                                  UnauthorizedException)


## Utility functions

def logged_in(f):
    """Decorator to check if the user has permission to view these
    pages"""

    @wraps(f)
    def wrapper(request, token, *args, **kwargs):
        subscriber = Subscriber.objects.filter(token=token)
        if not subscriber.exists():
            return json_response({
                'status': 'error',
                'desc': 'Must have valid token for this request',
            }, status=403)

        request.subscriber = subscriber[0]
        return f(request, token, *args, **kwargs)
    return wrapper


def json_response(data, status=200):
    res = HttpResponse(json.dumps(data),
                       mimetype='application/json')
    res.status_code = status
    return res


def update_user_task(request, type, data=None, optin=True):
    """Call the update_user task async with the right parameters"""

    user = getattr(request, 'subscriber', None)

    # When celery is turned on, use delay to call update_user and
    # don't catch any exceptions. For now, return an error if
    # something goes wrong.
    try:
        update_user(data or request.POST.copy(),
                    user and user.email,
                    type,
                    optin)
        return json_response({'status': 'ok'})
    except (NewsletterException, UnauthorizedException), e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)


def get_user(token=None, email=None):
    newsletters = newsletter_fields()

    fields = [
        'EMAIL_ADDRESS_',
        'EMAIL_FORMAT_',
        'COUNTRY_',
        'LANGUAGE_ISO2',
        'TOKEN',
        'CREATED_DATE_',
    ]

    for nl in newsletters:
        fields.append('%s_FLG' % nl)

    try:
        ext = ExactTargetDataExt(settings.EXACTTARGET_USER,
                                 settings.EXACTTARGET_PASS)
        user = ext.get_record(settings.EXACTTARGET_DATA,
                              email or token,
                              fields,
                              'EMAIL_ADDRESS_' if email else 'TOKEN')
    except NewsletterException, e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)
    except UnauthorizedException, e:
        return json_response({'status': 'error',
                              'desc': 'Email service provider auth failure'},
                             status=500)

    user_data = {
        'email': user['EMAIL_ADDRESS_'],
        'format': user['EMAIL_FORMAT_'],
        'country': user['COUNTRY_'],
        'lang': user['LANGUAGE_ISO2'],
        'token': user['TOKEN'],
        'created-date': user['CREATED_DATE_'],
        'newsletters': [newsletter_name(nl) for nl in newsletters
                        if user.get('%s_FLG' % nl, False) == 'Y'],
    }

    return json_response(user_data)

## Views


@require_POST
@logged_in
@csrf_exempt
def confirm(request, token):
    # Until celery is turned on, return a message immediately
    try:
        confirm_user(request.subscriber.token)
        return json_response({'status': 'ok'})
    except (NewsletterException, UnauthorizedException), e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)


@require_POST
@csrf_exempt
def subscribe(request):
    if 'newsletters' not in request.POST:
        return json_response({'status': 'error',
                              'desc': 'newsletters is missing'},
                             status=500)

    optin = request.POST.get('optin', 'Y') == 'Y'
    return update_user_task(request, SUBSCRIBE, optin=optin)


@require_POST
@csrf_exempt
def subscribe_sms(request):
    if 'mobile_number' not in request.POST:
        return json_response({'status': 'error',
                              'desc': 'mobile_number is missing'},
                             status=400)

    msg_name = request.POST.get('msg_name', 'SMS_Android')
    mobile = request.POST['mobile_number']
    mobile = re.sub(r'\D+', '', mobile)
    if len(mobile) == 10:
        mobile = '1' + mobile
    elif len(mobile) != 11 or mobile[0] != '1':
        return json_response({'status': 'error',
                              'desc': 'mobile_number must be a US number'},
                             status=400)

    optin = request.POST.get('optin', 'N') == 'Y'

    # When celery is turned on, use delay to call update_user and
    # don't catch any exceptions. For now, return an error if
    # something goes wrong.
    try:
        add_sms_user(msg_name, mobile, optin)
        return json_response({'status': 'ok'})
    except (NewsletterException, UnauthorizedException), e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)


@require_POST
@logged_in
@csrf_exempt
def unsubscribe(request, token):
    data = request.POST.copy()

    if data.get('optout', 'N') == 'Y':
        data['newsletters'] = ','.join(newsletter_names())

    return update_user_task(request, UNSUBSCRIBE, data)


@require_POST
@logged_in
@csrf_exempt
def user(request, token):
    return get_user(request.subscriber.token)


def debug_user(request):
    if not 'email' in request.GET or not 'supertoken' in request.GET:
        return json_response({
            'status': 'error',
            'desc': 'Using debug_user, you need to pass the '
                    '`email` and `supertoken` GET parameters',
        }, status=500)

    if request.GET['supertoken'] != settings.SUPERTOKEN:
        return json_response({'status': 'error',<commit_msg>Gogo emergency late night fix!<commit_after>from functools import wraps
import json

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.conf import settings
import re

from tasks import (add_sms_user, update_user, confirm_user,
                   update_student_reps, SUBSCRIBE, UNSUBSCRIBE)
from newsletters import *
from models import Subscriber
from backends.exacttarget import (ExactTargetDataExt, NewsletterException,
                                  UnauthorizedException)


## Utility functions

def logged_in(f):
    """Decorator to check if the user has permission to view these
    pages"""

    @wraps(f)
    def wrapper(request, token, *args, **kwargs):
        subscriber = Subscriber.objects.filter(token=token)
        if not subscriber.exists():
            return json_response({
                'status': 'error',
                'desc': 'Must have valid token for this request',
            }, status=403)

        request.subscriber = subscriber[0]
        return f(request, token, *args, **kwargs)
    return wrapper


def json_response(data, status=200):
    res = HttpResponse(json.dumps(data),
                       mimetype='application/json')
    res.status_code = status
    return res


def update_user_task(request, type, data=None, optin=True):
    """Call the update_user task async with the right parameters"""

    user = getattr(request, 'subscriber', None)

    # When celery is turned on, use delay to call update_user and
    # don't catch any exceptions. For now, return an error if
    # something goes wrong.
    try:
        update_user(data or request.POST.copy(),
                    user and user.email,
                    type,
                    optin)
        return json_response({'status': 'ok'})
    except (NewsletterException, UnauthorizedException), e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)


def get_user(token=None, email=None):
    newsletters = newsletter_fields()

    fields = [
        'EMAIL_ADDRESS_',
        'EMAIL_FORMAT_',
        'COUNTRY_',
        'LANGUAGE_ISO2',
        'TOKEN',
        'CREATED_DATE_',
    ]

    for nl in newsletters:
        fields.append('%s_FLG' % nl)

    try:
        ext = ExactTargetDataExt(settings.EXACTTARGET_USER,
                                 settings.EXACTTARGET_PASS)
        user = ext.get_record(settings.EXACTTARGET_DATA,
                              email or token,
                              fields,
                              'EMAIL_ADDRESS_' if email else 'TOKEN')
    except NewsletterException, e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)
    except UnauthorizedException, e:
        return json_response({'status': 'error',
                              'desc': 'Email service provider auth failure'},
                             status=500)

    user_data = {
        'email': user['EMAIL_ADDRESS_'],
        'format': user['EMAIL_FORMAT_'],
        'country': user['COUNTRY_'],
        'lang': user['LANGUAGE_ISO2'],
        'token': user['TOKEN'],
        'created-date': user['CREATED_DATE_'],
        'newsletters': [newsletter_name(nl) for nl in newsletters
                        if user.get('%s_FLG' % nl, False) == 'Y'],
    }

    return json_response(user_data)

## Views


@require_POST
@logged_in
@csrf_exempt
def confirm(request, token):
    # Until celery is turned on, return a message immediately
    try:
        confirm_user(request.subscriber.token)
        return json_response({'status': 'ok'})
    except (NewsletterException, UnauthorizedException), e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)


@require_POST
@csrf_exempt
def subscribe(request):
    if 'newsletters' not in request.POST:
        return json_response({'status': 'error',
                              'desc': 'newsletters is missing'},
                             status=500)

    optin = request.POST.get('optin', 'Y') == 'Y'
    return update_user_task(request, SUBSCRIBE, optin=optin)


@require_POST
@csrf_exempt
def subscribe_sms(request):
    if 'mobile_number' not in request.POST:
        return json_response({'status': 'error',
                              'desc': 'mobile_number is missing'},
                             status=400)

    msg_name = request.POST.get('msg_name', 'SMS_Android')
    mobile = request.POST['mobile_number']
    mobile = re.sub(r'\D+', '', mobile)
    if len(mobile) == 10:
        mobile = '1' + mobile
    elif len(mobile) != 11 or mobile[0] != '1':
        return json_response({'status': 'error',
                              'desc': 'mobile_number must be a US number'},
                             status=400)

    optin = request.POST.get('optin', 'N') == 'Y'

    # When celery is turned on, use delay to call update_user and
    # don't catch any exceptions. For now, return an error if
    # something goes wrong.
    try:
        add_sms_user(msg_name, mobile, optin)
        return json_response({'status': 'ok'})
    except (NewsletterException, UnauthorizedException), e:
        return json_response({'status': 'error',
                              'desc': e.message},
                             status=500)


@require_POST
@logged_in
@csrf_exempt
def unsubscribe(request, token):
    data = request.POST.copy()

    if data.get('optout', 'N') == 'Y':
        data['newsletters'] = ','.join(newsletter_names())

    return update_user_task(request, UNSUBSCRIBE, data)


@logged_in
@csrf_exempt
def user(request, token):
    if request.method == 'POST':
        return update_user_task(request, SET)

    return get_user(request.subscriber.token)


def debug_user(request):
    if not 'email' in request.GET or not 'supertoken' in request.GET:
        return json_response({
            'status': 'error',
            'desc': 'Using debug_user, you need to pass the '
                    '`email` and `supertoken` GET parameters',
        }, status=500)

    if request.GET['supertoken'] != settings.SUPERTOKEN:
        return json_response({'status': 'error',
{'fraction_non_alphanumeric': 0.10963139120095125, 'fraction_numerical': 0.006817281014665081, 'mean_word_length': 2.1866632988128316, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3664041', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3536, 'n_words': 930}