Esempio n. 1
0
def add_rule(req, poll_id, category_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    if poll.type != Poll.TYPE_TEXT:
        return HttpResponse(status=404)
    category = get_object_or_404(Category, pk=category_id)
    form = RuleForm()

    if req.method == "POST":
        form = RuleForm(req.POST)
        if form.is_valid():
            rule = form.save(commit=False)
            rule.category = category
            rule.update_regex()
            rule.save()
            poll.reprocess_responses()
            return render_to_response(
                "polls/rule_view.html",
                {"rule": rule, "form": form, "poll": poll, "category": category},
                context_instance=RequestContext(req),
            )
    else:
        form = RuleForm()

    return render_to_response(
        "polls/rule_edit.html", {"form": form, "poll": poll, "category": category}, context_instance=RequestContext(req)
    )
Esempio n. 2
0
def edit_rule(req, poll_id, category_id, rule_id):

    poll = get_object_or_404(Poll, pk=poll_id)
    category = get_object_or_404(Category, pk=category_id)
    rule = get_object_or_404(Rule, pk=rule_id)

    if req.method == "POST":
        form = RuleForm(req.POST, instance=rule)
        if form.is_valid():
            rule = form.save(commit=False)
            rule.update_regex()
            rule.save()
            poll.reprocess_responses()
            return render_to_response(
                "polls/rule_view.html",
                {"rule": rule, "poll": poll, "category": category},
                context_instance=RequestContext(req),
            )
        else:
            return render_to_response(
                "polls/rule_edit.html",
                {"rule": rule, "form": form, "poll": poll, "category": category},
                context_instance=RequestContext(req),
            )
    else:
        form = RuleForm(instance=rule)

    return render_to_response(
        "polls/rule_edit.html",
        {"form": form, "poll": poll, "category": category, "rule": rule},
        context_instance=RequestContext(req),
    )