<commit_before>from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.shortcuts import render, get_object_or_404
from django.utils.translation import ugettext as _

from .utils import nopermission, get_or_404, is_involved

from ..models import Event, BadgeDesign, BadgePermission, BadgeRole
from ..forms import BadgeSettingsForm, BadgeDesignForm, BadgePermissionForm, \
    BadgeRoleForm, BadgeDefaultsForm, BadgeJobDefaultsForm, RegisterBadgeForm
from ..badges import BadgeCreator, BadgeCreatorError, warnings_for_job


def notactive(request):
    return render(request, 'registration/admin/badges_not_active.html')


@login_required
def badges(request, event_url_name):
    event = get_object_or_404(Event, url_name=event_url_name)

    # check permission
    if not event.is_admin(request.user):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # check if all necessary settings are done
    possible = event.badge_settings.creation_possible()

    # number for warnings for each job
    jobs = event.job_set.all()
    for job in jobs:
        job.num_warnings = len(warnings_for_job(job))

    context = {'event': event,
               'jobs': jobs,
               'possible': possible}
    return render(request, 'registration/admin/badges.html', context)


@login_required
def badges_warnings(request, event_url_name, job_pk):
    event, job, shift, helper = get_or_404(event_url_name, job_pk)

    # check permission
    if not event.is_admin(request.user):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # TODO: check if possible

    helpers = warnings_for_job(job)

    # render
    context = {'event': event,
               'helpers': helpers}
    return render(request, 'registration/admin/badges_warnings.html', context)


@login_required
def generate_badges(request, event_url_name, job_pk, generate_all=False):
    event, job, shift, helper = get_or_404(event_url_name, job_pk)

    # check permission
    if not event.is_admin(request.user):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # TODO: check if possible, show error page

    # badge creation
    creator = BadgeCreator(event.badge_settings)

    # skip already printed badges?
    skip_printed = event.badge_settings.barcodes and not generate_all

    # add helpers and coordinators
    for h in job.helpers_and_coordinators():
        # skip if badge was printed already (and this behaviour is requested)
        if skip_printed and h.badge.printed:
            continue

        helpers_job = h.badge.get_job()
        # print badge only if this is the primary job or the job is unambiguous
        if (not helpers_job or helpers_job == job):
            creator.add_helper(h)

    # generate
    try:
        pdf_filename = creator.generate()
    except BadgeCreatorError as e:
        # remove temp files
        creator.finish()

        # return error message
        context = {'event': event,
                   'error': e.value,
                   'latex_output': e.get_latex_output()}
        return render(request, 'registration/admin/badges_failed.html',
                      context)

    # output
    filename = "%s.pdf" % job.name

    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename

    # send file
    with open(pdf_filename, 'rb') as f:
        response.write(f.read())

    # finish badge generation (delete files)
    creator.finish()

    return response


@login_required
def configure_badges(request, event_url_name):
    event = get_object_or_404(Event, url_name=event_url_name)

    # check permission
    if not is_involved(request.user, event_url_name, admin_required=True):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # roles
    roles = event.badge_settings.badgerole_set.all()

    # designs
    designs = event.badge_settings.badgedesign_set.all()<commit_msg>badges: no invalid characters in filename<commit_after>from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.shortcuts import render, get_object_or_404
from django.utils.translation import ugettext as _

from .utils import nopermission, get_or_404, is_involved

from ..models import Event, BadgeDesign, BadgePermission, BadgeRole
from ..forms import BadgeSettingsForm, BadgeDesignForm, BadgePermissionForm, \
    BadgeRoleForm, BadgeDefaultsForm, BadgeJobDefaultsForm, RegisterBadgeForm
from ..badges import BadgeCreator, BadgeCreatorError, warnings_for_job
from ..utils import escape_filename


def notactive(request):
    return render(request, 'registration/admin/badges_not_active.html')


@login_required
def badges(request, event_url_name):
    event = get_object_or_404(Event, url_name=event_url_name)

    # check permission
    if not event.is_admin(request.user):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # check if all necessary settings are done
    possible = event.badge_settings.creation_possible()

    # number for warnings for each job
    jobs = event.job_set.all()
    for job in jobs:
        job.num_warnings = len(warnings_for_job(job))

    context = {'event': event,
               'jobs': jobs,
               'possible': possible}
    return render(request, 'registration/admin/badges.html', context)


@login_required
def badges_warnings(request, event_url_name, job_pk):
    event, job, shift, helper = get_or_404(event_url_name, job_pk)

    # check permission
    if not event.is_admin(request.user):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # TODO: check if possible

    helpers = warnings_for_job(job)

    # render
    context = {'event': event,
               'helpers': helpers}
    return render(request, 'registration/admin/badges_warnings.html', context)


@login_required
def generate_badges(request, event_url_name, job_pk, generate_all=False):
    event, job, shift, helper = get_or_404(event_url_name, job_pk)

    # check permission
    if not event.is_admin(request.user):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # TODO: check if possible, show error page

    # badge creation
    creator = BadgeCreator(event.badge_settings)

    # skip already printed badges?
    skip_printed = event.badge_settings.barcodes and not generate_all

    # add helpers and coordinators
    for h in job.helpers_and_coordinators():
        # skip if badge was printed already (and this behaviour is requested)
        if skip_printed and h.badge.printed:
            continue

        helpers_job = h.badge.get_job()
        # print badge only if this is the primary job or the job is unambiguous
        if (not helpers_job or helpers_job == job):
            creator.add_helper(h)

    # generate
    try:
        pdf_filename = creator.generate()
    except BadgeCreatorError as e:
        # remove temp files
        creator.finish()

        # return error message
        context = {'event': event,
                   'error': e.value,
                   'latex_output': e.get_latex_output()}
        return render(request, 'registration/admin/badges_failed.html',
                      context)

    # output
    filename = escape_filename("%s.pdf" % job.name)

    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename

    # send file
    with open(pdf_filename, 'rb') as f:
        response.write(f.read())

    # finish badge generation (delete files)
    creator.finish()

    return response


@login_required
def configure_badges(request, event_url_name):
    event = get_object_or_404(Event, url_name=event_url_name)

    # check permission
    if not is_involved(request.user, event_url_name, admin_required=True):
        return nopermission(request)

    # check if badge system is active
    if not event.badges:
        return notactive(request)

    # roles
    roles = event.badge_settings.badgerole_set.all()

    # designs
    designs = event.badge_settings.badgedesign_set.all()
{'fraction_non_alphanumeric': 0.08087986463620982, 'fraction_numerical': 0.0047377326565143825, 'mean_word_length': 3.2584053794428436, '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': '2732383', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2555, 'n_words': 704}