<commit_before># coding: utf-8

import json
import datetime

from django.core.urlresolvers import reverse, reverse_lazy
from django.contrib import messages
from django.http.response import HttpResponseRedirect, JsonResponse
from django.shortcuts import render, get_object_or_404
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView, FormView
from django.views.generic.edit import UpdateView

from django.contrib.auth.decorators import login_required, permission_required

from django.utils.translation import ugettext_lazy as _

from .models import Location, Need
from notifications.models import Notification
from registration.models import RegistrationProfile
from .forms import RegisterForNeedForm


class LoginRequiredMixin(object):
    @method_decorator(login_required())
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)


class HomeView(TemplateView):
    template_name = "home.html"

    def get(self, request, *args, **kwargs):
        if self.request.user.is_authenticated():
            return HttpResponseRedirect(reverse('helpdesk'))
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context['locations'] = Location.objects.all()
        context['notifications'] = Notification.objects.all()
        return context


class HelpDesk(LoginRequiredMixin, TemplateView):
    """
    Location overview. First view that a volunteer gets redirected to when they log in.
    """
    template_name = "helpdesk.html"

    def get_context_data(self, **kwargs):
        context = super(HelpDesk, self).get_context_data(**kwargs)
        locations = context['locations'] = Location.objects.all()
        the_dates = [{loc: loc.get_dates_of_needs()} for loc in locations]
        context['need_dates_by_location'] = the_dates
        context['notifications'] = Notification.objects.all()
        return context


class ProfileView(LoginRequiredMixin, UpdateView):
    """
    Allows a user to update their profile.

    Maik isn't sure if this is linked to from anywhere. The template looks nasty.
    """
    fields = ['first_name', 'last_name', 'email']
    template_name = "profile_edit.html"
    success_url = reverse_lazy('helpdesk')

    def get_object(self, queryset=None):
        return self.request.user


class PlannerView(LoginRequiredMixin, FormView):
    """
    View that gets shown to volunteers when they browse a specific day.
    It'll show all the available needs, and they can add and remove
    themselves from needs.
    """
    template_name = "helpdesk_single.html"
    form_class = RegisterForNeedForm

    def get_context_data(self, **kwargs):
        context = super(PlannerView, self).get_context_data(**kwargs)
        context['needs'] = Need.objects.filter(location__pk=self.kwargs['pk']) \
            .filter(time_period_to__date_time__year=self.kwargs['year'],
                    time_period_to__date_time__month=self.kwargs['month'],
                    time_period_to__date_time__day=self.kwargs['day']) \
            .order_by('topic', 'time_period_to__date_time')
        return context

    def form_invalid(self, form):
        messages.warning(self.request, _(u'The submitted data was invalid.'))
        return super(PlannerView, self).form_invalid(form)

    def form_valid(self, form):
        reg_profile = self.request.user.registrationprofile
        need = form.cleaned_data['need']
        if form.cleaned_data['action'] == RegisterForNeedForm.ADD:
            conflicts = need.get_conflicting_needs(reg_profile.needs.all())
            if conflicts:
                # Casting the list of conflicts to a string is lazy.
                messages.warning(self.request,
                                 _(u'We can\'t add you to this shift because you\'ve already agreed to other shifts at the same time: {conflicts}'.format(
                                     conflicts=conflicts)))
            else:
                messages.success(self.request, _(u'You were successfully added to this shift.'))
                reg_profile.needs.add(need)
        elif form.cleaned_data['action'] == RegisterForNeedForm.REMOVE:
            reg_profile.needs.remove(need)
        reg_profile.save()
        return super(PlannerView, self).form_valid(form)

    def get_success_url(self):
        """
        Redirect to the same page.
        """
        return reverse('planner_by_location', kwargs=self.kwargs)


@login_required(login_url='/auth/login/')
@permission_required('location.can_view')
def volunteer_list(request, **kwargs):
    """
    Show list of volunteers for current shift
    """
    today = datetime.date.today()
    loc = get_object_or_404(Location, id=kwargs.get('loc_pk'))
    needs = Need.objects.filter(location=loc, time_period_to__date_time__contains=today)
    data = list(RegistrationProfile.objects.filter(needs__in=needs).distinct().values_list('user__email', flat=True))
    # add param ?type=json in url to get JSON data
    if request.GET.get('type') == 'json':<commit_msg>Fix unicode of Need objects in conflict list<commit_after># coding: utf-8

import json
import datetime

from django.core.urlresolvers import reverse, reverse_lazy
from django.contrib import messages
from django.http.response import HttpResponseRedirect, JsonResponse
from django.shortcuts import render, get_object_or_404
from django.utils.decorators import method_decorator

from django.views.generic import TemplateView, FormView

from django.views.generic.edit import UpdateView

from django.contrib.auth.decorators import login_required, permission_required

from django.utils.translation import ugettext_lazy as _

from .models import Location, Need
from notifications.models import Notification
from registration.models import RegistrationProfile
from .forms import RegisterForNeedForm


class LoginRequiredMixin(object):
    @method_decorator(login_required())
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)


class HomeView(TemplateView):
    template_name = "home.html"

    def get(self, request, *args, **kwargs):
        if self.request.user.is_authenticated():
            return HttpResponseRedirect(reverse('helpdesk'))
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context['locations'] = Location.objects.all()
        context['notifications'] = Notification.objects.all()
        return context


class HelpDesk(LoginRequiredMixin, TemplateView):
    """
    Location overview. First view that a volunteer gets redirected to when they log in.
    """
    template_name = "helpdesk.html"

    def get_context_data(self, **kwargs):
        context = super(HelpDesk, self).get_context_data(**kwargs)
        locations = context['locations'] = Location.objects.all()
        the_dates = [{loc: loc.get_dates_of_needs()} for loc in locations]
        context['need_dates_by_location'] = the_dates
        context['notifications'] = Notification.objects.all()
        return context


class ProfileView(LoginRequiredMixin, UpdateView):
    """
    Allows a user to update their profile.

    Maik isn't sure if this is linked to from anywhere. The template looks nasty.
    """
    fields = ['first_name', 'last_name', 'email']
    template_name = "profile_edit.html"
    success_url = reverse_lazy('helpdesk')

    def get_object(self, queryset=None):
        return self.request.user


class PlannerView(LoginRequiredMixin, FormView):
    """
    View that gets shown to volunteers when they browse a specific day.
    It'll show all the available needs, and they can add and remove
    themselves from needs.
    """
    template_name = "helpdesk_single.html"
    form_class = RegisterForNeedForm

    def get_context_data(self, **kwargs):
        context = super(PlannerView, self).get_context_data(**kwargs)
        context['needs'] = Need.objects.filter(location__pk=self.kwargs['pk']) \
            .filter(time_period_to__date_time__year=self.kwargs['year'],
                    time_period_to__date_time__month=self.kwargs['month'],
                    time_period_to__date_time__day=self.kwargs['day']) \
            .order_by('topic', 'time_period_to__date_time')
        return context

    def form_invalid(self, form):
        messages.warning(self.request, _(u'The submitted data was invalid.'))
        return super(PlannerView, self).form_invalid(form)

    def form_valid(self, form):
        reg_profile = self.request.user.registrationprofile
        need = form.cleaned_data['need']
        if form.cleaned_data['action'] == RegisterForNeedForm.ADD:
            conflicts = need.get_conflicting_needs(reg_profile.needs.all())
            if conflicts:
                conflicts_string = u", ".join(u'{}'.format(conflict) for conflict in conflicts)
                messages.warning(self.request,
                                 _(u'We can\'t add you to this shift because you\'ve already agreed to other shifts at the same time: {conflicts}'.format(conflicts=
                                     conflicts_string)))
            else:
                messages.success(self.request, _(u'You were successfully added to this shift.'))
                reg_profile.needs.add(need)
        elif form.cleaned_data['action'] == RegisterForNeedForm.REMOVE:
            reg_profile.needs.remove(need)
        reg_profile.save()
        return super(PlannerView, self).form_valid(form)

    def get_success_url(self):
        """
        Redirect to the same page.
        """
        return reverse('planner_by_location', kwargs=self.kwargs)


@login_required(login_url='/auth/login/')
@permission_required('location.can_view')
def volunteer_list(request, **kwargs):
    """
    Show list of volunteers for current shift
    """
    today = datetime.date.today()
    loc = get_object_or_404(Location, id=kwargs.get('loc_pk'))
    needs = Need.objects.filter(location=loc, time_period_to__date_time__contains=today)
    data = list(RegistrationProfile.objects.filter(needs__in=needs).distinct().values_list('user__email', flat=True))
    # add param ?type=json in url to get JSON data
    if request.GET.get('type') == 'json':
{'fraction_non_alphanumeric': 0.10151414151033235, 'fraction_numerical': 0.001333206361298924, 'mean_word_length': 3.4237573715248524, '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': '1830625', 'n_tokens_mistral': 2973, 'n_tokens_neox': 2870, 'n_words': 709}