This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added RATINGS_VOTES_PER_IP setting, defaults to 3. All instance looku…
…ps are now pk rather than id. Added some initial unit tests.
- Loading branch information
David Cramer
authored and
David Cramer
committed
Dec 27, 2009
1 parent
492075c
commit 8c06cd1
Showing
8 changed files
with
134 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.conf import settings | ||
|
||
# Used to limit the number of unique IPs that can vote on a single object+field. | ||
# useful if you're getting rating spam by users registering multiple accounts | ||
RATINGS_VOTES_PER_IP = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class InvalidRating(ValueError): pass | ||
class AuthRequired(TypeError): pass | ||
class CannotChangeVote(Exception): pass | ||
class IPLimitReached(Exception): pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from models import Vote | ||
from django.contrib.contenttypes.models import ContentType | ||
from fields import RatingField | ||
|
||
def delete_from_ip_address(ip_address): | ||
qs = Vote.objects.filter(ip_address=ip_address) | ||
|
||
to_update = [] | ||
for content_type, objects in itertools.groupby(qs.distinct().values('content_type_id', 'object_id').order_by('content_type_id'), key=lambda x: x[0]): | ||
ct = ContentType.objects.get_for_model(pk=content_type) | ||
to_update.extend(ct.get_object_for_this_type(pk__in=objects)) | ||
|
||
qs.delete() | ||
|
||
# TODO: this could be improved | ||
for obj in to_update: | ||
for field in obj._meta.fields: | ||
if isinstance(field, RatingField): | ||
getattr(obj, field.name)._update() | ||
obj.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
from django.conf import settings | ||
|
||
from exceptions import * | ||
from fields import AnonymousRatingField, RatingField | ||
|
||
settings.RATINGS_VOTES_PER_IP = 1 | ||
|
||
class RatingTestModel(models.Model): | ||
rating = AnonymousRatingField(range=2, can_change_vote=True) | ||
rating2 = RatingField(range=2, can_change_vote=False) | ||
|
||
class RatingTestCase(unittest.TestCase): | ||
def testRatings(self): | ||
instance = RatingTestModel.objects.create() | ||
|
||
# Test adding votes | ||
instance.rating.add(score=1, user=None, ip_address='127.0.0.1') | ||
self.assertEquals(instance.rating.score, 1) | ||
self.assertEquals(instance.rating.votes, 1) | ||
|
||
# Test adding votes | ||
instance.rating.add(score=2, user=None, ip_address='127.0.0.2') | ||
self.assertEquals(instance.rating.score, 3) | ||
self.assertEquals(instance.rating.votes, 2) | ||
|
||
# Test changing of votes | ||
instance.rating.add(score=2, user=None, ip_address='127.0.0.1') | ||
self.assertEquals(instance.rating.score, 4) | ||
self.assertEquals(instance.rating.votes, 2) | ||
|
||
# Test users | ||
user = User.objects.create(username='django-ratings') | ||
user2 = User.objects.create(username='django-ratings2') | ||
|
||
instance.rating.add(score=2, user=user, ip_address='127.0.0.3') | ||
self.assertEquals(instance.rating.score, 6) | ||
self.assertEquals(instance.rating.votes, 3) | ||
|
||
instance.rating2.add(score=2, user=user, ip_address='127.0.0.3') | ||
self.assertEquals(instance.rating2.score, 2) | ||
self.assertEquals(instance.rating2.votes, 1) | ||
|
||
self.assertRaises(IPLimitReached, instance.rating2.add, score=2, user=user2, ip_address='127.0.0.3') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters