summaryrefslogtreecommitdiffstats
path: root/hyperkitty/views/thread.py
blob: bef9c406687524d9306054eae98a0771f6845e5d (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import datetime

import django.utils.simplejson as simplejson

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 kittystore import get_store

from hyperkitty.models import Rating, Tag
#from hyperkitty.lib.mockup import *
from forms import *
from hyperkitty.utils import log
from hyperkitty.lib import get_months



def thread_index (request, mlist_fqdn, threadid):
    ''' Displays all the email for a given thread identifier '''
    list_name = mlist_fqdn.split('@')[0]

    search_form = SearchForm(auto_id=False)
    t = loader.get_template('thread.html')
    store = get_store(settings.KITTYSTORE_URL)
    messages = store.get_messages_in_thread(mlist_fqdn, threadid)
    #prev_thread = mongo.get_thread_name(list_name, int(threadid) - 1)
    prev_thread = []
    if len(prev_thread) > 30:
        prev_thread = '%s...' % prev_thread[:31]
    #next_thread = mongo.get_thread_name(list_name, int(threadid) + 1)
    next_thread = []
    if len(next_thread) > 30:
        next_thread = '%s...' % next_thread[:31]

    participants = {}
    cnt = 0

    for message in messages:
        # @TODO: Move this logic inside KittyStore?
        message.sender_email = message.sender_email.strip()

        # Extract all the votes for this message
        try:
            votes = Rating.objects.filter(messageid=message.message_id)
        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

        # Statistics on how many participants and messages this month
        participants[message.sender_name] = {'email': message.sender_email}
        cnt = cnt + 1

    archives_length = get_months(store, mlist_fqdn)
    from_url = '/thread/%s/%s/' % (mlist_fqdn, threadid)
    tag_form = AddTagForm(initial={'from_url' : from_url})

    try:
        tags = Tag.objects.filter(threadid=threadid)
    except Tag.DoesNotExist:
        tags = {}

    # Extract relative dates
    today = datetime.date.today()
    days_old = today - messages[0].date.date()
    days_inactive = today - messages[-1].date.date()

    c = RequestContext(request, {
        'list_name' : list_name,
        'threadid' : threadid,
        'tags' : tags,
        'list_address': mlist_fqdn,
        'search_form': search_form,
        'addtag_form': tag_form,
        'month': 'Thread',
        'participants': participants,
        'answers': cnt,
        'first_mail': messages[0],
        'replies': messages[1:],
        'next_thread': next_thread,
        'next_thread_id': 0,
        'prev_thread': prev_thread,
        'prev_thread_id': 0,
        'archives_length': archives_length,
        'days_inactive': days_inactive.days,
        'days_old': days_old.days,
    })
    return HttpResponse(t.render(c))


@login_required
def add_tag(request, mlist_fqdn, email_id):
    """ Add a tag to a given thread. """

    if request.method == 'POST':
        form = AddTagForm(request.POST)
        if form.is_valid():
            print "Adding tag..."

            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')