def add_poll(request, space_url): """ Create a new poll. Only registered users belonging to a concrete group are allowed to create polls. The polls are composed by a form and a choice formset. :parameters: space_url :context: get_place """ space = get_object_or_404(Space, url=space_url) poll_form = PollForm(request.POST or None) choice_form = ChoiceFormSet(request.POST or None, prefix="choiceform", queryset=Choice.objects.none()) if (has_operation_permission(request.user, space, 'voting.add_poll', allow=['admins', 'mods'])): if request.method == 'POST': if poll_form.is_valid() and choice_form.is_valid(): poll_form_uncommited = poll_form.save(commit=False) poll_form_uncommited.space = space poll_form_uncommited.author = request.user saved_poll = poll_form_uncommited.save() poll_instance = get_object_or_404(Poll, pk=poll_form_uncommited.pk) cform_uncommited = choice_form.save(commit=False) for cf in cform_uncommited: cf.poll = poll_instance cf.save() return HttpResponseRedirect( reverse(urln.SPACE_INDEX, kwargs={'space_url': space.url})) return render_to_response('voting/poll_form.html', { 'form': poll_form, 'choiceform': choice_form, 'get_place': space }, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def edit_poll(request, space_url, poll_id): """ Edit a specific poll. :parameters: space_url, poll_id :context: form, get_place, choiceform, pollid """ place = get_object_or_404(Space, url=space_url) if has_operation_permission(request.user, place, 'voting.change_poll', allow=['admins', 'mods']): ChoiceFormSet = inlineformset_factory(Poll, Choice) instance = Poll.objects.get(pk=poll_id) poll_form = PollForm(request.POST or None, instance=instance) choice_form = ChoiceFormSet(request.POST or None, instance=instance, prefix="choiceform") if request.method == 'POST': if poll_form.is_valid() and choice_form.is_valid(): poll_form_uncommited = poll_form.save(commit=False) poll_form_uncommited.space = place poll_form_uncommited.author = request.user saved_poll = poll_form_uncommited.save() for form in choice_form.forms: choice = form.save(commit=False) choice.poll = instance choice.save() return HttpResponseRedirect( reverse(urln.SPACE_INDEX, kwargs={'space_url': place.url})) return render_to_response('voting/poll_form.html', { 'form': poll_form, 'choiceform': choice_form, 'get_place': place, 'pollid': poll_id, }, context_instance=RequestContext(request)) else: return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def edit_poll(request, space_url, poll_id): """ Edit a specific poll. :parameters: space_url, poll_id :context: form, get_place, choiceform, pollid """ place = get_object_or_404(Space, url=space_url) if has_operation_permission(request.user, place, 'voting.change_poll', allow=['admins', 'mods']): ChoiceFormSet = inlineformset_factory(Poll, Choice, extra=1) instance = Poll.objects.get(pk=poll_id) poll_form = PollForm(request.POST or None, instance=instance) choice_form = ChoiceFormSet(request.POST or None, instance=instance, prefix="choiceform") if request.method == 'POST': if poll_form.is_valid() and choice_form.is_valid(): poll_form_uncommited = poll_form.save(commit=False) poll_form_uncommited.space = place poll_form_uncommited.author = request.user saved_poll = poll_form_uncommited.save() choices = choice_form.save(commit=False) for form in choices: form.poll = instance form.save() return HttpResponseRedirect(reverse(urln.SPACE_INDEX, kwargs={'space_url': place.url})) return render_to_response('voting/poll_form.html', {'form': poll_form, 'choiceform': choice_form, 'get_place': place, 'pollid': poll_id, }, context_instance=RequestContext(request)) else: return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def add_poll(request, space_url): """ Create a new poll. Only registered users belonging to a concrete group are allowed to create polls. The polls are composed by a form and a choice formset. :parameters: space_url :context: get_place """ space = get_object_or_404(Space, url=space_url) poll_form = PollForm(request.POST or None) choice_form = ChoiceFormSet(request.POST or None, prefix="choiceform", queryset=Choice.objects.none()) if (has_operation_permission(request.user, space, 'voting.add_poll', allow=['admins', 'mods'])): if request.method == 'POST': if poll_form.is_valid() and choice_form.is_valid(): poll_form_uncommited = poll_form.save(commit=False) poll_form_uncommited.space = space poll_form_uncommited.author = request.user saved_poll = poll_form_uncommited.save() poll_instance = get_object_or_404(Poll, pk=poll_form_uncommited.pk) cform_uncommited = choice_form.save(commit=False) for cf in cform_uncommited: cf.poll = poll_instance cf.save() return HttpResponseRedirect(reverse(urln.SPACE_INDEX, kwargs={'space_url': space.url})) return render_to_response('voting/poll_form.html', {'form': poll_form, 'choiceform': choice_form, 'get_place': space}, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))