<commit_before>from django.db import transaction
from django.views.decorators.http import require_GET, require_POST
from django.template import RequestContext
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.conf import settings
from django import forms
from django.core.exceptions import ValidationError
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from .models import Poll, Category, Rule, Response, ResponseCategory, STARTSWITH_PATTERN_TEMPLATE, CONTAINS_PATTERN_TEMPLATE
from rapidsms.models import Contact
from simple_locations.models import Area
from mptt.forms import TreeNodeChoiceField

from xml.dom.minidom import parse, parseString

# CSV Export
@require_GET
def responses_as_csv(req, pk):
    poll = get_object_or_404(Poll, pk=pk)

    responses = poll.responses.all().order_by('-pk')

    resp = render_to_response(
        "polls/responses.csv", 
        {'responses': responses},
        mimetype="text/csv",
        context_instance=RequestContext(req))
    resp['Content-Disposition'] = 'attachment;filename="%s.csv"' % poll.name
    return resp

@require_GET
def polls(req): 
    polls = Poll.objects.all()
    breadcrumbs = (('Polls', ''),)
    return render_to_response(
        "polls/poll_index.html", 
        { 'polls': polls, 'breadcrumbs': breadcrumbs },
        context_instance=RequestContext(req))

class NewPollForm(forms.Form): # pragma: no cover
    
    TYPE_YES_NO = 'yn'
    
    type = forms.ChoiceField(
               required=True,
               choices=(
                    (Poll.TYPE_TEXT, 'Free-form'),
                    (TYPE_YES_NO, 'Yes/No Question'),                    
                    (Poll.TYPE_NUMERIC, 'Numeric Response'),
                    (Poll.TYPE_LOCATION, 'Location-based'),
                    (Poll.TYPE_REGISTRATION, 'Name/registration-based'),
                ))
    contacts = forms.ModelMultipleChoiceField(queryset=Contact.objects.all())
    name = forms.CharField(max_length=32, required=True)
    question = forms.CharField(max_length=160, required=True)
    default_response = forms.CharField(max_length=160, required=True)
    start_immediately = forms.BooleanField(required=False)

class EditPollForm(forms.ModelForm): # pragma: no cover
    class Meta:
        model = Poll
        fields = ('name', 'contacts', 'default_response')

def new_poll(req):
    if req.method == 'POST':
        form = NewPollForm(req.POST)
        if form.is_valid():
            # create our XForm
            question = form.cleaned_data['question']
            default_response = form.cleaned_data['default_response']
            contacts = form.cleaned_data['contacts']
            name = form.cleaned_data['name']
            if form.cleaned_data['type'] == Poll.TYPE_TEXT:
                poll = Poll.create_freeform(name, question, default_response, contacts, req.user)
            elif form.cleaned_data['type'] == Poll.TYPE_REGISTRATION:
                poll = Poll.create_registration(name, question, default_response, contacts, req.user)                
            elif form.cleaned_data['type'] == Poll.TYPE_NUMERIC:
                poll = Poll.create_numeric(name, question, default_response, contacts, req.user)
            elif form.cleaned_data['type'] == NewPollForm.TYPE_YES_NO:
                poll = Poll.create_yesno(name, question, default_response, contacts, req.user)
            elif form.cleaned_data['type'] == Poll.TYPE_LOCATION:
                poll = Poll.create_location_based(name, question, default_response, contacts, req.user)
            if form.cleaned_data['start_immediately']:
                poll.start()
            
            return redirect("/polls/%d/view/" % poll.pk)
    else:
        form = NewPollForm()

    return render_to_response(
        "polls/poll_create.html", { 'form': form },
        context_instance=RequestContext(req))

def view_poll(req, poll_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    categories = Category.objects.filter(poll=poll)
    breadcrumbs = (('Polls', '/polls'),('Edit Poll', ''))
    return render_to_response("polls/poll_view.html", 
        { 'poll': poll, 'categories': categories, 'category_count' : len(categories), 'breadcrumbs' : breadcrumbs },
        context_instance=RequestContext(req))<commit_msg>add site on poll creation<commit_after>from django.db import transaction
from django.views.decorators.http import require_GET, require_POST
from django.template import RequestContext
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.conf import settings
from django import forms
from django.core.exceptions import ValidationError
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from django.contrib.sites.models import Site

from .models import Poll, Category, Rule, Response, ResponseCategory, STARTSWITH_PATTERN_TEMPLATE, CONTAINS_PATTERN_TEMPLATE
from rapidsms.models import Contact
from simple_locations.models import Area
from mptt.forms import TreeNodeChoiceField

from xml.dom.minidom import parse, parseString

# CSV Export
@require_GET
def responses_as_csv(req, pk):
    poll = get_object_or_404(Poll, pk=pk)

    responses = poll.responses.all().order_by('-pk')

    resp = render_to_response(
        "polls/responses.csv", 
        {'responses': responses},
        mimetype="text/csv",
        context_instance=RequestContext(req))
    resp['Content-Disposition'] = 'attachment;filename="%s.csv"' % poll.name
    return resp

@require_GET
def polls(req): 
    polls = Poll.objects.all()
    breadcrumbs = (('Polls', ''),)
    return render_to_response(
        "polls/poll_index.html", 
        { 'polls': polls, 'breadcrumbs': breadcrumbs },
        context_instance=RequestContext(req))

class NewPollForm(forms.Form): # pragma: no cover
    
    TYPE_YES_NO = 'yn'
    
    type = forms.ChoiceField(
               required=True,
               choices=(
                    (Poll.TYPE_TEXT, 'Free-form'),
                    (TYPE_YES_NO, 'Yes/No Question'),                    
                    (Poll.TYPE_NUMERIC, 'Numeric Response'),
                    (Poll.TYPE_LOCATION, 'Location-based'),
                    (Poll.TYPE_REGISTRATION, 'Name/registration-based'),
                ))
    contacts = forms.ModelMultipleChoiceField(queryset=Contact.objects.all())
    name = forms.CharField(max_length=32, required=True)
    question = forms.CharField(max_length=160, required=True)
    default_response = forms.CharField(max_length=160, required=True)
    start_immediately = forms.BooleanField(required=False)

class EditPollForm(forms.ModelForm): # pragma: no cover
    class Meta:
        model = Poll
        fields = ('name', 'contacts', 'default_response')

def new_poll(req):
    if req.method == 'POST':
        form = NewPollForm(req.POST)
        if form.is_valid():
            # create our XForm
            question = form.cleaned_data['question']
            default_response = form.cleaned_data['default_response']
            contacts = form.cleaned_data['contacts']
            name = form.cleaned_data['name']
            if form.cleaned_data['type'] == Poll.TYPE_TEXT:
                poll = Poll.create_freeform(name, question, default_response, contacts, req.user)
            elif form.cleaned_data['type'] == Poll.TYPE_REGISTRATION:
                poll = Poll.create_registration(name, question, default_response, contacts, req.user)                
            elif form.cleaned_data['type'] == Poll.TYPE_NUMERIC:
                poll = Poll.create_numeric(name, question, default_response, contacts, req.user)
            elif form.cleaned_data['type'] == NewPollForm.TYPE_YES_NO:
                poll = Poll.create_yesno(name, question, default_response, contacts, req.user)
            elif form.cleaned_data['type'] == Poll.TYPE_LOCATION:
                poll = Poll.create_location_based(name, question, default_response, contacts, req.user)
            if form.cleaned_data['start_immediately']:
                poll.start()
            if settings.SITE_ID:
                poll.sites.add(Site.objects.get_current())
            return redirect("/polls/%d/view/" % poll.pk)
    else:
        form = NewPollForm()

    return render_to_response(
        "polls/poll_create.html", { 'form': form },
        context_instance=RequestContext(req))

def view_poll(req, poll_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    categories = Category.objects.filter(poll=poll)
    breadcrumbs = (('Polls', '/polls'),('Edit Poll', ''))
    return render_to_response("polls/poll_view.html", 
        { 'poll': poll, 'categories': categories, 'category_count' : len(categories), 'breadcrumbs' : breadcrumbs },
        context_instance=RequestContext(req))
{'fraction_non_alphanumeric': 0.10969416544408081, 'fraction_numerical': 0.0038370387089493286, 'mean_word_length': 3.0281818181818183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1605512', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2588, 'n_words': 526}