<commit_before>from datetime import date, datetime, timedelta
import json
from time import strptime

from django.contrib import messages
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import DataError, IntegrityError
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
import xlrd

from models import UserProfile, Event, legend_hr, PreviousTransfers


__author__ = 'tmehta'


def login_user(request):
    if request.method == "POST":
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = authenticate(username=username, password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                login(request, user)
                return redirect(reverse('dashboard:main'))
            else:
                pass
        else:
            messages.error(request, 'Incorrect Username/Password')
            return redirect(reverse('dashboard:login'))
    else:
        if request.user.is_authenticated():
            return redirect(reverse('dashboard:main'))
        return render(request, 'dashboard/login.html')


def sign_up(request):
    if request.method == "POST":
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        first_name = request.POST.get('first_name', '')
        last_name = request.POST.get('last_name', '')
        organization = request.POST.get('organization', 'UHN')
        qualification = request.POST.get('qualification', 'RN')
        if username == '' or password == '' or first_name == '' or last_name == '':
            messages.error(request, 'Please fill out all the credentials correctly')
            return redirect(reverse('dashboard:signup'))
        try:
            user = User.objects.create(username=username, email=username, first_name=first_name, last_name=last_name)
            UserProfile.objects.create(user=user, organization=organization, qualification=qualification)
        except (DataError, IntegrityError):
            messages.error(request, 'Please fill out all the credentials correctly')
            return redirect(reverse('dashboard:signup'))
        user.set_password(password)
        user.save()
        user = authenticate(username=username, password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                login(request, user)
                return redirect(reverse('dashboard:main'))
            else:
                pass
        else:
            return redirect(reverse('dashboard:signup'))
    else:
        if request.user.is_authenticated():
            return redirect(reverse('dashboard:main'))
        return render(request, 'dashboard/sign_up.html')


@login_required()
def main(request):
    context = {}
    if request.user.userprofile.is_admin:
        context['employees'] = UserProfile.objects.filter(
            organization=request.user.userprofile.organization, role='employee').values_list('user__id',
                                                                                             'user__first_name',
                                                                                             'user__last_name')
    context['now'] = datetime.today().month
    return render(request, 'dashboard/index.html', context)


@login_required()
def organization_shifts(request):
    context = {'organization': True}
    return render(request, 'dashboard/index.html', context)


@login_required()
def swap_page(request):
    context = {}
    events = Event.objects.filter(belongs_to__userprofile__organization=request.user.userprofile.organization)
    if request.user.userprofile.is_admin:
        potential_agreed_swaps = list(events.filter(~Q(agreed_swap=None)).filter(requested_swap=True))
        agreed_swaps = set()
        for s in potential_agreed_swaps:
            if s.agreed_swap.agreed_swap == s and s not in agreed_swaps and s.agreed_swap not in agreed_swaps:
                agreed_swaps.add(s.id)
        context['agreed_swaps'] = Event.objects.filter(pk__in=list(agreed_swaps))
        context['agreed_transfers'] = events.filter(requested_transfer=True).filter(~Q(to_change_user=None))
        return render(request, 'dashboard/swap_admin.html', context)
    else:
        my_shifts = events.filter(belongs_to=request.user)
        context['my_shifts'] = my_shifts
        context['my_swaps'] = my_shifts.filter(requested_swap=True)
        context['my_transfers'] = my_shifts.filter(requested_transfer=True)
        context['other_swaps'] = events.filter(~Q(belongs_to=request.user)).filter(requested_swap=True)
        context['other_transfers'] = events.filter(~Q(belongs_to=request.user)).filter(requested_transfer=True)
        return render(request, 'dashboard/swap.html', context)


@login_required()
def previous_swap_page(request):
    context = {}
    if request.user.userprofile.is_admin:
        context['previous_swaps'] = PreviousTransfers.objects.filter(
            event__belongs_to__userprofile__organization=request.user.userprofile.organization)
        return render(request, 'dashboard/previous_swap_admin.html', context)


@login_required()
def get_shifts(request):
    type_shift = request.GET['type']<commit_msg>add performacne<commit_after>from datetime import date, datetime, timedelta
import json
from time import strptime

from django.contrib import messages
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import DataError, IntegrityError
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
import xlrd

from models import UserProfile, Event, legend_hr, PreviousTransfers


__author__ = 'tmehta'


def login_user(request):
    if request.method == "POST":
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = authenticate(username=username, password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                login(request, user)
                return redirect(reverse('dashboard:main'))
            else:
                pass
        else:
            messages.error(request, 'Incorrect Username/Password')
            return redirect(reverse('dashboard:login'))
    else:
        if request.user.is_authenticated():
            return redirect(reverse('dashboard:main'))
        return render(request, 'dashboard/login.html')


def sign_up(request):
    if request.method == "POST":
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        first_name = request.POST.get('first_name', '')
        last_name = request.POST.get('last_name', '')
        organization = request.POST.get('organization', 'UHN')
        qualification = request.POST.get('qualification', 'RN')
        if username == '' or password == '' or first_name == '' or last_name == '':
            messages.error(request, 'Please fill out all the credentials correctly')
            return redirect(reverse('dashboard:signup'))
        try:
            user = User.objects.create(username=username, email=username, first_name=first_name, last_name=last_name)
            UserProfile.objects.create(user=user, organization=organization, qualification=qualification)
        except (DataError, IntegrityError):
            messages.error(request, 'Please fill out all the credentials correctly')
            return redirect(reverse('dashboard:signup'))
        user.set_password(password)
        user.save()
        user = authenticate(username=username, password=password)
        if user is not None:
            # the password verified for the user
            if user.is_active:
                login(request, user)
                return redirect(reverse('dashboard:main'))
            else:
                pass
        else:
            return redirect(reverse('dashboard:signup'))
    else:
        if request.user.is_authenticated():
            return redirect(reverse('dashboard:main'))
        return render(request, 'dashboard/sign_up.html')


@login_required()
def main(request):
    context = {}
    if request.user.userprofile.is_admin:
        context['employees'] = UserProfile.objects.filter(
            organization=request.user.userprofile.organization, role='employee').values_list('user__id',
                                                                                             'user__first_name',
                                                                                             'user__last_name')
    context['now'] = datetime.today().month
    return render(request, 'dashboard/index.html', context)


@login_required()
def organization_shifts(request):
    context = {'organization': True}
    return render(request, 'dashboard/index.html', context)


@login_required()
def swap_page(request):
    context = {}
    events = Event.objects.filter(belongs_to__userprofile__organization=request.user.userprofile.organization)
    if request.user.userprofile.is_admin:
        potential_agreed_swaps = list(events.filter(~Q(agreed_swap=None)).filter(requested_swap=True))
        agreed_swaps = set()
        for s in potential_agreed_swaps:
            if s.id not in agreed_swaps and s.agreed_swap.id not in agreed_swaps:
                agreed_swaps.add(s.id)
        context['agreed_swaps'] = Event.objects.filter(pk__in=list(agreed_swaps))
        context['agreed_transfers'] = events.filter(requested_transfer=True).filter(~Q(to_change_user=None))
        return render(request, 'dashboard/swap_admin.html', context)
    else:
        my_shifts = events.filter(belongs_to=request.user)
        context['my_shifts'] = my_shifts
        context['my_swaps'] = my_shifts.filter(requested_swap=True)
        context['my_transfers'] = my_shifts.filter(requested_transfer=True)
        context['other_swaps'] = events.filter(~Q(belongs_to=request.user)).filter(requested_swap=True)
        context['other_transfers'] = events.filter(~Q(belongs_to=request.user)).filter(requested_transfer=True)
        return render(request, 'dashboard/swap.html', context)


@login_required()
def previous_swap_page(request):
    context = {}
    if request.user.userprofile.is_admin:
        context['previous_swaps'] = PreviousTransfers.objects.filter(
            event__belongs_to__userprofile__organization=request.user.userprofile.organization)
        return render(request, 'dashboard/previous_swap_admin.html', context)


@login_required()
def get_shifts(request):
    type_shift = request.GET['type']
{'fraction_non_alphanumeric': 0.10593182428200769, 'fraction_numerical': 0.0, 'mean_word_length': 2.966643009226402, 'pattern_counts': {'":': 4, '<': 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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4008528', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2955, 'n_words': 609}