Browse files

Rewrite mark_accepted view to provide richer ajax

  • Loading branch information...
1 parent 6483d66 commit ba1a9e9e70a21e27ac4aa94e49a69742f2b77e38 @paltman paltman committed Feb 4, 2013
Showing with 52 additions and 13 deletions.
  1. +1 −0 MANIFEST.in
  2. +19 −0 aiteo/templates/aiteo/_accepted.html
  3. +9 −1 aiteo/templatetags/aiteo_tags.py
  4. +1 −1 aiteo/urls.py
  5. +22 −11 aiteo/views.py
View
1 MANIFEST.in
@@ -1,4 +1,5 @@
include README.rst
+recursive-include aiteo/templates *
recursive-include aiteo/fixtures *
recursive-include media *
View
19 aiteo/templates/aiteo/_accepted.html
@@ -0,0 +1,19 @@
+{% load url from future %}
+{% load aiteo_tags %}
+{% if request.user|can_accept:response %}
+ <div class="accepted" id="accepted-{{ response.pk }}">
+ {% if response.accepted %}
+ <i class="icon-ok"></i>
+ {% else %}
+ <a href="{% url 'aiteo_mark_accepted' response.pk %}" class="ajax" data-method="post">
+ <i class="icon-ok"></i>
+ </a>
+ {% endif %}
+ </div>
+{% else %}
+ {% if response.accepted %}
+ <div class="accepted" id="accepted-{{ response.pk }}">
+ <i class="icon-ok"></i>
+ </div>
+ {% endif %}
+{% endif %}
View
10 aiteo/templatetags/aiteo_tags.py
@@ -1,6 +1,9 @@
from django import template
+from django.conf import settings
+from django.utils.importlib import import_module
+workflow = import_module(getattr(settings, "AITEO_WORKFLOW_MODULE", "aiteo.workflow"))
register = template.Library()
@@ -17,4 +20,9 @@ def get_state(original_type, vote_obj):
return "clear"
if original_type == "down" and vote_obj and vote_obj.is_upvote():
return "clear"
- return original_type
+ return original_type
+
+
+@register.filter
+def can_accept(user, response):
+ return workflow.can_mark_accepted(user, response.question)
View
2 aiteo/urls.py
@@ -18,7 +18,7 @@
"aiteo.views.question_detail",
name="aiteo_question_detail"
),
- url(r"^question/(?P<question_id>\d+)/accept/(?P<response_id>\d+)/$",
+ url(r"^responses/(?P<response_id>\d+)/accept/$",
"aiteo.views.mark_accepted",
name="aiteo_mark_accepted"
),
View
33 aiteo/views.py
@@ -1,12 +1,16 @@
+import json
+
from django.conf import settings
-from django.http import Http404, HttpResponse, HttpResponseRedirect
-from django.http import HttpResponseNotAllowed, HttpResponseForbidden
+from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.shortcuts import get_object_or_404, render
+from django.template import RequestContext
+from django.template.loader import render_to_string
from django.utils.importlib import import_module
+from django.views.decorators.http import require_POST
from account.decorators import login_required
from aiteo.forms import AskQuestionForm, AddResponseForm
-from aiteo.models import Question
+from aiteo.models import Question, Response
workflow = import_module(getattr(settings, "AITEO_WORKFLOW_MODULE", "aiteo.workflow"))
@@ -64,13 +68,20 @@ def question_detail(request, question_id, **kwargs):
return render(request, "aiteo/question_detail.html", ctx)
-def mark_accepted(request, question_id, response_id, **kwargs):
- if request.method != "POST":
- return HttpResponseNotAllowed(["POST"])
- questions = Question.objects.all()
- question = get_object_or_404(questions, pk=question_id)
- if not workflow.can_mark_accepted(request.user, question):
+@login_required
+@require_POST
+def mark_accepted(request, response_id):
+ response = get_object_or_404(Response, pk=response_id)
+ if not workflow.can_mark_accepted(request.user, response.question):
return HttpResponseForbidden("You are not allowed to mark this question accepted.")
- response = question.responses.get(pk=response_id)
+
response.accept()
- return HttpResponse("good")
+
+ data = {"fragments": {}}
+ for resp in response.question.responses.all():
+ data["fragments"]["#accepted-{}".format(resp.pk)] = render_to_string(
+ "aiteo/_accepted.html",
+ {"response": resp},
+ context_instance=RequestContext(request)
+ )
+ return HttpResponse(json.dumps(data), mimetype="application/json")

0 comments on commit ba1a9e9

Please sign in to comment.