<commit_before># -*- coding: utf-8  -*-
from dal import autocomplete
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db.models import Count
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.views.decorators import csrf
from .forms import NominationForm
from .models import SACYear, Position, Nomination, \
                    NominationAnnouncement, VoteNomination, \
                    city_choices
from . import decorators, utils
import accounts.utils
import re

def show_index(request):
    if request.user.is_authenticated():
        return render(request, 'accounts/home.html')
    else:
        return render(request, 'accounts/home_unauthenticated.html')

@login_required
def list_positions(request, entity):
    current_year = SACYear.objects.get_current()
    if not current_year.is_nomination_open():
        return render(request, "voting/closed.html")

    user_nominations = Nomination.objects.filter(position__entity=entity,
                                                 position__year=current_year,
                                                 user=request.user)
    positions = Position.objects.filter(entity=entity, year=current_year)
    if not request.user.is_superuser:
        positions = positions.filter(colleges_allowed_to_nominate=request.user.profile.college)

    context = {'user_nominations': user_nominations,
               'positions': positions}
    return render(request, 'voting/list_' + entity + '_positions.html', context)

@login_required
def add_nominee(request, position_id):
    current_year = SACYear.objects.get_current()
    if not current_year.is_nomination_open():
        return HttpResponseRedirect(reverse("voting:closed"))

    position = get_object_or_404(Position, pk=position_id)
    context = {'position': position}

    user_nominations = Nomination.objects.filter(position__entity=position.entity,
                                                 position__year=current_year,
                                                 user=request.user)
    if user_nominations.exists():
        context['already_on'] = True
    else:
        if request.user.is_superuser:
            positions = Position.objects.filter(year=current_year)
        else:
            positions = Position.objects.filter(colleges_allowed_to_nominate=request.user.profile.college,
                                                year=current_year)
            if position not in positions:
                raise PermissionDenied
        if request.method == 'POST':
            instance = Nomination(user=request.user, position=position)
            form = NominationForm(request.POST, request.FILES, instance=instance)
            if form.is_valid():
                instance = form.save()
                return HttpResponseRedirect(reverse("voting:nomination_thanks", args=(position.pk,)))
        elif request.method == 'GET':
            form = NominationForm()
        context['form'] = form

    return render(request,'voting/add_nominee.html', context)

@login_required
def show_nomination(request, position_id, nomination_id):
    nomination = get_object_or_404(Nomination, position__pk=position_id,
                                   pk=nomination_id)
    if not request.user.is_superuser and \
       not nomination.user == request.user:
        raise PermissionDenied
    context = {'nomination': nomination}
    return render(request,'voting/show_nomination.html', context)

def announce_nominees(request, entity):
    current_year = SACYear.objects.get_current()
    context = {}
    if current_year.is_announcement_due():
        per_city = []
        for city_code, city_name in city_choices:
            positions = Position.objects\
                               .filter(entity=entity, city=city_code, year=current_year)\
                               .annotate(nomination_count=Count('nominationannouncement'))\
                               .filter(nomination_count__gt=1)
            if positions.exists():
                per_city.append((city_name, positions))
        context = {'entity': entity, 'per_city': per_city}

    return render(request, 'voting/announce_nominees.html', context)

@login_required
def show_voting_index(request):
    current_year = SACYear.objects.get_current()
    if not request.user.is_superuser and not current_year.is_voting_open():
        return render(request, "voting/voting_closed.html")
    else:
        position_pool = Position.objects.filter(year=current_year)\
                                        .annotate(announced_count=Count('nominationannouncement'))\
                                        .filter(announced_count__gte=2)\
                                        .exclude(votenomination__user=request.user)\
                                        .order_by('entity')
        if request.user.profile:
            position_pool = position_pool.filter(colleges_allowed_to_vote=request.user.profile.college)

        if position_pool.exists():
            completed_voting = False
            votes = VoteNomination.objects.none()
        else:
            completed_voting = True
            votes = VoteNomination.objects.filter(user=request.user)
        qrcode_value = utils.get_ticket(request.user)
        context = {'qrcode_value': qrcode_value,
                   'completed_voting': completed_voting,
                   'votes': votes}
        return render(request,'voting/show_voting.html', context)

@login_required
@decorators.ajax_only
@decorators.post_only
@csrf.csrf_exempt
def handle_vote(request):
    current_year = SACYear.objects.get_current()
    if not request.user.is_superuser and not current_year.is_voting_open():
        raise Exception("التصويت غير مُتاح حاليًا!")

    nomination_vote_pk = request.POST.get('nomination_vote_pk', None)
    if nomination_vote_pk:<commit_msg>add year filter to ticket<commit_after># -*- coding: utf-8  -*-
from dal import autocomplete
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db.models import Count
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.views.decorators import csrf
from .forms import NominationForm
from .models import SACYear, Position, Nomination, \
                    NominationAnnouncement, VoteNomination, \
                    city_choices
from . import decorators, utils
import accounts.utils
import re

def show_index(request):
    if request.user.is_authenticated():
        return render(request, 'accounts/home.html')
    else:
        return render(request, 'accounts/home_unauthenticated.html')

@login_required
def list_positions(request, entity):
    current_year = SACYear.objects.get_current()
    if not current_year.is_nomination_open():
        return render(request, "voting/closed.html")

    user_nominations = Nomination.objects.filter(position__entity=entity,
                                                 position__year=current_year,
                                                 user=request.user)
    positions = Position.objects.filter(entity=entity, year=current_year)
    if not request.user.is_superuser:
        positions = positions.filter(colleges_allowed_to_nominate=request.user.profile.college)

    context = {'user_nominations': user_nominations,
               'positions': positions}
    return render(request, 'voting/list_' + entity + '_positions.html', context)

@login_required
def add_nominee(request, position_id):
    current_year = SACYear.objects.get_current()
    if not current_year.is_nomination_open():
        return HttpResponseRedirect(reverse("voting:closed"))

    position = get_object_or_404(Position, pk=position_id)
    context = {'position': position}

    user_nominations = Nomination.objects.filter(position__entity=position.entity,
                                                 position__year=current_year,
                                                 user=request.user)
    if user_nominations.exists():
        context['already_on'] = True
    else:
        if request.user.is_superuser:
            positions = Position.objects.filter(year=current_year)
        else:
            positions = Position.objects.filter(colleges_allowed_to_nominate=request.user.profile.college,
                                                year=current_year)
            if position not in positions:
                raise PermissionDenied
        if request.method == 'POST':
            instance = Nomination(user=request.user, position=position)
            form = NominationForm(request.POST, request.FILES, instance=instance)
            if form.is_valid():
                instance = form.save()
                return HttpResponseRedirect(reverse("voting:nomination_thanks", args=(position.pk,)))
        elif request.method == 'GET':
            form = NominationForm()
        context['form'] = form

    return render(request,'voting/add_nominee.html', context)

@login_required
def show_nomination(request, position_id, nomination_id):
    nomination = get_object_or_404(Nomination, position__pk=position_id,
                                   pk=nomination_id)
    if not request.user.is_superuser and \
       not nomination.user == request.user:
        raise PermissionDenied
    context = {'nomination': nomination}
    return render(request,'voting/show_nomination.html', context)

def announce_nominees(request, entity):
    current_year = SACYear.objects.get_current()
    context = {}
    if current_year.is_announcement_due():
        per_city = []
        for city_code, city_name in city_choices:
            positions = Position.objects\
                               .filter(entity=entity, city=city_code, year=current_year)\
                               .annotate(nomination_count=Count('nominationannouncement'))\
                               .filter(nomination_count__gt=1)
            if positions.exists():
                per_city.append((city_name, positions))
        context = {'entity': entity, 'per_city': per_city}

    return render(request, 'voting/announce_nominees.html', context)

@login_required
def show_voting_index(request):
    current_year = SACYear.objects.get_current()
    if not request.user.is_superuser and not current_year.is_voting_open():
        return render(request, "voting/voting_closed.html")
    else:
        position_pool = Position.objects.filter(year=current_year)\
                                        .annotate(announced_count=Count('nominationannouncement'))\
                                        .filter(announced_count__gte=2)\
                                        .exclude(votenomination__user=request.user)\
                                        .order_by('entity')
        if request.user.profile:
            position_pool = position_pool.filter(colleges_allowed_to_vote=request.user.profile.college)

        if position_pool.exists():
            completed_voting = False
            votes = VoteNomination.objects.none()
        else:
            completed_voting = True
            votes = VoteNomination.objects.filter(user=request.user,position__year=current_year)
        qrcode_value = utils.get_ticket(request.user)
        context = {'qrcode_value': qrcode_value,
                   'completed_voting': completed_voting,
                   'votes': votes}
        return render(request,'voting/show_voting.html', context)

@login_required
@decorators.ajax_only
@decorators.post_only
@csrf.csrf_exempt
def handle_vote(request):
    current_year = SACYear.objects.get_current()
    if not request.user.is_superuser and not current_year.is_voting_open():
        raise Exception("التصويت غير مُتاح حاليًا!")

    nomination_vote_pk = request.POST.get('nomination_vote_pk', None)
    if nomination_vote_pk:
{'fraction_non_alphanumeric': 0.08852621167161226, 'fraction_numerical': 0.002472799208704253, 'mean_word_length': 2.571680894907271, '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': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2830850', 'n_tokens_mistral': 3325, 'n_tokens_neox': 3224, 'n_words': 615}