Пример #1
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))
Пример #2
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))