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 /hyperkitty/views | |
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
Diffstat (limited to 'hyperkitty/views')
-rw-r--r-- | hyperkitty/views/message.py | 29 | ||||
-rw-r--r-- | hyperkitty/views/thread.py | 46 |
2 files changed, 48 insertions, 27 deletions
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') |