summaryrefslogtreecommitdiffstats
path: root/views/message.py
blob: 77466a70c1507ae7c03462eec26ec8f3326f253f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import django.utils.simplejson as simplejson

from django import forms
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger, InvalidPage
from django.contrib.auth.decorators import (login_required,
                                            permission_required,
                                            user_passes_test)
from gsoc.models import Rating
from pages import SearchForm, STORE

logger = logging.getLogger(__name__)


def index (request, mlist_fqdn, messageid):
    ''' Displays a single message identified by its messageid '''
    list_name = mlist_fqdn.split('@')[0]

    search_form = SearchForm(auto_id=False)
    t = loader.get_template('message.html')
    message = STORE.get_email(list_name, messageid)
    message.email = message.email.strip()
    # Extract all the votes for this message
    try:
	votes = Rating.objects.filter(messageid = messageid)
    except Rating.DoesNotExist:
	votes = {}

    likes = 0
    dislikes = 0

    for vote in votes:
	if vote.vote == 1:
		likes = likes + 1
	elif vote.vote == -1:
		dislikes = dislikes + 1
	else:
		pass
	
    message.votes = votes
    message.likes = likes
    message.dislikes = dislikes

    c = RequestContext(request, {
        'list_name' : list_name,
        'list_address': mlist_fqdn,
        'message': message,
	'messageid' : messageid,
    })
    return HttpResponse(t.render(c))



@login_required
def vote (request, mlist_fqdn):
    """ Add a rating to a given message identified by messageid. """
    if not request.user.is_authenticated():
	return redirect('user_login')

    value = request.POST['vote']
    messageid = request.POST['messageid']

    # Checks if the user has already voted for a this message. If yes modify db entry else create a new one.
    try:
	v = Rating.objects.get(user = request.user, messageid = messageid, list_address = mlist_fqdn)
    except Rating.DoesNotExist:
    	v = Rating(list_address=mlist_fqdn, messageid = messageid, vote = value) 

    v.user = request.user
    v.vote = value  
    v.save()
    response_dict = { }

    return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')