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')
def get(self, request, id=None): if id: question = get_object_or_404(Question, id=id) poll_form = PollForm(instance=question) choices = question.choice_set.all() choice_forms = [ChoiceForm(prefix=str(choice.id), instance=choice) for choice in choices] template = 'polls/edit_poll.html' else: poll_form = PollForm(instance=Question()) choice_forms = [ChoiceForm(prefix=str(x), instance=Choice()) for x in range(4)] template = 'polls/new_poll.html' context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, template, context)
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)
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})
def get(self,request,id=None): if id: pass else: poll_form = PollForm(instance=Questions()) choice_forms = [ChoiceForm(prefix=str( x), instance=Choice()) for x in range(3)] context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, 'poll/new_poll.html', context)
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 get(self, request, id=None): if id: # qus = get_object_or_404(Qus, id) qus = Qus.objects.get(id=id) poll_form = PollForm(instance=qus) choices = qus.choices_set.all() choice_form = [ ChoiceForm(prefix=str(choice.id), instance=choice) for choice in choices ] template = 'poll/edit_poll.html' else: poll_form = PollForm(instance=Qus()) choice_form = [ ChoiceForm(prefix=str(x), instance=Choices()) for x in range(3) ] template = 'poll/new_poll.html' context = {'poll_form': poll_form, 'choice_form': choice_form} return render(request, template, context)
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)
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)
def get(self, request, id=None): context = {} question = get_object_or_404(Questions, 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('poll') context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, 'poll/edit_poll.html', context)
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)
def put(self, request, id=None): print("put---->") 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, 'poll/edit_poll.html', 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)