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(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 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 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 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 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 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 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 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))