Пример #1
0
def poll(request):
    open('/app/static/data.json', 'w').close()
    form = PollForm()
    try:
        clear_message()
    except Exception as ex:
        template = "An exception of type {0} occured. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print message

    try:
        if request.POST:
            form = PollForm(request.POST)
            if form.is_valid():
                form.save()
                get_twilio_msg()
                return HttpResponseRedirect('/display/')
            else:
                print "form invalid"
        else:
            form = PollForm()

        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request, 'home.html', args)

    except Exception as ex:
        template = "An exception of type {0} occured. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print message
        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request,'home.html')
Пример #2
0
def poll(request):
    open('/app/static/data.json', 'w').close()
    form = PollForm()
    try:
        clear_message()
    except Exception as ex:
        template = "An exception of type {0} occured. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print message

    try:
        if request.POST:
            form = PollForm(request.POST)
            if form.is_valid():
                form.save()
                get_twilio_msg()
                return HttpResponseRedirect('/display/')
            else:
                print "form invalid"
        else:
            form = PollForm()

        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request, 'home.html', args)

    except Exception as ex:
        template = "An exception of type {0} occured. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print message
        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request, 'home.html')
Пример #3
0
def create(request):
    if request.method != "POST":
        return RequestErr("HTTP_METHOD_ERROR" ).response() 
    
    try: 
        param_check_dict = _create_param
        for param in param_check_dict.keys():
            if request.REQUEST.get(param) in  [None, ""]:
                missing_param = param_check_dict[param]
                return RequestErr( "REQ_PARAM_MISS", missing_param ).response()
        
        
        question_list = request.REQUEST.getlist("question_list[]")
        count = len(question_list)
        answer_list = []
        #for i in range( count ):
        #    answer_list.append( request.REQUEST.getlist("anwser_list[%d][]"% (i,) ) )
        
        pform = PollForm(request.POST)
        pform.is_valid()
        poll = pform.save(request.user)
        
        for poll_data_set_index in range( count ):
            answer_list = request.REQUEST.getlist("anwser_list[%d][]"% (poll_data_set_index,) )
            model_col_dict = {}
            for j in range(  len(answer_list)  ) :
                attr_name = "answer%d"%(j + 1,)
                model_col_dict[attr_name] = answer_list[j]
            model_col_dict['poll'] = poll
            model_col_dict['index'] = poll_data_set_index
            model_col_dict['question'] = question_list[poll_data_set_index]
            poll_data_set = PollDataSet.objects.create( **model_col_dict )
            
        #poll.
        
    except ErrClass as e:
        return e.response()
    except Exception as e:
        return ErrClass('UNKNWON_ERROR').response()
    
    return RequestErr("NOERROR").response()
Пример #4
0
 def put(self, request, id=None):
     context = {}
     question = get_object_or_404(Question, id=id)
     poll_form = PollForm(request.POST, instance=question)
     choice_forms = [ChoiceForm(request.POST, prefix=str(choice.id), instance=choice) for choice in question.choice_set.all()]
     if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]):
         new_poll = poll_form.save(commit=False)
         new_poll.created_by = request.user
         new_poll.save()
         for cf in choice_forms:
             new_choice = cf.save(commit=False)
             new_choice.question = new_poll
             new_choice.save()
         return redirect('polls_list')
     context = {'poll_form': poll_form, 'choice_forms': choice_forms}
     return render(request, 'polls/edit_poll.html', context)
Пример #5
0
def new_thread(request, subject_id):
    subject = get_object_or_404(Subject, pk=subject_id)
    poll_subject_formset = formset_factory(PollSubjectForm, extra=3)
    if request.method == "POST":
        thread_form = ThreadForm(request.POST)
        post_form = PostForm(request.POST)
        poll_form = PollForm(request.POST)
        poll_subject_formset = poll_subject_formset(request.POST)

        if thread_form.is_valid() and post_form.is_valid():
            thread = thread_form.save(False)
            thread.subject = subject
            thread.user = request.user
            thread.save()

            post = post_form.save(False)
            post.user = request.user
            post.thread = thread
            post.save()

            if request.POST.get('is_a_poll', None) and poll_form.is_valid() and poll_subject_formset.is_valid():
                poll = poll_form.save(False)
                poll.thread = thread
                poll.save()
                for subject_form in poll_subject_formset:
                    subject = subject_form.save(False)
                    subject.poll = poll
                    subject.save()
            messages.success(request, "You have successfully created a new thread")
            return redirect(reverse('thread', args={thread.pk}))

    else:
        thread_form = ThreadForm()
        post_form = PostForm(request.POST)
        poll_form = PollForm()
        poll_subject_formset = poll_subject_formset()

    args = {
        'thread_form': thread_form,
        'post_form': post_form,
        'subject': subject,
        'poll_form': poll_form,
        'poll_subject_formset': poll_subject_formset
    }
    args.update(csrf(request))

    return render(request, 'forum/thread_form.html', args)
Пример #6
0
 def post(self, request, id=None):
     context = {}
     if id:
         return self.put(request, id)
     poll_form = PollForm(request.POST, instance=Questions())
     choice_forms = [ChoiceForm(request.POST, prefix=str(
         x), instance=Choice()) for x in range(0, 3)]
     if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]):
         new_poll = poll_form.save(commit=False)
         new_poll.created_by = request.user
         new_poll.save()
         for cf in choice_forms:
             new_choice = cf.save(commit=False)
             new_choice.question = new_poll
             new_choice.save()
         return HttpResponseRedirect('/poll/')
     context = {'poll_form': poll_form, 'choice_forms': choice_forms}
     return render(request, 'polls/new_poll.html', context)
Пример #7
0
def Poll_Add(request):
    if request.method == 'POST':
        poll_form = PollForm(request.POST)
        choice_forms = [ChoiceForm(request.POST, prefix=str(x), instance=Choice()) for x in range(0, 3)]
        if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]):
            new_poll = poll_form.save(commit=False)
            new_poll.created_by = request.user
            new_poll.save()
            for cf in choice_forms:
                new_choice = cf.save(commit=False)
                new_choice.question = new_poll
                new_choice.save()
            return HttpResponseRedirect('/polls/')
        return render(request, 'polls/new_poll.html',{'poll_form': poll_form, 'choice_forms': choice_forms} )
    else:
        poll_form = PollForm(request.POST)
        choice_forms = [ChoiceForm(request.POST, prefix=str(x), instance=Choice()) for x in range(0, 3)]
        return render(request, 'polls/new_poll.html', {'poll_form': poll_form, 'choice_forms': choice_forms})
Пример #8
0
def addpoll(request):
    if not request.user.is_authenticated():
        return redirect('/accounts/login')
    else:
        context = RequestContext(request)
        if request.method == 'POST':
            form = PollForm(request.POST)
            #TODO Have we been provided with a valid form?
            if form.is_valid():
                f = form.save(commit=False)
                f.user = request.user
                f.save()
                #return redirect(thanks)
            else:
                print form.errors
        else:
            form = PollForm()
        return render_to_response('addpoll.html', {'form': form}, context)
 def post(self, request):
     context = {}
     poll_form = PollForm(request.POST, instance=Qus())
     choice_form = [
         ChoiceForm(request.POST, prefix=str(x), instance=Choices())
         for x in range(3)
     ]
     if poll_form.is_valid() and all([cf.is_valid() for cf in choice_form]):
         new_poll = poll_form.save(commit=False)
         new_poll.created_by = request.user
         new_poll.save()
         for cf in choice_form:
             new_choice = cf.save(commit=False)
             new_choice.qus_id = new_poll
             new_choice.save()
         return HttpResponseRedirect('/polls/')
     context = {'poll_form': poll_form, 'choice_form': choice_form}
     return render(request, 'poll/new_poll.html', context)
Пример #10
0
def Poll_Edit(request, id=None):
    question = get_object_or_404(Question, id=id)
    if request.method == 'POST':
        poll_form = PollForm(request.POST, instance=question)
        choice_forms = [ChoiceForm(request.POST, prefix=str(choice.id), instance=choice) for choice in question.choice_set.all()]
        if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]):
            new_poll = poll_form.save(commit=False)
            new_poll.created_by = request.user
            new_poll.save()
            for cf in choice_forms:
                new_choice = cf.save(commit=False)
                new_choice.question = new_poll
                new_choice.save()
            return HttpResponseRedirect('/polls/')
        return render(request,'polls/edit_poll.html',{'poll_form': poll_form, 'choice_forms': choice_forms})
    else:
        poll_form = PollForm(request.POST, instance=question)
        choice_forms = [ChoiceForm(request.POST, prefix=str(choice.id), instance=choice) for choice in question.choice_set.all()]
        return render(request, 'polls/edit_poll.html', {'poll_form': poll_form, 'choice_forms': choice_forms})
def poll_creation_form(request):
    if request.method == "POST":
        poll_form = PollForm(request.POST)
        content = {"form": poll_form}
        if poll_form.is_valid():

            poll = Poll(poll_name=poll_form.cleaned_data["poll_name"],
                        start_date=poll_form.cleaned_data["start_date"],
                        manager=request.user.email,
                        end_date=poll_form.cleaned_data["end_date"])
            poll.save()
            poll.member.add(request.user)
            return redirect("pollhome.page")

        else:
            return render(request, "poll/poll_form.html", content)
    else:
        poll_form = PollForm()

        content = {"form": poll_form}
    return render(request, "poll/poll_form.html", content)
Пример #12
0
def poll_view(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)
    ballotnumber = poll.ballot
    options = poll.options.order_by('user__user__first_name')
    assignment = poll.assignment
    if request.user.has_perm('assignment.can_manage_assignment'):
        if request.method == 'POST':
            form = PollForm(request.POST, prefix="poll")
            if form.is_valid():
                poll.votesinvalid = form.cleaned_data['invalid'] or 0
                poll.votescast = form.cleaned_data['votescast'] or 0
                poll.save()

            success = 0
            for option in options:
                option.form = OptionResultForm(request.POST, prefix="o%d" % option.id)
                if option.form.is_valid():
                    option.voteyes = option.form.cleaned_data['yes']
                    option.voteno = option.form.cleaned_data['no'] or 0
                    option.voteundesided = option.form.cleaned_data['undesided'] or 0
                    option.save()
                    success = success + 1
            if success == options.count():
                messages.success(request, _("Votes are successfully saved.") )
            if not 'apply' in request.POST:
               return redirect(reverse('assignment_view', args=[assignment.id]))
        else:
            form = PollForm(initial={'invalid': poll.votesinvalid, 'votescast': poll.votescast}, prefix="poll")
            for option in options:
                option.form = OptionResultForm(initial={
                    'yes': option.voteyes,
                    'no': option.voteno,
                    'undesided': option.voteundesided,
                }, prefix="o%d" % option.id)
    return {
        'poll': poll,
        'form': form,
        'options': options,
        'ballotnumber': ballotnumber,
    }
Пример #13
0
def add_poll(request):
    if request.method == "POST":
        form = PollForm(request.POST)
        if form.is_valid():
            new_poll = form.save(commit=False)
            new_poll.owner = request.user
            new_poll.save()
            new_choice1 = Choice(
                poll=new_poll,
                choice_text=form.cleaned_data['choice1']).save()
            new_choice2 = Choice(
                poll=new_poll,
                choice_text=form.cleaned_data['choice2']).save()
            messages.success(
                request,
                "Poll and Choices added successfully",
                extra_tags='alert alert-success alert-dismissible fade show')
            return redirect('poll:polls')
    else:
        form = PollForm()

    context = {'form': form}
    return render(request, 'poll/add_poll.html', context)