Skip to content
This repository was archived by the owner on Jul 17, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions djangoratings/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
def md5_hexdigest(value):
return md5(value).hexdigest()

def gen_cookie_name(c_type_id, obj_id, key):
formatted = '%d.%d.%s' % (c_type_id, obj_id, key)
return 'drv%s' % formatted if settings.DEBUG else md5_hexdigest(formatted)

def gen_cookie_value():
value = now().strftime('%Y%m%d%H%M%S%f')
return value if settings.DEBUG else md5_hexdigest(value)

class Rating(object):
def __init__(self, score, votes):
self.score = score
Expand Down Expand Up @@ -98,17 +106,18 @@ def get_rating_for_user(self, user, ip_address=None, cookies={}):
)

if not (user and user.is_authenticated()):
is_anonymous = True
if not ip_address:
raise ValueError('``user`` or ``ip_address`` must be present.')
kwargs['user__isnull'] = True
kwargs['ip_address'] = ip_address
else:
is_anonymous = False
kwargs['user'] = user

use_cookies = (self.field.allow_anonymous and self.field.use_cookies)
use_cookies = (self.field.allow_anonymous and is_anonymous and self.field.use_cookies)
if use_cookies:
# TODO: move 'vote-%d.%d.%s' to settings or something
cookie_name = 'vote-%d.%d.%s' % (kwargs['content_type'].pk, kwargs['object_id'], kwargs['key'][:6],) # -> md5_hexdigest?
cookie_name = gen_cookie_name(kwargs['content_type'].pk, kwargs['object_id'], kwargs['key'][:6])
cookie = cookies.get(cookie_name)
if cookie:
kwargs['cookie'] = cookie
Expand Down Expand Up @@ -165,11 +174,10 @@ def add(self, score, user, ip_address, cookies={}, commit=True):
if not user:
kwargs['ip_address'] = ip_address

use_cookies = (self.field.allow_anonymous and self.field.use_cookies)
use_cookies = (self.field.allow_anonymous and is_anonymous and self.field.use_cookies)
if use_cookies:
defaults['cookie'] = now().strftime('%Y%m%d%H%M%S%f') # -> md5_hexdigest?
# TODO: move 'vote-%d.%d.%s' to settings or something
cookie_name = 'vote-%d.%d.%s' % (kwargs['content_type'].pk, kwargs['object_id'], kwargs['key'][:6],) # -> md5_hexdigest?
defaults['cookie'] = gen_cookie_value()
cookie_name = gen_cookie_name(kwargs['content_type'].pk, kwargs['object_id'], kwargs['key'][:6])
cookie = cookies.get(cookie_name) # try to get existent cookie value
if not cookie:
kwargs['cookie__isnull'] = True
Expand Down
12 changes: 6 additions & 6 deletions djangoratings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User

from django.conf import settings
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
try:
from django.utils.timezone import now
except ImportError:
Expand All @@ -17,7 +17,7 @@ class Vote(models.Model):
object_id = models.PositiveIntegerField()
key = models.CharField(max_length=32)
score = models.IntegerField()
user = models.ForeignKey(User, blank=True, null=True, related_name="votes")
user = models.ForeignKey(AUTH_USER_MODEL, blank=True, null=True, related_name="votes")
ip_address = models.IPAddressField()
cookie = models.CharField(max_length=32, blank=True, null=True)
date_added = models.DateTimeField(default=now, editable=False)
Expand Down Expand Up @@ -65,8 +65,8 @@ def __unicode__(self):
return u"%s scored %s with %s votes" % (self.content_object, self.score, self.votes)

class SimilarUser(models.Model):
from_user = models.ForeignKey(User, related_name="similar_users")
to_user = models.ForeignKey(User, related_name="similar_users_from")
from_user = models.ForeignKey(AUTH_USER_MODEL, related_name="similar_users")
to_user = models.ForeignKey(AUTH_USER_MODEL, related_name="similar_users_from")
agrees = models.PositiveIntegerField(default=0)
disagrees = models.PositiveIntegerField(default=0)
exclude = models.BooleanField(default=False)
Expand All @@ -80,7 +80,7 @@ def __unicode__(self):
print u"%s %s similar to %s" % (self.from_user, self.exclude and 'is not' or 'is', self.to_user)

class IgnoredObject(models.Model):
user = models.ForeignKey(User)
user = models.ForeignKey(AUTH_USER_MODEL)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()

Expand Down