def add_poll(): """ Add a poll to the database """ check_admin() add_poll = True form = PollForm() if form.validate_on_submit(): poll = Poll(name=form.name.data, description=form.description.data) try: # add department to the database db.session.add(poll) db.session.commit() flash('You have successfully added a new poll.') except: # in case poll name already exists flash('Error: poll name already exists.') # redirect to polls page return redirect(url_for('admin.list_polls')) # load department template return render_template('admin/polls/poll.html', action="Add", add_poll=add_poll, form=form, title="Add Poll")
def add_poll(): poll_form = PollForm() if poll_form.validate_on_submit(): poll = models.Poll(body=poll_form.poll.data, user_id=g.user.id, cat_id=poll_form.poll_category.data, anonymous=poll_form.anonymous.data) db.session.add(poll) db.session.commit() poll = models.Poll.query.filter_by(body=poll_form.poll.data).first() choice1 = models.Choice(poll_id=poll.id, value=poll_form.choice1.data) db.session.add(choice1) choice2 = models.Choice(poll_id=poll.id, value=poll_form.choice2.data) db.session.add(choice2) if str(poll_form.choice3.data ) is not None and poll_form.choice3.data != '': choice3 = models.Choice(poll_id=poll.id, value=poll_form.choice3.data) db.session.add(choice3) if str(poll_form.choice4.data ) is not None and poll_form.choice4.data != '': choice4 = models.Choice(poll_id=poll.id, value=poll_form.choice4.data) db.session.add(choice4) db.session.commit() flash('the data was', poll_form.choice3.data) return redirect(url_for('index')) return render_template('add_poll.html', form=poll_form)
def new(request): if request.method == 'POST': poll_form = PollForm(request.POST) if poll_form.is_valid(): new_poll = Poll.objects.create( question=poll_form.cleaned_data['question'], creator=request.user ) answer_formset = AnswerFormSet(request.POST, instance=new_poll) if answer_formset.is_valid(): answer_formset.save() redirect_url = getattr( settings, 'ASKMEANYTHING_POST_CREATE_URL', reverse( 'askmeanything.views.created', kwargs={'poll_id': new_poll.id} ) ) return HttpResponseRedirect(redirect_url) else: new_poll.delete() poll_form = PollForm() answer_formset = AnswerFormSet() return render_to_response( 'askmeanything/poll_create.html', {'poll_form': poll_form, 'answer_formset': answer_formset}, context_instance=RequestContext(request) )
def post(self, request, poll_id): user_polls = Poll.objects.filter(creator=request.user) poll = get_object_or_404(Poll, pk=poll_id) form = PollForm(request.POST, instance=poll) if form.is_valid(): poll_save = form.save() else: return render(request, EditPoll.template_name, {'form':form, 'edit': True}) return redirect(request, reverse('poll:show', poll_id=new_poll.id), {'form':form})
def createPoll(request): if request.method == "POST": poll_form = PollForm(data=request.POST) if poll_form.is_valid(): poll = poll_form.save(commit=False) poll.creator = request.user poll.save() return redirect('/createquestion/'+str(poll.id)) poll_form = PollForm() return render_to_response("create_poll.html", {'poll_form': poll_form}, context_instance=RequestContext(request))
def create_room(request, extra_context={}): """ View for creating a room. Uses a clever combination of PollForm, RoomForm and ChoiceFormSet to achieve 3-way model creation: - PollForm allows the user to specify the question - ChoiceFormSet allows the user to specify an arbitrary number of "choices" to go with the question (each one represented by its own DB object) - RoomForm gives more advanced "tweaks" for the room, for example: - period length (how long each period lasts, default is 30) - join threshold (the amount of time that a room is in lock mode before a poll begins) """ if request.method == "POST": # if the user has submitted the form, get the data from all three poll_form = PollForm(request.POST, request.FILES) choice_formset = ChoiceFormSet(request.POST, request.FILES) room_form = RoomForm(request.POST, request.FILES) if poll_form.is_valid() and choice_formset.is_valid() and room_form.is_valid(): # if all 3 forms are valid, create a new question object and save it q = Question(text=poll_form.cleaned_data['question']) q.save() # create a new poll object that points to the question, and save it p = Poll(question=q) p.save() # for every choice the user has inputted for form in choice_formset.forms: # create a new choice object, and point it at the question created earlier c = Choice(question=q, text=form.cleaned_data['choice']) c.save() # finally, create the room itself, pointing to the question object, with the creator of the # currently logged in user, and the period length & join threshold as specified in the form # data. r = Room(question=q, opened_by=request.user, controller=request.user, period_length=room_form.cleaned_data['period_length'], join_threshold=room_form.cleaned_data['join_threshold']) r.save() # redirect the user to their newly created room return HttpResponseRedirect(r.get_absolute_url()) else: # if the user has not submitted the form (i.e. wishes to fill the form in) # then initialise the 3 forms with no data poll_form = PollForm() choice_formset = ChoiceFormSet() room_form = RoomForm() # put the forms into a context dictionary (and update that with any extra context we have been given) data = {'poll_form': poll_form, 'choice_formset': choice_formset, 'room_form': room_form} data.update(extra_context) # render the page return render_to_response('rooms/room_form.html', data, context_instance=RequestContext(request))
def add_edit_poll(request): if request.method == 'POST': form = PollForm(request.POST) if form.is_valid(): #TODO: have pub_date set automatically p = Poll(question=form.cleaned_data['question'], pub_date=timezone.now()) p.save() return HttpResponseRedirect(reverse('poll_detail', args=(p.id,))) else: form = PollForm() return render_to_response('polls/add_edit_poll.html', {'form': form}, context_instance=RequestContext(request))
def create(request): if request.method == 'POST': form = PollForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('polls:administration:index')) else: form = PollForm() return render(request, 'polls/administration/create.html', {'form': form, 'active_module': 'polls'})
def poll_create(): form = PollForm(request.form) app.logger.debug(form) if form.validate_on_submit(): poll = Poll.create(form.question.data) for choice in form.choices: if choice.data: Choice.create(poll.key, choice.data) return redirect(url_for('poll_detail', poll_key=poll.key)) return render_template('poll_create.html', form=form)
def vote(request, poll_id, template='polls/detail.html'): poll = get_object_or_404(Poll, pk=poll_id) pollform = PollForm(request.POST or None, instance=poll) if pollform.is_valid(): instance = pollform.save(commit=False,) choice = instance.choice_set.get(id=pollform.cleaned_data['votes']) choice.votes += 1 choice.save() return redirect(reverse('results',args=(instance.id,))) return render(request, template,{'form': pollform,},)
def add_poll(): """ Function that allows admin to create a new poll """ error = None form = PollForm(csrf_enabled=False) if form.validate_on_submit(): new_poll = Poll(form.question.data) db.session.add(new_poll) db.session.commit() flash("New entry was successfully posted. Thanks.") else: flash_errors(form) return redirect(url_for("admin_main"))
def edit(request, poll_id): poll = get_object_or_404(Poll, id=poll_id) if request.method == 'POST': form = PollForm(request.POST, instance=poll) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('polls:administration:index')) else: form = PollForm(model_to_dict(poll),instance=poll) return render(request, 'polls/administration/edit.html', {'form': form, 'poll':poll, 'active_module': 'polls'})
def post(self, request): form = PollForm(request.POST) if form.is_valid(): import datetime from django.utils import timezone new_poll = form.save(commit=False) new_poll.expires = timezone.now() + datetime.timedelta(hours=48) new_poll.creator = request.user new_poll.save() else: return render(request, NewPoll.template_name, {'form': form}) return redirect('poll:show', pk=new_poll.id)
def detail(request, id): poll = Poll.objects.filter(id=id)[0] if request.method == 'POST': form = PollForm(request.POST) if form.is_valid(): choice = Choice.objects.filter(id=form.cleaned_data['choice_id'])[0] choice.votes += 1 choice.save() return HttpResponseRedirect('/test/thanks/') return render(request, 'detail.html', { 'poll': poll, 'choices': poll.choice_set, })
def add_poll(request): if request.method == "POST": pform = PollForm(request.POST, instance=Poll()) cforms = [ChoiceForm(request.POST, prefix=str(x), instance=Choice()) for x in range(0,3)] if pform.is_valid() and all([cf.is_valid() for cf in cforms]): new_poll = pform.save() for cf in cforms: new_choice = cf.save(commit=False) new_choice.poll = new_poll new_choice.save() return HttpResponseRedirect('/polls/add/') else: pform = PollForm(instance=Poll()) cforms = [ChoiceForm(prefix=str(x), instance=Choice()) for x in range(0,3)] return render_to_response('add_poll.html', {'poll_form': pform, 'choice_forms': cforms})
def create(request): if request.method == 'POST': form = PollForm(request.POST) if form.is_valid(): poll = form.save(commit = False) poll.save() choices = [Choice(poll = poll, choice = a['choice']) for a in form.cleaned_data['choices']] Choice.objects.bulk_create(choices) return HttpResponseRedirect(poll.get_absolute_url()) else: form = PollForm() context = { 'form': form } return TemplateResponse(request, "polls/poll_create.html", RequestContext(request, context))
def vote(request, poll_id, template='polls/detail.html'): poll = get_object_or_404(Poll, pk=poll_id) pollform = PollForm(request.POST or None, instance=poll) if pollform.is_valid(): instance = pollform.save(commit=False, ) choice = instance.choice_set.get(id=pollform.cleaned_data['votes']) choice.votes += 1 choice.save() return redirect(reverse('results', args=(instance.id, ))) return render( request, template, { 'form': pollform, }, )
def create_poll(request, library_code, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden( u'Вы должны быть сотрудником этой организации') if request.method == 'POST': poll_form = PollForm(request.POST, request.FILES, prefix='poll_form') poll_content_forms = [] for lang in settings.LANGUAGES: poll_content_forms.append({ 'form': PollContentForm(request.POST, prefix='poll_content' + lang[0]), 'lang': lang[0] }) if poll_form.is_valid(): valid = False for poll_content_form in poll_content_forms: valid = poll_content_form['form'].is_valid() if not valid: break if valid: poll = poll_form.save(commit=False) poll.library = library poll.save() for poll_content_form in poll_content_forms: poll_content = poll_content_form['form'].save(commit=False) poll_content.lang = poll_content_form['lang'] poll_content.poll = poll poll_content.save() poll_form.save_m2m() return redirect( 'participant_photopolls:administration:polls_list', library_code=library_code) else: poll_form = PollForm(prefix="poll_form") poll_content_forms = [] for lang in settings.LANGUAGES: poll_content_forms.append({ 'form': PollContentForm(prefix='poll_content' + lang[0]), 'lang': lang[0] }) return render( request, 'participant_photopolls/administration/create_poll.html', { 'library': library, 'poll_form': poll_form, 'poll_content_forms': poll_content_forms, })
def add_poll(): poll_form=PollForm() if poll_form.validate_on_submit(): poll=models.Poll( body=poll_form.poll.data, user_id=g.user.id, cat_id=poll_form.poll_category.data, anonymous=poll_form.anonymous.data ) db.session.add(poll) db.session.commit() poll = models.Poll.query.filter_by(body=poll_form.poll.data).first() choice1 = models.Choice( poll_id=poll.id, value=poll_form.choice1.data ) db.session.add(choice1) choice2 = models.Choice( poll_id=poll.id, value=poll_form.choice2.data ) db.session.add(choice2) if str(poll_form.choice3.data) is not None and poll_form.choice3.data != '': choice3 = models.Choice( poll_id=poll.id, value=poll_form.choice3.data ) db.session.add(choice3) if str(poll_form.choice4.data) is not None and poll_form.choice4.data != '': choice4 = models.Choice( poll_id=poll.id, value=poll_form.choice4.data ) db.session.add(choice4) db.session.commit() flash('the data was', poll_form.choice3.data) return redirect(url_for('index')) return render_template('add_poll.html', form=poll_form)
def add_poll(request): data = {} if request.method == "POST": form = PollForm(request.POST) if form.is_valid(): # question = form.cleaned_data["question"] # description = form.cleaned_data["description"] # active = form.cleaned_data["active"] # # answer = Poll(question=question, description=description, active=active) # answer.save() form.save() data["message"] = "Answer saved." else: form = PollForm() data["poll_form"] = form return render(request,"form.html", data)
def create_poll(request): if request.method == 'POST': form = PollForm(request, request.POST) if form.is_valid(): p = form.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': p.slug})) else: form = PollForm(request) sidebar_polls = get_sidebar_polls(request.user) t = loader.get_template('polls-create-poll.html') c = RequestContext(request, {'form': form, 'sidebar_polls': sidebar_polls, 'navigation': 'polls', 'navigation2': 'polls-create',}) return HttpResponse(t.render(c))
def edit_poll(id): """ Edit a poll """ check_admin() add_poll = False poll = Poll.query.get_or_404(id) form = PollForm(obj=poll) if form.validate_on_submit(): poll.name = form.name.data poll.description = form.description.data db.session.commit() flash('You have successfully edited the poll.') # redirect to the departments page return redirect(url_for('admin.list_polls')) form.description.data = poll.description form.name.data = poll.name return render_template('admin/polls/poll.html', action="Edit", add_poll=add_poll, form=form, poll=poll, title="Edit Poll")
def createPoll(request): if request.method == "POST": poll_form = PollForm(data=request.POST) if poll_form.is_valid(): poll = poll_form.save(commit=False) poll.creator = request.user poll.save() return redirect('/createquestion/' + str(poll.id)) poll_form = PollForm() return render_to_response("create_poll.html", {'poll_form': poll_form}, context_instance=RequestContext(request))
def create_poll(request, library_code, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden(u'Вы должны быть сотрудником этой организации') if request.method == 'POST': poll_form = PollForm(request.POST,request.FILES, prefix='poll_form') poll_content_forms = [] for lang in settings.LANGUAGES: poll_content_forms.append({ 'form':PollContentForm(request.POST,prefix='poll_content' + lang[0]), 'lang':lang[0] }) if poll_form.is_valid(): valid = False for poll_content_form in poll_content_forms: valid = poll_content_form['form'].is_valid() if not valid: break if valid: poll = poll_form.save(commit=False) poll.library = library poll.save() for poll_content_form in poll_content_forms: poll_content = poll_content_form['form'].save(commit=False) poll_content.lang = poll_content_form['lang'] poll_content.poll = poll poll_content.save() poll_form.save_m2m() return redirect('participant_photopolls:administration:polls_list', library_code=library_code) else: poll_form = PollForm(prefix="poll_form") poll_content_forms = [] for lang in settings.LANGUAGES: poll_content_forms.append({ 'form': PollContentForm(prefix='poll_content' + lang[0]), 'lang': lang[0] }) return render(request, 'participant_photopolls/administration/create_poll.html', { 'library': library, 'poll_form': poll_form, 'poll_content_forms': poll_content_forms, })
def poll_create_or_update(request, id=None): if id: p = get_object_or_404(Poll, pk=id) if request.method == 'POST': if id: form = PollForm(request.POST, instance=p) else: form = PollForm(request.POST) if form.is_valid(): obj = form.save() return render_to_rest(obj) else: if request.is_rest(): return render_form_error_to_rest(form) else: if id: form = PollForm(instance=p) else: form = PollForm() return render_to_response('update.html', { 'form': form, }, context_instance=RequestContext(request) )
def edit_poll(request, library_code, id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden( u'Вы должны быть сотрудником этой организации') poll = get_object_or_404(Poll, id=id) poll_contents = PollContent.objects.filter(poll=poll) poll_contents_langs = {} for lang in settings.LANGUAGES: poll_contents_langs[lang] = None for poll_content in poll_contents: poll_contents_langs[poll_content.lang] = poll_content if request.method == 'POST': poll_form = PollForm(request.POST, request.FILES, prefix='poll_form', instance=poll) poll_content_forms = [] if poll_form.is_valid(): poll_form.save() poll_content_forms = [] for lang in settings.LANGUAGES: if lang in poll_contents_langs: lang = lang[0] if lang in poll_contents_langs: poll_content_forms.append({ 'form': PollContentForm( request.POST, prefix='poll_content_' + lang, instance=poll_contents_langs[lang]), 'lang': lang }) else: poll_content_forms.append({ 'form': PollContentForm(request.POST, prefix='poll_content_' + lang), 'lang': lang }) valid = False for poll_content_form in poll_content_forms: valid = poll_content_form['form'].is_valid() if not valid: break if valid: for poll_content_form in poll_content_forms: poll_content = poll_content_form['form'].save(commit=False) poll_content.poll = poll poll_content.lang = poll_content_form['lang'] poll_content.save() return redirect( 'participant_photopolls:administration:polls_list', library_code=library_code) else: poll_form = PollForm(prefix="poll_form", instance=poll) poll_content_forms = [] for lang in settings.LANGUAGES: lang = lang[0] if lang in poll_contents_langs: poll_content_forms.append({ 'form': PollContentForm(prefix='poll_content_' + lang, instance=poll_contents_langs[lang]), 'lang': lang }) else: poll_content_forms.append({ 'form': PollContentForm(prefix='poll_content_' + lang), 'lang': lang }) return render( request, 'participant_photopolls/administration/edit_poll.html', { 'poll': poll, 'library': library, 'poll_form': poll_form, 'poll_content_forms': poll_content_forms, 'content_type': 'participant_photopolls_' + str(library.id), 'content_id': unicode(poll.id) })
def edit_poll(request, library_code, id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden(u'Вы должны быть сотрудником этой организации') poll = get_object_or_404(Poll, id=id) poll_contents = PollContent.objects.filter(poll=poll) poll_contents_langs = {} for lang in settings.LANGUAGES: poll_contents_langs[lang] = None for poll_content in poll_contents: poll_contents_langs[poll_content.lang] = poll_content if request.method == 'POST': poll_form = PollForm(request.POST, request.FILES, prefix='poll_form', instance=poll) poll_content_forms = [] if poll_form.is_valid(): poll_form.save() poll_content_forms = [] for lang in settings.LANGUAGES: if lang in poll_contents_langs: lang = lang[0] if lang in poll_contents_langs: poll_content_forms.append({ 'form':PollContentForm(request.POST,prefix='poll_content_' + lang, instance=poll_contents_langs[lang]), 'lang':lang }) else: poll_content_forms.append({ 'form':PollContentForm(request.POST,prefix='poll_content_' + lang), 'lang':lang }) valid = False for poll_content_form in poll_content_forms: valid = poll_content_form['form'].is_valid() if not valid: break if valid: for poll_content_form in poll_content_forms: poll_content = poll_content_form['form'].save(commit=False) poll_content.poll = poll poll_content.lang = poll_content_form['lang'] poll_content.save() return redirect('participant_photopolls:administration:polls_list', library_code=library_code) else: poll_form = PollForm(prefix="poll_form", instance=poll) poll_content_forms = [] for lang in settings.LANGUAGES: lang = lang[0] if lang in poll_contents_langs: poll_content_forms.append({ 'form':PollContentForm(prefix='poll_content_' + lang, instance=poll_contents_langs[lang]), 'lang':lang }) else: poll_content_forms.append({ 'form':PollContentForm(prefix='poll_content_' + lang), 'lang':lang }) return render(request, 'participant_photopolls/administration/edit_poll.html', { 'poll': poll, 'library': library, 'poll_form': poll_form, 'poll_content_forms': poll_content_forms, 'content_type': 'participant_photopolls_' + str(library.id), 'content_id': unicode(poll.id) })
def edit_poll(request, slug): poll = get_object_or_404(Poll, slug=slug) if request.user != poll.user: raise PermissionDenied("You must own a poll in order to edit it.") choices = Choice.objects.get_choices_and_votes_for_poll(poll.id) poll_form = PollForm(request, instance=poll, prefix='poll') choice_form = ChoiceForm(request, poll, prefix='choice') if request.method == 'POST': if 'poll' in request.POST: poll_form = PollForm(request, request.POST, instance=poll, prefix='poll') if poll_form.is_valid(): p = poll_form.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': p.slug})) elif 'choice' in request.POST: choice_form = ChoiceForm(request, poll, request.POST, prefix='choice') if choice_form.is_valid(): choice, created = Choice.objects \ .get_or_create(poll=poll, choice=choice_form.cleaned_data['choice'], defaults={'user': request.user}) return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 'delete-choice' in request.POST and 'choice-id' in request.POST: try: choice = Choice.objects.get(id=request.POST['choice-id'], poll=poll) \ .delete() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) except Choice.DoesNotExist: raise Http404 elif 'delete' in request.POST: poll.delete() return HttpResponseRedirect(reverse('molnet-polls-startpage')) elif 'close' in request.POST: poll.status="CLOSED" poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 're-open' in request.POST: poll.status="PUBLISHED" poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 'unpublish' in request.POST: poll.status="DRAFT" poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 'publish' in request.POST: poll.status="PUBLISHED" poll.published_at = datetime.datetime.now() poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) else: raise Http404 related_polls = None sidebar_polls = get_sidebar_polls(request.user) t = loader.get_template('polls-edit-poll.html') c = RequestContext(request, {'poll': poll, 'choices': choices, 'choice_form': choice_form, 'poll_form': poll_form, 'related_polls': related_polls, 'sidebar_polls': sidebar_polls}) return HttpResponse(t.render(c))
def create_room(request, extra_context={}): """ View for creating a room. Uses a clever combination of PollForm, RoomForm and ChoiceFormSet to achieve 3-way model creation: - PollForm allows the user to specify the question - ChoiceFormSet allows the user to specify an arbitrary number of "choices" to go with the question (each one represented by its own DB object) - RoomForm gives more advanced "tweaks" for the room, for example: - period length (how long each period lasts, default is 30) - join threshold (the amount of time that a room is in lock mode before a poll begins) """ if request.method == "POST": # if the user has submitted the form, get the data from all three poll_form = PollForm(request.POST, request.FILES) choice_formset = ChoiceFormSet(request.POST, request.FILES) room_form = RoomForm(request.POST, request.FILES) if poll_form.is_valid() and choice_formset.is_valid( ) and room_form.is_valid(): # if all 3 forms are valid, create a new question object and save it q = Question(text=poll_form.cleaned_data['question']) q.save() # create a new poll object that points to the question, and save it p = Poll(question=q) p.save() # for every choice the user has inputted for form in choice_formset.forms: # create a new choice object, and point it at the question created earlier c = Choice(question=q, text=form.cleaned_data['choice']) c.save() # finally, create the room itself, pointing to the question object, with the creator of the # currently logged in user, and the period length & join threshold as specified in the form # data. r = Room(question=q, opened_by=request.user, controller=request.user, period_length=room_form.cleaned_data['period_length'], join_threshold=room_form.cleaned_data['join_threshold']) r.save() # redirect the user to their newly created room return HttpResponseRedirect(r.get_absolute_url()) else: # if the user has not submitted the form (i.e. wishes to fill the form in) # then initialise the 3 forms with no data poll_form = PollForm() choice_formset = ChoiceFormSet() room_form = RoomForm() # put the forms into a context dictionary (and update that with any extra context we have been given) data = { 'poll_form': poll_form, 'choice_formset': choice_formset, 'room_form': room_form } data.update(extra_context) # render the page return render_to_response('rooms/room_form.html', data, context_instance=RequestContext(request))