diff options
author | Aurélien Bompard <aurelien@bompard.org> | 2012-12-10 12:25:38 +0100 |
---|---|---|
committer | Aurélien Bompard <aurelien@bompard.org> | 2012-12-10 12:25:38 +0100 |
commit | 6ea042ab3c6310bb87cb01041656e6928a163a5f (patch) | |
tree | a191c3d7dbd9de77f7b24c69b31bbb9af0eadbf1 | |
parent | 1efaf31a907aa42fc0b01029d332f539b2067a75 (diff) | |
download | hyperkitty-6ea042ab3c6310bb87cb01041656e6928a163a5f.tar.gz hyperkitty-6ea042ab3c6310bb87cb01041656e6928a163a5f.tar.xz hyperkitty-6ea042ab3c6310bb87cb01041656e6928a163a5f.zip |
Don't reload the page when adding tags or liking stuff
-rw-r--r-- | hyperkitty/static/js/hyperkitty.js | 20 | ||||
-rw-r--r-- | hyperkitty/templates/messages/message.html | 6 | ||||
-rw-r--r-- | hyperkitty/templates/month_view.html | 8 | ||||
-rw-r--r-- | hyperkitty/templates/threads/add_tag_form.html | 2 | ||||
-rw-r--r-- | hyperkitty/templates/threads/right_col.html | 13 | ||||
-rw-r--r-- | hyperkitty/templates/threads/tags.html | 13 | ||||
-rw-r--r-- | hyperkitty/tests/test_views.py | 4 | ||||
-rw-r--r-- | hyperkitty/urls.py | 2 | ||||
-rw-r--r-- | hyperkitty/views/message.py | 29 | ||||
-rw-r--r-- | hyperkitty/views/thread.py | 46 |
10 files changed, 85 insertions, 58 deletions
diff --git a/hyperkitty/static/js/hyperkitty.js b/hyperkitty/static/js/hyperkitty.js index 238954d..2b6a818 100644 --- a/hyperkitty/static/js/hyperkitty.js +++ b/hyperkitty/static/js/hyperkitty.js @@ -36,13 +36,15 @@ function vote(elem, value) { $.ajax({ type: "POST", url: $(elem).parent("form").attr("action"), + dataType: "json", data: data, success: function(response) { - // @TODO : Remove this reload and update the count using the AJAX response - location.reload(); + var likestatus = $(elem).parent("form").find(".likestatus"); + likestatus.find(".likecount").html(response.like); + likestatus.find(".dislikecount").html(response.dislike); }, error: function(jqXHR, textStatus, errorThrown) { - // You must be authenticated to do that + // authentication or double-vote if (jqXHR.status === 403) { alert(jqXHR.responseText); } @@ -65,17 +67,15 @@ function setup_add_tag() { $("#add_tag_form").submit( function () { $.ajax({ type: "POST", + dataType: "json", data : $(this).serialize(), url: $(this).attr("action"), - success: function(data){ - // @TODO : Remove this reload and update the tag list using the AJAX response - //location.reload(); + success: function(data) { + $("#tags").html(data.html); }, error: function(jqXHR, textStatus, errorThrown) { - // You must be authenticated to do that - if (jqXHR.status === 403) { - alert(jqXHR.responseText); - } + // authentication and invalid data + alert(jqXHR.responseText); } }); return false; diff --git a/hyperkitty/templates/messages/message.html b/hyperkitty/templates/messages/message.html index c69c2ff..7281f7d 100644 --- a/hyperkitty/templates/messages/message.html +++ b/hyperkitty/templates/messages/message.html @@ -46,10 +46,10 @@ <ul class="email_info inline"> {% if use_mockups %} <form method="post" action="{% url message_vote mlist_fqdn=list_address %}"> - <input type="hidden" name="messageid" value="{{email.message_id_hash}}" /> + <input type="hidden" name="hashid" value="{{ email.message_id_hash }}" /> {% csrf_token %} - <li class="likestatus {{email.likestatus}}"> - +{{email.likes}}/-{{email.dislikes}} + <li class="likestatus {{ email.likestatus }}"> + +<span class="likecount">{{ email.likes }}</span>/-<span class="dislikecount">{{ email.dislikes }}</span> </li> <li class="voteup"> <a class="youlike" href="#like">Like</a> diff --git a/hyperkitty/templates/month_view.html b/hyperkitty/templates/month_view.html index 511866c..f08a9f2 100644 --- a/hyperkitty/templates/month_view.html +++ b/hyperkitty/templates/month_view.html @@ -80,9 +80,12 @@ </li> </ul> {% if use_mockups %} - <ul class="inline-block" messageid="{{ thread.thread_id }}"> + <ul class="inline-block"> + <form method="post" action="{% url message_vote mlist_fqdn=list_address %}"> + <input type="hidden" name="hashid" value="{{ thread.thread_id }}" /> + {% csrf_token %} <li class="likestatus {{ thread.likestatus }}"> - +{{ thread.avglike }}/-{{ thread.avgdislike }} + +<span class="like">{{ thread.avglike }}</span>/-<span class="dislike">{{ thread.avgdislike }}</span> </li> <li class="voteup"> <a class="youlike" href="#like">Like</a> @@ -90,6 +93,7 @@ <li class="votedown"> <a class="youdislike" href="#dislike">Dislike</a> </li> + </form> </ul> <a href="{% url thread threadid=thread.thread_id, mlist_fqdn=list_address %}" class="btn">Show discussion</a> diff --git a/hyperkitty/templates/threads/add_tag_form.html b/hyperkitty/templates/threads/add_tag_form.html index f719152..2d7f02f 100644 --- a/hyperkitty/templates/threads/add_tag_form.html +++ b/hyperkitty/templates/threads/add_tag_form.html @@ -3,7 +3,7 @@ {% block header %} {% endblock %} {% block content %} -<form id="addtag" action="{% url add_tag mlist_fqdn=list_address, email_id=threadid %}" method="post"> +<form id="addtag" action="{% url add_tag mlist_fqdn=list_address, hashid=threadid %}" method="post"> {% csrf_token %} {{ addtag_form }} <button type="submit"> diff --git a/hyperkitty/templates/threads/right_col.html b/hyperkitty/templates/threads/right_col.html index cc486b1..33ed790 100644 --- a/hyperkitty/templates/threads/right_col.html +++ b/hyperkitty/templates/threads/right_col.html @@ -30,20 +30,11 @@ <!-- End dates --> <hr id="grey"/> <div id="tags"> - <span id="tag_title">tags </span>({{tags|length}}) - {% if tags|length %} - <ul class="inline"> - {% for tag in tags %} - <li> - <a href="{% url search_tag mlist_fqdn=list_address, tag=tag.tag %}" > {{ tag.tag }} </a>| - </li> - {% endfor %} - </ul> - {% endif %} + {% include 'threads/tags.html' %} </div> <div id="add_tag"> <form id="add_tag_form" name="addtag" method="post" - action="{% url add_tag mlist_fqdn=list_address, email_id=threadid %}"> + action="{% url add_tag mlist_fqdn=list_address, hashid=threadid %}"> {% csrf_token %} {{ addtag_form.as_p }} </form> diff --git a/hyperkitty/templates/threads/tags.html b/hyperkitty/templates/threads/tags.html new file mode 100644 index 0000000..ce162ed --- /dev/null +++ b/hyperkitty/templates/threads/tags.html @@ -0,0 +1,13 @@ + <span id="tag_title">tags </span>({{tags|length}}) + {% if tags|length %} + <ul class="inline"> + {% for tag in tags %} + <li> + <a href="{% url search_tag mlist_fqdn=list_address, tag=tag.tag %}" >{{ tag.tag }}</a> + {% if not forloop.last %} | {% endif %} + </li> + {% endfor %} + </ul> + {% endif %} + +{# vim: set noet: #} diff --git a/hyperkitty/tests/test_views.py b/hyperkitty/tests/test_views.py index dd85258..8141b5b 100644 --- a/hyperkitty/tests/test_views.py +++ b/hyperkitty/tests/test_views.py @@ -79,6 +79,4 @@ class MessageViewsTestCase(TestCase): def test_unauth_vote(self): resp = self.client.post(reverse('message_vote', kwargs={'mlist_fqdn': 'list@list.com'}), {'vote': 1, 'hashid': 123, }) - url = "%s?next=%s" % (reverse('user_login'), urllib.quote(reverse('message_vote', kwargs={'mlist_fqdn': 'list@list.com'}))) - self.assertRedirects(resp, url) - + self.assertEqual(resp.status_code, 403) diff --git a/hyperkitty/urls.py b/hyperkitty/urls.py index 3340971..5969b61 100644 --- a/hyperkitty/urls.py +++ b/hyperkitty/urls.py @@ -75,7 +75,7 @@ urlpatterns = patterns('hyperkitty.views', 'thread.thread_index', name='thread_index'), # Add Tag to a thread - url(r'^addtag/(?P<mlist_fqdn>.*@.*)\/(?P<email_id>.*)/$', + url(r'^addtag/(?P<mlist_fqdn>.*@.*)\/(?P<hashid>.*)/$', 'thread.add_tag', name='add_tag'), ### THREAD LEVEL VIEW ENDS ### diff --git a/hyperkitty/views/message.py b/hyperkitty/views/message.py index 5ed17c2..8f5679c 100644 --- a/hyperkitty/views/message.py +++ b/hyperkitty/views/message.py @@ -101,7 +101,8 @@ def attachment(request, mlist_fqdn, hashid, counter, filename): message = store.get_message_by_hash_from_list(mlist_fqdn, hashid) if message is None: raise Http404 - attachment = store.get_attachment_by_counter(mlist_fqdn, message.message_id, int(counter)) + attachment = store.get_attachment_by_counter( + mlist_fqdn, message.message_id, int(counter)) if attachment is None or attachment.name != filename: raise Http404 # http://djangosnippets.org/snippets/1710/ @@ -122,18 +123,30 @@ def vote(request, mlist_fqdn): return HttpResponse('You must be logged in to vote', content_type="text/plain", status=403) - value = request.POST['vote'] + value = int(request.POST['vote']) hashid = request.POST['hashid'] - # Checks if the user has already voted for a this message. If yes modify db entry else create a new one. + # Checks if the user has already voted for a this message. try: - v = Rating.objects.get(user = request.user, messageid = hashid, list_address = mlist_fqdn) + v = Rating.objects.get(user=request.user, messageid=hashid, + list_address=mlist_fqdn) + if v.vote == value: + return HttpResponse("You've already cast this vote", + content_type="text/plain", status=403) except Rating.DoesNotExist: - v = Rating(list_address=mlist_fqdn, messageid = hashid, vote = value) + v = Rating(list_address=mlist_fqdn, messageid=hashid, vote=value) + v.user = request.user - v.user = request.user v.vote = value v.save() - response_dict = { } - return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript') + # Extract all the votes for this message to refresh it + status = { "like": 0, "dislike": 0 } + for vote in Rating.objects.filter(messageid=hashid): + if vote.vote == 1: + status["like"] += 1 + elif vote.vote == -1: + status["dislike"] += 1 + + return HttpResponse(simplejson.dumps(status), + mimetype='application/javascript') diff --git a/hyperkitty/views/thread.py b/hyperkitty/views/thread.py index ec5a60f..1375d9f 100644 --- a/hyperkitty/views/thread.py +++ b/hyperkitty/views/thread.py @@ -90,7 +90,7 @@ def thread_index(request, mlist_fqdn, threadid): tag_form = AddTagForm(initial={'from_url' : from_url}) try: - tags = Tag.objects.filter(threadid=threadid) + tags = Tag.objects.filter(threadid=threadid, list_address=mlist_fqdn) except Tag.DoesNotExist: tags = {} @@ -124,28 +124,36 @@ def thread_index(request, mlist_fqdn, threadid): return HttpResponse(t.render(c)) -def add_tag(request, mlist_fqdn, email_id): +def add_tag(request, mlist_fqdn, hashid): """ Add a tag to a given thread. """ if not request.user.is_authenticated(): return HttpResponse('You must be logged in to add a tag', content_type="text/plain", status=403) - if request.method == 'POST': - form = AddTagForm(request.POST) - if form.is_valid(): - print "Adding tag..." + if request.method != 'POST': + return HttpResponse("Something went wrong here", + content_type="text/plain", status=500) - tag = form.data['tag'] - - try: - tag_obj = Tag.objects.get(threadid=email_id, list_address=mlist_fqdn, tag=tag) - except Tag.DoesNotExist: - tag_obj = Tag(list_address=mlist_fqdn, threadid=email_id, tag=tag) - - tag_obj.save() - response_dict = { } - else: - response_dict = {'error' : 'Error adding tag, enter valid data' } - - return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript') + form = AddTagForm(request.POST) + if not form.is_valid(): + return HttpResponse("Error adding tag: invalid data", + content_type="text/plain", status=500) + tag = form.data['tag'] + try: + tag_obj = Tag.objects.get(threadid=hashid, + list_address=mlist_fqdn, tag=tag) + except Tag.DoesNotExist: + tag_obj = Tag(list_address=mlist_fqdn, threadid=hashid, tag=tag) + tag_obj.save() + + # Now refresh the tag list + tags = Tag.objects.filter(threadid=hashid, list_address=mlist_fqdn) + t = loader.get_template('threads/tags.html') + html = t.render(RequestContext(request, { + "tags": tags, + "list_address": mlist_fqdn})) + + response = {"tags": [ t.tag for t in tags ], "html": html} + return HttpResponse(simplejson.dumps(response), + mimetype='application/javascript') |