Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 75fd9ed
Showing
28 changed files
with
946 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2011, Eldarion, Inc. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of Eldarion, Inc. nor the names of its contributors may | ||
be used to endorse or promote products derived from this software without | ||
specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,2 @@ | ||
include README.rst | ||
recursive-include agon_ratings/templates *.html |
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,11 @@ | ||
agon-ratings | ||
============ | ||
|
||
Provides user ratings of objects. | ||
|
||
|
||
Documentation | ||
------------- | ||
|
||
Documentation can be found online at http://agon-ratings.readthedocs.org/. | ||
|
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 @@ | ||
__version__ = "0.1.dev8" |
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,16 @@ | ||
from django.db import models | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
|
||
|
||
class OverallRatingManager(models.Manager): | ||
|
||
def top_rated(self, klass): | ||
|
||
return self.filter( | ||
content_type=ContentType.objects.get_for_model(klass) | ||
).extra( | ||
select={ | ||
"sortable_rating": "COALESCE(rating, 0)" | ||
} | ||
).order_by("-sortable_rating") |
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,51 @@ | ||
import datetime | ||
|
||
from decimal import Decimal | ||
|
||
from django.db import models | ||
|
||
from django.contrib.auth.models import User | ||
from django.contrib.contenttypes.generic import GenericForeignKey | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from agon_ratings.managers import OverallRatingManager | ||
|
||
|
||
class OverallRating(models.Model): | ||
|
||
object_id = models.IntegerField(db_index=True) | ||
content_type = models.ForeignKey(ContentType) | ||
content_object = GenericForeignKey() | ||
rating = models.DecimalField(decimal_places=1, max_digits=3, null=True) | ||
|
||
objects = OverallRatingManager() | ||
|
||
class Meta: | ||
unique_together = [ | ||
("object_id", "content_type"), | ||
] | ||
|
||
def update(self): | ||
self.rating = Rating.objects.filter( | ||
overall_rating = self | ||
).aggregate(r = models.Avg("rating"))["r"] | ||
self.rating = Decimal(str(self.rating or "0")) | ||
self.save() | ||
|
||
|
||
class Rating(models.Model): | ||
overall_rating = models.ForeignKey(OverallRating, null = True, related_name = "ratings") | ||
object_id = models.IntegerField(db_index=True) | ||
content_type = models.ForeignKey(ContentType) | ||
content_object = GenericForeignKey() | ||
user = models.ForeignKey(User) | ||
rating = models.IntegerField() | ||
timestamp = models.DateTimeField(default=datetime.datetime.now) | ||
|
||
class Meta: | ||
unique_together = [ | ||
("object_id", "content_type", "user"), | ||
] | ||
|
||
def __unicode__(self): | ||
return unicode(self.rating) |
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,4 @@ | ||
<form action="{% url agon_ratings_rate content_type_id=ct.id object_id=obj.pk %}" method="POST"> | ||
{% csrf_token %} | ||
<input type="hidden" name="rating" id="id_rating" /> | ||
</form> |
Empty file.
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,96 @@ | ||
from django import template | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from agon_ratings.models import Rating, OverallRating | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
class UserRatingNode(template.Node): | ||
|
||
@classmethod | ||
def handle_token(cls, parser, token): | ||
bits = token.split_contents() | ||
if len(bits) != 7: | ||
raise template.TemplateSyntaxError() | ||
return cls( | ||
user = parser.compile_filter(bits[2]), | ||
obj = parser.compile_filter(bits[4]), | ||
as_var = bits[6] | ||
) | ||
|
||
def __init__(self, user, obj, as_var): | ||
self.user = user | ||
self.obj = obj | ||
self.as_var = as_var | ||
|
||
def render(self, context): | ||
user = self.user.resolve(context) | ||
obj = self.obj.resolve(context) | ||
try: | ||
ct = ContentType.objects.get_for_model(obj) | ||
rating = Rating.objects.get( | ||
object_id = obj.pk, | ||
content_type = ct, | ||
user = user | ||
).rating | ||
except Rating.DoesNotExist: | ||
rating = 0 | ||
context[self.as_var] = rating | ||
return "" | ||
|
||
|
||
@register.tag | ||
def user_rating(parser, token): | ||
""" | ||
Usage: | ||
{% user_rating for user and obj as var %} | ||
""" | ||
return UserRatingNode.handle_token(parser, token) | ||
|
||
|
||
class OverallRatingNode(template.Node): | ||
|
||
@classmethod | ||
def handle_token(cls, parser, token): | ||
bits = token.split_contents() | ||
if len(bits) != 5: | ||
raise template.TemplateSyntaxError() | ||
return cls( | ||
obj = parser.compile_filter(bits[2]), | ||
as_var = bits[4] | ||
) | ||
|
||
def __init__(self, obj, as_var): | ||
self.obj = obj | ||
self.as_var = as_var | ||
|
||
def render(self, context): | ||
obj = self.obj.resolve(context) | ||
try: | ||
ct = ContentType.objects.get_for_model(obj) | ||
rating = OverallRating.objects.get( | ||
object_id=obj.pk, | ||
content_type=ct | ||
).rating or 0 | ||
except OverallRating.DoesNotExist: | ||
rating = 0 | ||
context[self.as_var] = rating | ||
return "" | ||
|
||
|
||
@register.tag | ||
def overall_rating(parser, token): | ||
""" | ||
Usage: | ||
{% overall_rating for obj as var %} | ||
""" | ||
return OverallRatingNode.handle_token(parser, token) | ||
|
||
|
||
@register.inclusion_tag("agon_ratings/_rate_form.html") | ||
def user_rate_form(obj): | ||
ct = ContentType.objects.get_for_model(obj) | ||
return {"ct": ct, "obj": obj} |
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,6 @@ | ||
from django.conf.urls.defaults import * | ||
|
||
|
||
urlpatterns = patterns("agon_ratings.views", | ||
url(r"^(?P<content_type_id>\d+)/(?P<object_id>\d+)/rate/$", "rate", name="agon_ratings_rate"), | ||
) |
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,66 @@ | ||
from django.conf import settings | ||
from django.http import HttpResponse, HttpResponseForbidden | ||
from django.shortcuts import get_object_or_404 | ||
from django.utils import simplejson as json | ||
from django.views.decorators.http import require_POST | ||
|
||
from django.contrib.auth.decorators import login_required | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from agon_ratings.models import Rating, OverallRating | ||
|
||
|
||
NUM_OF_RATINGS = getattr(settings, "AGON_NUM_OF_RATINGS", 5) | ||
|
||
|
||
@require_POST | ||
@login_required | ||
def rate(request, content_type_id, object_id): | ||
ct = get_object_or_404(ContentType, pk=content_type_id) | ||
obj = get_object_or_404(ct.model_class(), pk=object_id) | ||
rating_input = int(request.POST.get("rating")) | ||
|
||
data = { | ||
"user_rating": rating_input, | ||
"overall_rating": 0 | ||
} | ||
|
||
# @@@ Seems like this could be much more DRY with a model method or something | ||
if rating_input == 0: # clear the rating | ||
try: | ||
rating = Rating.objects.get( | ||
object_id = object_id, | ||
content_type = ct, | ||
user = request.user | ||
) | ||
overall = rating.overall_rating | ||
rating.delete() | ||
overall.update() | ||
data["overall_rating"] = str(overall.rating) | ||
except Rating.DoesNotExist: | ||
pass | ||
elif 1 <= rating_input <= NUM_OF_RATINGS: # set the rating | ||
print rating_input | ||
rating, created = Rating.objects.get_or_create( | ||
object_id = obj.pk, | ||
content_type = ct, | ||
user = request.user, | ||
defaults = { | ||
"rating": rating_input | ||
} | ||
) | ||
overall, created = OverallRating.objects.get_or_create( | ||
object_id = obj.pk, | ||
content_type = ct | ||
) | ||
rating.overall_rating = overall | ||
rating.rating = rating_input | ||
rating.save() | ||
overall.update() | ||
data["overall_rating"] = str(overall.rating) | ||
else: # whoops | ||
return HttpResponseForbidden( | ||
"Invalid rating. It must be a value between 0 and %s" % NUM_OF_RATINGS | ||
) | ||
|
||
return HttpResponse(json.dumps(data), mimetype="application/json") |
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 @@ | ||
__version__ = "0.1.dev1" |
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,16 @@ | ||
from django.db import models | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
|
||
|
||
class OverallRatingManager(models.Manager): | ||
|
||
def top_rated(self, klass): | ||
|
||
return self.filter( | ||
content_type=ContentType.objects.get_for_model(klass) | ||
).extra( | ||
select={ | ||
"sortable_rating": "COALESCE(rating, 0)" | ||
} | ||
).order_by("-sortable_rating") |
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,48 @@ | ||
import datetime | ||
|
||
from decimal import Decimal | ||
|
||
from django.db import models | ||
|
||
from django.contrib.auth.models import User | ||
from django.contrib.contenttypes.generic import GenericForeignKey | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from agon_ratings.managers import OverallRatingManager | ||
|
||
|
||
class OverallRating(models.Model): | ||
|
||
object_id = models.IntegerField(db_index=True) | ||
content_type = models.ForeignKey(ContentType) | ||
content_object = GenericForeignKey() | ||
rating = models.DecimalField(decimal_places=1, max_digits=3, null=True) | ||
|
||
objects = OverallRatingManager() | ||
|
||
class Meta: | ||
unique_together = [ | ||
("object_id", "content_type"), | ||
] | ||
|
||
def update(self): | ||
self.rating = Rating.objects.filter( | ||
overall_rating = self | ||
).aggregate(r = models.Avg("rating"))["r"] | ||
self.rating = Decimal(str(self.rating or "0")) | ||
self.save() | ||
|
||
|
||
class Rating(models.Model): | ||
overall_rating = models.ForeignKey(OverallRating, null = True, related_name = "ratings") | ||
object_id = models.IntegerField(db_index=True) | ||
content_type = models.ForeignKey(ContentType) | ||
content_object = GenericForeignKey() | ||
user = models.ForeignKey(User) | ||
rating = models.IntegerField() | ||
timestamp = models.DateTimeField(default=datetime.datetime.now) | ||
|
||
class Meta: | ||
unique_together = [ | ||
("object_id", "content_type", "user"), | ||
] |
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,4 @@ | ||
<form action="{% url agon_ratings_rate content_type_id=ct.id object_id=obj.pk %}" method="POST"> | ||
{% csrf_token % | ||
<input type="hidden" name="rating" id="id_rating" /> | ||
</form> |
Empty file.
Oops, something went wrong.