<commit_before>from django.utils.timezone import now
from survey.models import Survey, Question, Ballot
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import View, TemplateView
from django.views.generic.detail import DetailView
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from datetime import timedelta
from survey import settings
import json
import qrcode


def survey_list_processor(request=None): 
    return {
    'published_surveys': Survey.objects.filter(Q(end_date__isnull=True) | Q(end_date__gte=now()), start_date__lte=now()),
    'unpublished_surveys': Survey.objects.filter(Q(start_date__isnull=True) | Q(start_date__gt=now())),
    'closed_surveys': Survey.objects.filter(start_date__isnull=False, end_date__lte=now()).order_by('end_date')[:10]
    }


class IndexView(TemplateView):
    template_name = 'survey/index.html'

    def get_context_data(self, *args, **kwargs):
        return survey_list_processor()


class SurveyDashboardView(DetailView):
    model = Survey
    template_name = 'survey/survey_dashboard.html'

    def post(self, request, slug):
        survey = self.get_object()
        if 'future_publish_date' in request.POST:
            survey.set_future_date('start_date', request.POST.get('future_publish_date', ''))
        elif 'future_close_date' in request.POST:
            survey.set_future_date('end_date', request.POST.get('future_close_date', ''))
        return self.get(request, slug)
                                  
    def get_context_data(self, *args, **kwargs):
        context = super(SurveyDashboardView, self).get_context_data(*args, **kwargs)
        context.update(survey_list_processor())
        return context


class SurveyView(View):
    def inactive_survey_response(self, request, duplicate=False):
        return HttpResponseForbidden(
            render_to_response('survey/survey_closed.html',
                               context_instance=RequestContext(request, {'duplicate': duplicate})
                               )
        )

    def get(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        # if the survey is not active
        # and the user is not staff
        if not survey.is_active and not request.user.is_staff:
            return self.inactive_survey_response(request, bool(request.COOKIES.get(survey.cookie, None)))
        return render_to_response('survey/survey.html', {'survey': Survey.objects.get(slug=slug)}, context_instance=RequestContext(request))

    def post(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        # prevent ballot stuffing by checking for the cookie
        # and also check to see if the survey is active
        if not request.COOKIES.get(survey.cookie, None) and survey.is_active:
            response = render_to_response('survey/survey_success.html', context_instance=RequestContext(request))
            # the cookie doesn't exist yet, it will be added to the response here
            response.set_cookie(survey.cookie, value='True', max_age=timedelta(weeks=settings.COOKIE_EXPIRATION).total_seconds())
            ballot = Ballot.objects.create(ip=request.META['REMOTE_ADDR'], survey=survey)
            for question in survey.question_set.all():
                # Found in <input name= for this question
                form_input_name = u'q%s' % question.pk
                if form_input_name not in request.POST:
                    # The browser doesn't submit blank answers so it won't even be in request.POST
                    # Move along to the next question.
                    continue
                if question.type in ('TA', 'TB'):
                    # Don't have to worry about choices for text inputs
                    form_input_value = request.POST.get(form_input_name)
                    # Submit the answer
                    question.answer_with_text(form_input_value, ballot)
                else:
                    # Decide which choices were answered for multi-choice inputs
                    form_input_values = request.POST.getlist(form_input_name)
                    # Clean the form input values from "c##" to ##, ignoring the ones that don't conform
                    scrubbed_choice_pks = [int(v[1:]) for v in form_input_values if v.startswith('c') and v[1:].isdigit()]
                    # Find all the choice objects being voted on
                    chosen_choice_objects = question.choice_set.filter(pk__in=scrubbed_choice_pks)
                    # Submit the answers
                    question.answer_with_choices(chosen_choice_objects, ballot)
            return response
        # if either are false return a forbidden response
        return self.inactive_survey_response(request)


class SurveyEditView(SurveyDashboardView):
    model = Survey
    template_name = 'survey/survey_edit.html'

    def post(self, request, slug):
        survey = self.get_object()
        data = json.loads(request.POST.get('r'))
        questions = data.get('questions', [])
        # delete existing questions
        # due to cascading deletes, this will also delete choices
        survey.question_set.all().delete()
        # edit the title if it has changed
        survey.title = data.get('title')
        survey.save()
        Question.add_questions(questions, survey)
        return HttpResponse(json.dumps({'status': 'success', 'url': survey.get_absolute_url()}), mimetype='application/json')


class SurveyResultsView(SurveyDashboardView):
    template_name = 'survey/results.html'
    model = Survey


class BallotResultsView(DetailView):
    def get(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        ballot_list = survey.ballot_set.all()
        paginator = Paginator(ballot_list, 1)

        page = request.GET.get('page')
        try:
            ballots = paginator.page(page)
        except PageNotAnInteger:
            ballots = paginator.page(1)
        except EmptyPage:
            ballots = paginator.page(paginator.num_pages)
        return render_to_response('survey/ballots.html', {"ballots": ballots, "survey": survey})


class SurveyNewView(View):
    def get(self, request):
        return render_to_response('survey/survey_new.html', context_instance=RequestContext(request))

    def post(self, request):
        data = json.loads(request.POST.get('r'))
        slug = slugify(data.get('title', ''))
        survey = Survey.objects.create(slug=slug, title=data.get('title', ''))
        questions = data.get('questions', [])
        survey.save()
        Question.add_questions(questions, survey)
        return HttpResponse(json.dumps({'status': 'success', 'url': survey.get_absolute_url()}), mimetype='application/json')


class SurveyPublishView(View):
    def get(self, request, slug):
        survey = Survey.objects.get(slug=slug)
        if request.user.is_staff:
            survey.publish()
        return HttpResponseRedirect(reverse('index'))


class SurveyCloseView(View):
    def get(self, request, slug):
        survey = Survey.objects.get(slug=slug)
        if request.user.is_staff:
            survey.close()
        return HttpResponseRedirect(reverse('index'))


class SurveyQRCodeView(View):
    def get(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        img = qrcode.make(settings.HOST_URL + survey.get_absolute_url())
        response = HttpResponse(mimetype='image/png')
        response['Content-Disposition'] = 'attachment; filename=%s.png' % survey.slug
        img.save(response, 'PNG')<commit_msg>Fixed the sidebar on the 'new survey' page<commit_after>from django.utils.timezone import now
from survey.models import Survey, Question, Ballot
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import View, TemplateView
from django.views.generic.detail import DetailView
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from datetime import timedelta
from survey import settings
import json
import qrcode


def survey_list_processor(request=None): 
    return {
    'published_surveys': Survey.objects.filter(Q(end_date__isnull=True) | Q(end_date__gte=now()), start_date__lte=now()),
    'unpublished_surveys': Survey.objects.filter(Q(start_date__isnull=True) | Q(start_date__gt=now())),
    'closed_surveys': Survey.objects.filter(start_date__isnull=False, end_date__lte=now()).order_by('end_date')[:10]
    }


class IndexView(TemplateView):
    template_name = 'survey/index.html'

    def get_context_data(self, *args, **kwargs):
        return survey_list_processor()


class SurveyDashboardView(DetailView):
    model = Survey
    template_name = 'survey/survey_dashboard.html'

    def post(self, request, slug):
        survey = self.get_object()
        if 'future_publish_date' in request.POST:
            survey.set_future_date('start_date', request.POST.get('future_publish_date', ''))
        elif 'future_close_date' in request.POST:
            survey.set_future_date('end_date', request.POST.get('future_close_date', ''))
        return self.get(request, slug)
                                  
    def get_context_data(self, *args, **kwargs):
        context = super(SurveyDashboardView, self).get_context_data(*args, **kwargs)
        context.update(survey_list_processor())
        return context


class SurveyView(View):
    def inactive_survey_response(self, request, duplicate=False):
        return HttpResponseForbidden(
            render_to_response('survey/survey_closed.html',
                               context_instance=RequestContext(request, {'duplicate': duplicate})
                               )
        )

    def get(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        # if the survey is not active
        # and the user is not staff
        if not survey.is_active and not request.user.is_staff:
            return self.inactive_survey_response(request, bool(request.COOKIES.get(survey.cookie, None)))
        return render_to_response('survey/survey.html', {'survey': Survey.objects.get(slug=slug)}, context_instance=RequestContext(request))

    def post(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        # prevent ballot stuffing by checking for the cookie
        # and also check to see if the survey is active
        if not request.COOKIES.get(survey.cookie, None) and survey.is_active:
            response = render_to_response('survey/survey_success.html', context_instance=RequestContext(request))
            # the cookie doesn't exist yet, it will be added to the response here
            response.set_cookie(survey.cookie, value='True', max_age=timedelta(weeks=settings.COOKIE_EXPIRATION).total_seconds())
            ballot = Ballot.objects.create(ip=request.META['REMOTE_ADDR'], survey=survey)
            for question in survey.question_set.all():
                # Found in <input name= for this question
                form_input_name = u'q%s' % question.pk
                if form_input_name not in request.POST:
                    # The browser doesn't submit blank answers so it won't even be in request.POST
                    # Move along to the next question.
                    continue
                if question.type in ('TA', 'TB'):
                    # Don't have to worry about choices for text inputs
                    form_input_value = request.POST.get(form_input_name)
                    # Submit the answer
                    question.answer_with_text(form_input_value, ballot)
                else:
                    # Decide which choices were answered for multi-choice inputs
                    form_input_values = request.POST.getlist(form_input_name)
                    # Clean the form input values from "c##" to ##, ignoring the ones that don't conform
                    scrubbed_choice_pks = [int(v[1:]) for v in form_input_values if v.startswith('c') and v[1:].isdigit()]
                    # Find all the choice objects being voted on
                    chosen_choice_objects = question.choice_set.filter(pk__in=scrubbed_choice_pks)
                    # Submit the answers
                    question.answer_with_choices(chosen_choice_objects, ballot)
            return response
        # if either are false return a forbidden response
        return self.inactive_survey_response(request)


class SurveyEditView(SurveyDashboardView):
    model = Survey
    template_name = 'survey/survey_edit.html'

    def post(self, request, slug):
        survey = self.get_object()
        data = json.loads(request.POST.get('r'))
        questions = data.get('questions', [])
        # delete existing questions
        # due to cascading deletes, this will also delete choices
        survey.question_set.all().delete()
        # edit the title if it has changed
        survey.title = data.get('title')
        survey.save()
        Question.add_questions(questions, survey)
        return HttpResponse(json.dumps({'status': 'success', 'url': survey.get_absolute_url()}), mimetype='application/json')


class SurveyResultsView(SurveyDashboardView):
    template_name = 'survey/results.html'
    model = Survey


class BallotResultsView(DetailView):
    def get(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        ballot_list = survey.ballot_set.all()
        paginator = Paginator(ballot_list, 1)

        page = request.GET.get('page')
        try:
            ballots = paginator.page(page)
        except PageNotAnInteger:
            ballots = paginator.page(1)
        except EmptyPage:
            ballots = paginator.page(paginator.num_pages)
        return render_to_response('survey/ballots.html', {"ballots": ballots, "survey": survey})


class SurveyNewView(TemplateView):
    template_name = 'survey/survey_new.html'
    
    def post(self, request):
        data = json.loads(request.POST.get('r'))
        slug = slugify(data.get('title', ''))
        survey = Survey.objects.create(slug=slug, title=data.get('title', ''))
        questions = data.get('questions', [])
        survey.save()
        Question.add_questions(questions, survey)
        return HttpResponse(json.dumps({'status': 'success', 'url': survey.get_absolute_url()}), mimetype='application/json')

    def get_context_data(self, *args, **kwargs):
        context = super(SurveyNewView, self).get_context_data(*args, **kwargs)
        context.update(survey_list_processor())
        return context


class SurveyPublishView(View):
    def get(self, request, slug):
        survey = Survey.objects.get(slug=slug)
        if request.user.is_staff:
            survey.publish()
        return HttpResponseRedirect(reverse('index'))


class SurveyCloseView(View):
    def get(self, request, slug):
        survey = Survey.objects.get(slug=slug)
        if request.user.is_staff:
            survey.close()
        return HttpResponseRedirect(reverse('index'))


class SurveyQRCodeView(View):
    def get(self, request, slug):
        survey = get_object_or_404(Survey, slug=slug)
        img = qrcode.make(settings.HOST_URL + survey.get_absolute_url())
        response = HttpResponse(mimetype='image/png')
        response['Content-Disposition'] = 'attachment; filename=%s.png' % survey.slug
        img.save(response, 'PNG')
{'fraction_non_alphanumeric': 0.09991197183098592, 'fraction_numerical': 0.002640845070422535, 'mean_word_length': 2.9584370333499255, 'pattern_counts': {'":': 4, '<': 5, '<?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': '865859', 'n_tokens_mistral': 4592, 'n_tokens_neox': 4480, 'n_words': 1027}