summaryrefslogtreecommitdiffstats
path: root/models.py
blob: da28552e52fda954e9e347b03b3ec7c238bd9261 (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
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings

from kittystore.kittysastore import KittySAStore

from gsoc.utils import log

STORE = KittySAStore(settings.KITTYSTORE_URL)


class Rating(models.Model):
	# @TODO: instead of list_address, user list model from kittystore?
	list_address = models.CharField(max_length=50)

	# @TODO: instead of messsageid, use message model from kittystore?
	messageid = models.CharField(max_length=100)

	user = models.ForeignKey(User)

	vote = models.SmallIntegerField()

	def __unicode__(self):
		"""Unicode representation"""
		if self.vote == 1:
	  		return u'id = %s : %s voted up %s' % (self.id, unicode(self.user), self.messageid)
		else:
			return u'id = %s : %s voted down %s' % (self.id, unicode(self.user), self.messageid)


class UserProfile(models.Model):
	# User Object
	user = models.OneToOneField(User)

	karma = models.IntegerField(default=1)

	def _get_votes(self):
   	    "Returns all the votes by a user"
            # Extract all the votes by this user
     	    try:
	    	votes = Rating.objects.filter(user = self.user)
            except Rating.DoesNotExist:
		votes = {}

	    for vote in votes:
		list_name = vote.list_address.split('@')[0]
	        message = STORE.get_email(list_name, vote.messageid)
		vote.message = message

     	    return votes

	votes = property(_get_votes)

	def __unicode__(self):
		"""Unicode representation"""
		return u'%s' % (unicode(self.user))