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

from collections import OrderedDict
from datetime import datetime, time, timedelta

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.forms.models import modelformset_factory
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from django.utils.translation import ugettext as _
from guardian.decorators import permission_required

from apps.dashboard.tools import get_base_context, has_access
from apps.events.dashboard.forms import (ChangeAttendanceEventForm, ChangeEventForm,
                                         ChangeReservationForm)
from apps.events.dashboard.utils import event_ajax_handler
from apps.events.models import AttendanceEvent, Attendee, Event, Reservation, Reservee
from apps.events.utils import get_group_restricted_events, get_types_allowed
from apps.payment.models import PaymentRelation


@login_required
@permission_required('events.view_event', return_403=True)
def index(request):
    if not has_access(request):
        raise PermissionDenied

    allowed_events = get_group_restricted_events(request.user, True)
    events = allowed_events.filter(event_start__gte=timezone.now().date()).order_by('event_start')

    context = get_base_context(request)
    context['events'] = events

    return render(request, 'events/dashboard/index.html', context)


@login_required
@permission_required('events.view_event', return_403=True)
def past(request):
    if not has_access(request):
        raise PermissionDenied

    allowed_events = get_group_restricted_events(request.user, True)
    events = allowed_events.filter(event_start__lt=timezone.now().date()).order_by('-event_start')

    context = get_base_context(request)
    context['events'] = events

    return render(request, 'events/dashboard/index.html', context)


@login_required
@permission_required('events.view_event', return_403=True)
def create_event(request):
    if not has_access(request):
        raise PermissionDenied

    context = get_base_context(request)

    if request.method == 'POST':
        form = ChangeEventForm(request.POST)
        if form.is_valid():
            cleaned = form.cleaned_data

            if cleaned['event_type'] not in get_types_allowed(request.user):
                messages.error(request, _(
                    "Du har ikke tilgang til å lage arranngement av typen '%s'.") % cleaned['event_type'])
                context['change_event_form'] = form

            else:
                # Create object, but do not commit to db. We need to add stuff.
                event = form.save(commit=False)
                # Add author
                event.author = request.user
                event.save()

                messages.success(request, _("Arrangementet ble opprettet."))
                return redirect('dashboard_event_details', event_id=event.id)

        else:
            context['change_event_form'] = form

    if 'change_event_form' not in context.keys():
        context['change_event_form'] = ChangeEventForm()

    context['event'] = _('Nytt arrangement')
    context['active_tab'] = 'details'

    return render(request, 'events/dashboard/details.html', context)


def _create_details_context(request, event_id):
    """
    Prepare a context to be shared for all detail views.
    """

    event = get_object_or_404(Event, pk=event_id)

    # Start with adding base context and the event itself
    context = get_base_context(request)
    context['event'] = event

    # Add forms
    context['change_event_form'] = ChangeEventForm(instance=event)
    if event.is_attendance_event():
        context['change_attendance_form'] = ChangeAttendanceEventForm(instance=event.attendance_event)
        if event.attendance_event.has_reservation:
            context['change_reservation_form'] = ChangeReservationForm(instance=event.attendance_event.reserved_seats)
            seats = event.attendance_event.reserved_seats.seats
            ReserveeFormSet = modelformset_factory(
                Reservee, max_num=seats, extra=seats, fields=['name', 'note', 'allergies'])
            context['change_reservees_formset'] = ReserveeFormSet(
                queryset=event.attendance_event.reserved_seats.reservees.all())
        if event.attendance_event.payment() and len(event.attendance_event.payment().prices()) > 1:
            payments = {}

            for attendee in event.attendance_event.attendees_qs:
                paymentRelation = PaymentRelation.objects.filter(
                    payment=event.attendance_event.payment(),
                    user=attendee.user,
                    refunded=False
                )

                if paymentRelation:
                    payments[attendee] = paymentRelation[0].payment_price

            context['payment_prices'] = payments

    return context


def _payment_prices(attendance_event):
    payments = {}
    summary = OrderedDict()<commit_msg>cleanup<commit_after># -*- coding: utf-8 -*-

from collections import OrderedDict
from datetime import datetime, time, timedelta

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.forms.models import modelformset_factory
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from django.utils.translation import ugettext as _
from guardian.decorators import permission_required

from apps.dashboard.tools import get_base_context, has_access
from apps.events.dashboard.forms import (ChangeAttendanceEventForm, ChangeEventForm,
                                         ChangeReservationForm)
from apps.events.dashboard.utils import event_ajax_handler
from apps.events.models import AttendanceEvent, Attendee, Event, Reservation, Reservee
from apps.events.utils import get_group_restricted_events, get_types_allowed
from apps.payment.models import PaymentRelation


@login_required
@permission_required('events.view_event', return_403=True)
def index(request):
    if not has_access(request):
        raise PermissionDenied

    allowed_events = get_group_restricted_events(request.user, True)
    events = allowed_events.filter(event_start__gte=timezone.now().date()).order_by('event_start')

    context = get_base_context(request)
    context['events'] = events

    return render(request, 'events/dashboard/index.html', context)


@login_required
@permission_required('events.view_event', return_403=True)
def past(request):
    if not has_access(request):
        raise PermissionDenied

    allowed_events = get_group_restricted_events(request.user, True)
    events = allowed_events.filter(event_start__lt=timezone.now().date()).order_by('-event_start')

    context = get_base_context(request)
    context['events'] = events

    return render(request, 'events/dashboard/index.html', context)


@login_required
@permission_required('events.view_event', return_403=True)
def create_event(request):
    if not has_access(request):
        raise PermissionDenied

    context = get_base_context(request)

    if request.method == 'POST':
        form = ChangeEventForm(request.POST)
        if form.is_valid():
            cleaned = form.cleaned_data

            if cleaned['event_type'] not in get_types_allowed(request.user):
                messages.error(request, _(
                    "Du har ikke tilgang til å lage arranngement av typen '%s'.") % cleaned['event_type'])
                context['change_event_form'] = form

            else:
                # Create object, but do not commit to db. We need to add stuff.
                event = form.save(commit=False)
                # Add author
                event.author = request.user
                event.save()

                messages.success(request, _("Arrangementet ble opprettet."))
                return redirect('dashboard_event_details', event_id=event.id)

        else:
            context['change_event_form'] = form

    if 'change_event_form' not in context.keys():
        context['change_event_form'] = ChangeEventForm()

    context['event'] = _('Nytt arrangement')
    context['active_tab'] = 'details'

    return render(request, 'events/dashboard/details.html', context)


def _create_details_context(request, event_id):
    """
    Prepare a context to be shared for all detail views.
    """

    event = get_object_or_404(Event, pk=event_id)

    # Start with adding base context and the event itself
    context = get_base_context(request)
    context['event'] = event

    # Add forms
    context['change_event_form'] = ChangeEventForm(instance=event)
    if event.is_attendance_event():
        context['change_attendance_form'] = ChangeAttendanceEventForm(instance=event.attendance_event)
        if event.attendance_event.has_reservation:
            context['change_reservation_form'] = ChangeReservationForm(instance=event.attendance_event.reserved_seats)
            seats = event.attendance_event.reserved_seats.seats
            ReserveeFormSet = modelformset_factory(
                Reservee, max_num=seats, extra=seats, fields=['name', 'note', 'allergies'])
            context['change_reservees_formset'] = ReserveeFormSet(
                queryset=event.attendance_event.reserved_seats.reservees.all())

    return context


def _payment_prices(attendance_event):
    payments = {}
    summary = OrderedDict()
{'fraction_non_alphanumeric': 0.08996467899439019, 'fraction_numerical': 0.0035321005609806775, 'mean_word_length': 3.450762829403606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '495322', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2621, 'n_words': 580}