Esempio n. 1
0
def add_category(req, poll_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    form = CategoryForm()

    if req.method == "POST":
        form = CategoryForm(req.POST)
        if form.is_valid():
            if form.cleaned_data["default"] == True:
                for c in Category.objects.filter(poll=poll, default=True):
                    c.default = False
                    c.save()
            category = form.save(commit=False)
            category.poll = poll
            category.save()
            poll.categories.add(category)
            return render_to_response(
                "polls/category_view.html",
                {"category": category, "form": form, "poll": poll},
                context_instance=RequestContext(req),
            )
    else:
        form = CategoryForm()

    return render_to_response(
        "polls/category_edit.html", {"form": form, "poll": poll}, context_instance=RequestContext(req)
    )
Esempio n. 2
0
def edit_category(req, poll_id, category_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    category = get_object_or_404(Category, pk=category_id)
    if req.method == "POST":
        form = CategoryForm(req.POST, instance=category)

        if form.is_valid():
            if form.cleaned_data["default"] == True:
                Category.clear_defaults(poll)
            category = form.save(commit=False)
            category.poll = poll
            category.save()
            return render_to_response(
                "polls/category_view.html",
                {"form": form, "poll": poll, "category": category},
                context_instance=RequestContext(req),
            )
        else:
            return render_to_response(
                "polls/category_edit.html",
                {"form": form, "poll": poll, "category": category},
                context_instance=RequestContext(req),
            )
    else:
        form = CategoryForm(instance=category)

    return render_to_response(
        "polls/category_edit.html",
        {"form": form, "poll": poll, "category": category},
        context_instance=RequestContext(req),
    )
Esempio n. 3
0
def view_poll(request, pk):
    poll = get_object_or_404(UPoll, pk=pk)
    groups = Group.objects.filter(pk__in=poll.contacts.values_list('groups'))
    category = None
    if request.GET.get('poll'):
        if request.GET.get('start'):
            poll = Poll.objects.get(pk=pk)
            if getattr(settings, 'START_POLL_MULTI_TX', False):
                start_poll_multi_tx(poll)
            elif getattr(
                    settings, 'START_POLLS_IN_WEB_THREAD', False
            ):  #Starting poll in the web thread might still be good especially when testing (in development)
                log.info(
                    "[start-poll-in-web-thread] Starting poll [%s] in web thread..."
                    % str(poll.pk))
                if not poll.start_date:
                    poll.start()
                log.info("[poll-in-web-thread-started] Start Ok.")
            else:
                start_poll_single_tx(poll)

            if getattr(settings, "FEATURE_PREPARE_SEND_POLL", False):
                res = """ <a href="?send=True&poll=True" data-remote=true  id="poll_action" class="btn">Send Poll</a> """
            else:
                res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a> """

            return HttpResponse(res)

        if request.GET.get('send'):
            log.info("[send-poll] queuing...")
            poll.queue_message_batches_to_send()
            log.info("[send-poll] done.")
            res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a> """
            return HttpResponse(res)

        if request.GET.get('stop'):
            poll.end()
            res = HttpResponse(
                """ <a href="?reopen=True&poll=True" data-remote=true id="poll_action" class="btn">Reopen Poll</a> """
            )
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get("reopen"):
            poll.end_date = None
            poll.save()
            res = HttpResponse(
                """<a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a>  """
            )
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('viewable'):
            poll.set_attr('viewable', True)
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?unviewable=True&poll=True\')">Don\'t Show On Home page</a>'
            )
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('unviewable'):
            poll.set_attr('viewable', False)
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?viewable=True&poll=True\')">Show On Home page</a>')
            res['Cache-Control'] = 'no-store'
            return res
    if settings.USSD_ENABLED:
        x = XForm.objects.get(name='poll')
        xf, _ = XFormField.objects.get_or_create(
            name='latest_poll',
            xform=x,
            field_type=XFormField.TYPE_TEXT,
            command="poll_%d" % poll.pk)
        response = StubScreen.objects.get_or_create(slug='question_response')
    template = 'ureport/polls/view_poll.html'
    categories = poll.categories.all()
    category_form = CategoryForm()
    rule_form = RuleForm2()
    if request.method == "POST":
        if request.GET.get('edit'):
            if request.POST.get('poll[default_response]'):
                poll.default_response = request.POST['poll[default_response]']
                poll.save()
            if request.POST.get('poll[question]'):
                poll.question = request.POST['poll[question]']
                poll.save()
        if settings.USSD_ENABLED:
            if request.GET.get("ussd", None):
                question = request.POST.get("question")
                response = request.POST.get("response")
                xf.question = question
                xf.save()
                response.text = response
                response.save()
        if request.GET.get("category", None):
            if request.GET.get('cat_pk'):
                category = Category.objects.get(
                    pk=int(request.GET.get('cat_pk')))
            else:
                category = Category()
                category.poll = poll
            category_form = CategoryForm(request.POST, instance=category)
            if category_form.is_valid():
                template = "ureport/polls/rules.html"
                category = category_form.save()
                request.session['category'] = category
            else:
                template = "ureport/polls/category.html"

        if request.GET.get("rules", None):
            rule = Rule()
            rule.category = request.session['category']
            rule_form = RuleForm2(request.POST, instance=rule)
            if rule_form.is_valid:
                rule_form.save()
            else:
                template = "ureport/polls/rules.html"

    if not settings.USSD_ENABLED:
        xf = None
        response = None

    return render_to_response(template, {
        'poll':
        poll,
        'xf':
        xf,
        'response':
        response,
        'categories':
        categories,
        'category_form':
        category_form,
        'rule_form':
        rule_form,
        'category':
        category,
        'groups':
        groups,
        'FEATURE_PREPARE_SEND_POLL':
        getattr(settings, "FEATURE_PREPARE_SEND_POLL", False)
    },
                              context_instance=RequestContext(request))
Esempio n. 4
0
def view_poll(request, pk):
    poll = get_object_or_404(UPoll, pk=pk)
    groups = Group.objects.filter(pk__in=poll.contacts.values_list('groups'))
    category = None
    if request.GET.get('poll'):
        if request.GET.get('start'):
            poll = Poll.objects.get(pk=pk)
            if getattr(settings, 'START_POLL_MULTI_TX', False):
                start_poll_multi_tx(poll)
            else:
                start_poll_single_tx(poll)

            if getattr(settings, "FEATURE_PREPARE_SEND_POLL", False):
                res = """ <a href="?send=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a> """ \
                      % ugettext("Send Poll")
            else:
                res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a> """ \
                      % ugettext("Close Poll")

            return HttpResponse(res)

        if request.GET.get('send'):
            log.info("[send-poll] queuing...")
            poll.queue_message_batches_to_send()
            log.info("[send-poll] done.")
            res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a> """ \
                  % ugettext("Close Poll")
            return HttpResponse(res)

        if request.GET.get('stop'):
            poll.end()
            res = HttpResponse(
                """ <a href="?reopen=True&poll=True" data-remote=true id="poll_action" class="btn">%s</a> """
                % ugettext("Reopen Poll"))
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get("reopen"):
            poll.end_date = None
            poll.save()
            res = HttpResponse(
                """<a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a>  """
                % ugettext("Close Poll"))
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('viewable'):
            poll.viewable = True
            poll.save()
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?unviewable=True&poll=True\')">%s</a>'
                % ugettext("Don\'t Show On Home page"))
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('unviewable'):
            poll.viewable = False
            poll.save()
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?viewable=True&poll=True\')">%s</a>' % ugettext("Show On Home page"))
            res['Cache-Control'] = 'no-store'
            return res
    x = XForm.objects.get(name='poll')
    xf, _ = XFormField.objects.get_or_create(name='latest_poll', xform=x, field_type=XFormField.TYPE_TEXT,
                                             command="poll_%d" % poll.pk)
    response = StubScreen.objects.get_or_create(slug='question_response')
    template = 'ureport/polls/view_poll.html'
    categories = poll.categories.all()
    category_form = CategoryForm()
    rule_form = RuleForm2()
    if request.method == "POST":
        if 'g_name' in request.POST:
            group, __ = Group.objects.get_or_create(name=request.POST.get('g_name')[0])
            cat = Category.objects.get(pk=request.POST.get('category')[0])
            tasks.group_up_category(group, cat, request.user, Poll.objects.get(pk=pk))
            return HttpResponse("success")
        if request.GET.get('edit'):
            if request.POST.get('poll[default_response]'):
                poll.default_response = request.POST['poll[default_response]']
                poll.save()
            if request.POST.get('poll[question]'):
                poll.question = request.POST['poll[question]']
                poll.save()

        if request.GET.get("ussd", None):
            question = request.POST.get("question")
            response = request.POST.get("response")
            xf.question = question
            xf.save()
            response.text = response
            response.save()
        if request.GET.get("category", None):
            if request.GET.get('cat_pk'):
                category = Category.objects.get(pk=int(request.GET.get('cat_pk')))
            else:
                category = Category()
                category.poll = poll
            category_form = CategoryForm(request.POST, instance=category)
            if category_form.is_valid():
                template = "ureport/polls/rules.html"
                category = category_form.save()
                request.session['category'] = category
            else:
                template = "ureport/polls/category.html"

        if request.GET.get("rules", None):
            rule = Rule()
            rule.category = request.session['category']
            rule_form = RuleForm2(request.POST, instance=rule)
            if rule_form.is_valid:
                rule_form.save()
            else:
                template = "ureport/polls/rules.html"

    return render_to_response(template, {
        'poll': poll,
        'xf': xf,
        'response': response,
        'categories': categories,
        'category_form': category_form,
        'rule_form': rule_form,
        'category': category,
        'groups': groups,
        'FEATURE_PREPARE_SEND_POLL': getattr(settings, "FEATURE_PREPARE_SEND_POLL", False)
    }, context_instance=RequestContext(request))
Esempio n. 5
0
def view_poll(request, pk):
    poll = get_object_or_404(UPoll, pk=pk)
    groups = Group.objects.filter(pk__in=poll.contacts.values_list('groups'))
    category = None
    if request.GET.get('poll'):
        if request.GET.get('start'):
            poll = Poll.objects.get(pk=pk)
            if getattr(settings, 'START_POLL_MULTI_TX', False):
                start_poll_multi_tx(poll)
            elif getattr(settings, 'START_POLLS_IN_WEB_THREAD', False): #Starting poll in the web thread might still be good especially when testing (in development)
                log.info("[start-poll-in-web-thread] Starting poll [%s] in web thread..." % str(poll.pk))
                if not poll.start_date:
                    poll.start()
                log.info("[poll-in-web-thread-started] Start Ok.")
            else:
                start_poll_single_tx(poll)

            if getattr(settings, "FEATURE_PREPARE_SEND_POLL", False):
                res = """ <a href="?send=True&poll=True" data-remote=true  id="poll_action" class="btn">Send Poll</a> """
            else:
                res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a> """

            return HttpResponse(res)

        if request.GET.get('send'):
            log.info("[send-poll] queuing...")
            poll.queue_message_batches_to_send()
            log.info("[send-poll] done.")
            res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a> """
            return HttpResponse(res)

        if request.GET.get('stop'):
            poll.end()
            res = HttpResponse(
                """ <a href="?reopen=True&poll=True" data-remote=true id="poll_action" class="btn">Reopen Poll</a> """)
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get("reopen"):
            poll.end_date = None
            poll.save()
            res = HttpResponse(
                """<a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a>  """)
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('viewable'):
            poll.set_attr('viewable', True)
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?unviewable=True&poll=True\')">Don\'t Show On Home page</a>')
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('unviewable'):
            poll.set_attr('viewable', False)
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?viewable=True&poll=True\')">Show On Home page</a>')
            res['Cache-Control'] = 'no-store'
            return res
    if settings.USSD_ENABLED:
        x = XForm.objects.get(name='poll')
        xf, _ = XFormField.objects.get_or_create(name='latest_poll', xform=x, field_type=XFormField.TYPE_TEXT,
                                                 command="poll_%d" % poll.pk)
        response = StubScreen.objects.get_or_create(slug='question_response')
    template = 'ureport/polls/view_poll.html'
    categories = poll.categories.all()
    category_form = CategoryForm()
    rule_form = RuleForm2()
    if request.method == "POST":
        if request.GET.get('edit'):
            if request.POST.get('poll[default_response]'):
                poll.default_response = request.POST['poll[default_response]']
                poll.save()
            if request.POST.get('poll[question]'):
                poll.question = request.POST['poll[question]']
                poll.save()
        if settings.USSD_ENABLED:
            if request.GET.get("ussd", None):
                question = request.POST.get("question")
                response = request.POST.get("response")
                xf.question = question
                xf.save()
                response.text = response
                response.save()
        if request.GET.get("category", None):
            if request.GET.get('cat_pk'):
                category = Category.objects.get(pk=int(request.GET.get('cat_pk')))
            else:
                category = Category()
                category.poll = poll
            category_form = CategoryForm(request.POST, instance=category)
            if category_form.is_valid():
                template = "ureport/polls/rules.html"
                category = category_form.save()
                request.session['category'] = category
            else:
                template = "ureport/polls/category.html"

        if request.GET.get("rules", None):
            rule = Rule()
            rule.category = request.session['category']
            rule_form = RuleForm2(request.POST, instance=rule)
            if rule_form.is_valid:
                rule_form.save()
            else:
                template = "ureport/polls/rules.html"
    
    if not settings.USSD_ENABLED:
        xf = None
        response = None
        
    return render_to_response(template, {
        'poll': poll,
        'xf': xf,
        'response': response,
        'categories': categories,
        'category_form': category_form,
        'rule_form': rule_form,
        'category': category,
        'groups': groups,
        'FEATURE_PREPARE_SEND_POLL' : getattr(settings, "FEATURE_PREPARE_SEND_POLL", False)
    }, context_instance=RequestContext(request))
Esempio n. 6
0
def view_poll(request, pk):
    poll = get_object_or_404(UPoll, pk=pk)
    groups = Group.objects.filter(pk__in=poll.contacts.values_list('groups'))
    category = None
    if request.GET.get('poll'):
        if request.GET.get('start'):
            poll = Poll.objects.get(pk=pk)
            if getattr(settings, 'START_POLL_MULTI_TX', False):
                start_poll_multi_tx(poll)
            else:
                start_poll_single_tx(poll)

            if getattr(settings, "FEATURE_PREPARE_SEND_POLL", False):
                res = """ <a href="?send=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a> """ \
                      % ugettext("Send Poll")
            else:
                res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a> """ \
                      % ugettext("Close Poll")

            return HttpResponse(res)

        if request.GET.get('send'):
            log.info("[send-poll] queuing...")
            poll.queue_message_batches_to_send()
            log.info("[send-poll] done.")
            res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a> """ \
                  % ugettext("Close Poll")
            return HttpResponse(res)

        if request.GET.get('stop'):
            poll.end()
            res = HttpResponse(
                """ <a href="?reopen=True&poll=True" data-remote=true id="poll_action" class="btn">%s</a> """
                % ugettext("Reopen Poll"))
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get("reopen"):
            poll.end_date = None
            poll.save()
            res = HttpResponse(
                """<a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">%s</a>  """
                % ugettext("Close Poll"))
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('viewable'):
            poll.viewable = True
            poll.save()
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?unviewable=True&poll=True\')">%s</a>' %
                ugettext("Don\'t Show On Home page"))
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('unviewable'):
            poll.viewable = False
            poll.save()
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?viewable=True&poll=True\')">%s</a>' %
                ugettext("Show On Home page"))
            res['Cache-Control'] = 'no-store'
            return res
    x = XForm.objects.get(name='poll')
    xf, _ = XFormField.objects.get_or_create(name='latest_poll',
                                             xform=x,
                                             field_type=XFormField.TYPE_TEXT,
                                             command="poll_%d" % poll.pk)
    response = StubScreen.objects.get_or_create(slug='question_response')
    template = 'ureport/polls/view_poll.html'
    categories = poll.categories.all()
    category_form = CategoryForm()
    rule_form = RuleForm2()
    if request.method == "POST":
        if 'g_name' in request.POST:
            group, __ = Group.objects.get_or_create(
                name=request.POST.get('g_name'))
            cat = Category.objects.get(pk=request.POST.get('category'))
            tasks.group_up_category.delay(group, cat, request.user,
                                          Poll.objects.get(pk=pk))
            return HttpResponse("success")
        if request.GET.get('edit'):
            if request.POST.get('poll[default_response]'):
                poll.default_response = request.POST['poll[default_response]']
                poll.save()
            if request.POST.get('poll[question]'):
                poll.question = request.POST['poll[question]']
                poll.save()

        if request.GET.get("ussd", None):
            question = request.POST.get("question")
            response = request.POST.get("response")
            xf.question = question
            xf.save()
            response.text = response
            response.save()
        if request.GET.get("category", None):
            if request.GET.get('cat_pk'):
                category = Category.objects.get(
                    pk=int(request.GET.get('cat_pk')))
            else:
                category = Category()
                category.poll = poll
            category_form = CategoryForm(request.POST, instance=category)
            if category_form.is_valid():
                template = "ureport/polls/rules.html"
                category = category_form.save()
                request.session['category'] = category
            else:
                template = "ureport/polls/category.html"

        if request.GET.get("rules", None):
            rule = Rule()
            rule.category = request.session['category']
            rule_form = RuleForm2(request.POST, instance=rule)
            if rule_form.is_valid:
                rule_form.save()
            else:
                template = "ureport/polls/rules.html"

    return render_to_response(template, {
        'poll':
        poll,
        'xf':
        xf,
        'response':
        response,
        'categories':
        categories,
        'category_form':
        category_form,
        'rule_form':
        rule_form,
        'category':
        category,
        'groups':
        groups,
        'FEATURE_PREPARE_SEND_POLL':
        getattr(settings, "FEATURE_PREPARE_SEND_POLL", False)
    },
                              context_instance=RequestContext(request))
Esempio n. 7
0
def view_poll(request, pk):
    poll = get_object_or_404(UPoll, pk=pk)
    groups = Group.objects.filter(pk__in=poll.contacts.values_list('groups'))
    category = None
    if request.GET.get('poll'):
        if request.GET.get('start'):
            poll=Poll.objects.get(pk=pk)
            start_poll.delay(poll)
            res = """ <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a> """
            return HttpResponse(res)
        if request.GET.get('stop'):
            poll.end()
            res = HttpResponse(
                """ <a href="?reopen=True&poll=True" data-remote=true id="poll_action" class="btn">Reopen Poll</a> """)
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get("reopen"):
            poll.end_date = None
            poll.save()
            res = HttpResponse(
                """<a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a>  """)
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('viewable'):
            poll.set_attr('viewable', 'true')
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?unviewable=True&poll=True\')">Don\'t Show On Home page</a>')
            res['Cache-Control'] = 'no-store'
            return res
        if request.GET.get('unviewable'):
            poll.set_attr('viewable', 'false')
            res = HttpResponse(
                '<a href="javascript:void(0)" id="poll_v" class="btn" onclick="loadViewable'
                '(\'?viewable=True&poll=True\')">Show On Home page</a>')
            res['Cache-Control'] = 'no-store'
            return res
    xf, _ = XFormField.objects.get_or_create(name='latest_poll')
    response = StubScreen.objects.get(slug='question_response')
    template = 'ureport/polls/view_poll.html'
    categories = poll.categories.all()
    category_form = CategoryForm()
    rule_form = RuleForm2()
    if request.method == "POST":
        if request.GET.get('edit'):
            if request.POST.get('poll[default_response]'):
                poll.default_response = request.POST['poll[default_response]']
                poll.save()
            if request.POST.get('poll[question]'):
                poll.question = request.POST['poll[question]']
                poll.save()

        if request.GET.get("ussd", None):
            question = request.POST.get("question")
            response = request.POST.get("response")
            xf.question = question
            xf.save()
            response.text = response
            response.save()
        if request.GET.get("category", None):
            if request.GET.get('cat_pk'):
                category = Category.objects.get(pk=int(request.GET.get('cat_pk')))
            else:
                category = Category()
                category.poll = poll
            category_form = CategoryForm(request.POST, instance=category)
            if category_form.is_valid():
                template = "ureport/polls/rules.html"
                category = category_form.save()
                request.session['category'] = category
            else:
                template = "ureport/polls/category.html"

        if request.GET.get("rules", None):
            rule = Rule()
            rule.category = request.session['category']
            rule_form = RuleForm2(request.POST, instance=rule)
            if rule_form.is_valid:
                rule_form.save()
            else:
                template = "ureport/polls/rules.html"

    try:
        poll_is_viewable = poll.get_attr('viewable')
    except ObjectDoesNotExist:
        poll_is_viewable = poll.set_attr('viewable', 'true')

    return render_to_response(template, {
        'poll': poll,
        'poll_is_viewable': poll_is_viewable,
        'xf': xf,
        'response': response,
        'categories': categories,
        'category_form': category_form,
        'rule_form': rule_form,
        'category': category,
        'groups': groups,
    }, context_instance=RequestContext(request))
Esempio n. 8
0
def view_poll(request,pk):
    poll=Poll.objects.get(pk=pk)
    category=None
    if request.GET.get('poll'):
        if request.GET.get('start'):
            start_poll.delay(poll)
            res=""" <a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a> """
            return HttpResponse(res)
        if request.GET.get('stop'):
            poll.end()
            res=""" <a href="?reopen=True&poll=True" data-remote=true id="poll_action" class="btn">Reopen Poll</a> """
            return HttpResponse(res)
        if request.GET.get("reopen"):
            poll.end_date=None
            poll.save()
            res="""<a href="?stop=True&poll=True" data-remote=true  id="poll_action" class="btn">Close Poll</a>  """
            return HttpResponse(res)
    xf,_=XFormField.objects.get_or_create(name='latest_poll')
    response=StubScreen.objects.get(slug='question_response')
    template='ureport/polls/view_poll.html'
    categories=poll.categories.all()
    category_form=CategoryForm()
    rule_form=RuleForm2()
    if request.method == "POST":
        if request.GET.get('edit'):
            if request.POST.get('poll[default_response]'):
                poll.default_response=request.POST['poll[default_response]']
                poll.save()
            if request.POST.get('poll[question]'):
                poll.question=request.POST['poll[question]']
                poll.save()

        if request.GET.get("ussd",None):
            question=request.POST.get("question")
            response=request.POST.get("response")
            xf.question=question
            xf.save()
            response.text=response
            response.save()
        if request.GET.get("category",None):
            if request.GET.get('cat_pk'):
                category=Category.objects.get(pk=int(request.GET.get('cat_pk')))
            else:
                category=Category()
                category.poll=poll
            category_form=CategoryForm(request.POST,instance=category)
            if category_form.is_valid():
                template="ureport/polls/rules.html"
                category=category_form.save()
                request.session['category'] =category
            else:
                template="ureport/polls/category.html"

        if request.GET.get("rules",None):
            rule=Rule()
            rule.category=request.session['category']
            rule_form=RuleForm2(request.POST,instance=rule)
            if rule_form.is_valid:
                rule_form.save()
            else:
                template="ureport/polls/rules.html"





    return render_to_response(template, {
        'poll': poll,
        'xf':xf,
        'response':response,
        'categories': categories,
        'category_form':category_form,
        'rule_form':rule_form,
        'category':category ,
        }, context_instance=RequestContext(request))