Skip to content

Commit

Permalink
Merge branch '2070-rating-moved'
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Nov 20, 2014
2 parents c69753c + 45f476c commit 7877ba6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 60 deletions.
8 changes: 4 additions & 4 deletions ckan/logic/action/create.py
Expand Up @@ -10,7 +10,6 @@

import ckan.lib.plugins as lib_plugins
import ckan.logic as logic
import ckan.rating as ratings
import ckan.plugins as plugins
import ckan.lib.dictization
import ckan.logic.action
Expand Down Expand Up @@ -847,16 +846,17 @@ def rating_create(context, data_dict):
opts_err = _('Rating must be an integer value.')
else:
package = model.Package.get(package_ref)
if rating < ratings.MIN_RATING or rating > ratings.MAX_RATING:
if rating < model.MIN_RATING or rating > model.MAX_RATING:
opts_err = _('Rating must be between %i and %i.') \
% (ratings.MIN_RATING, ratings.MAX_RATING)
% (model.MIN_RATING, model.MAX_RATING)
elif not package:
opts_err = _('Not found') + ': %r' % package_ref
if opts_err:
raise ValidationError(opts_err)

user = model.User.by_name(user)
ratings.set_rating(user, package, rating_int)
package.set_rating(user, rating_int)
model.repo.commit()

package = model.Package.get(package_ref)
ret_dict = {'rating average': package.get_average_rating(),
Expand Down
2 changes: 2 additions & 0 deletions ckan/model/__init__.py
Expand Up @@ -105,6 +105,8 @@
)
from rating import (
Rating,
MIN_RATING,
MAX_RATING,
)
from related import (
Related,
Expand Down
50 changes: 50 additions & 0 deletions ckan/model/package.py
Expand Up @@ -24,6 +24,7 @@
'PACKAGE_NAME_MAX_LENGTH', 'PACKAGE_NAME_MIN_LENGTH',
'PACKAGE_VERSION_MAX_LENGTH', 'PackageTagRevision', 'PackageRevision']


PACKAGE_NAME_MAX_LENGTH = 100
PACKAGE_NAME_MIN_LENGTH = 2
PACKAGE_VERSION_MAX_LENGTH = 100
Expand Down Expand Up @@ -558,6 +559,55 @@ def activity_stream_detail(self, activity_id, activity_type):
return activity.ActivityDetail(activity_id, self.id, u"Package", activity_type,
{'package': package_dict })

def set_rating(self, user_or_ip, rating):
'''Record a user's rating of this package.
The caller function is responsible for doing the commit.
If a rating is outside the range MAX_RATING - MIN_RATING then a
RatingValueException is raised.
@param user_or_ip - user object or an IP address string
'''
user = None
from user import User
from rating import Rating, MAX_RATING, MIN_RATING
if isinstance(user_or_ip, User):
user = user_or_ip
rating_query = meta.Session.query(Rating)\
.filter_by(package=self, user=user)
else:
ip = user_or_ip
rating_query = meta.Session.query(Rating)\
.filter_by(package=self, user_ip_address=ip)

try:
rating = float(rating)
except TypeError:
raise RatingValueException
except ValueError:
raise RatingValueException
if rating > MAX_RATING or rating < MIN_RATING:
raise RatingValueException

if rating_query.count():
rating_obj = rating_query.first()
rating_obj.rating = rating
elif user:
rating = Rating(package=self,
user=user,
rating=rating)
meta.Session.add(rating)
else:
rating = Rating(package=self,
user_ip_address=ip,
rating=rating)
meta.Session.add(rating)


class RatingValueException(Exception):
pass

# import here to prevent circular import
import tag

Expand Down
6 changes: 5 additions & 1 deletion ckan/model/rating.py
Expand Up @@ -8,7 +8,11 @@
import domain_object
import types as _types

__all__ = ['Rating']
__all__ = ['Rating', 'MIN_RATING', 'MAX_RATING']

MIN_RATING = 1.0
MAX_RATING = 5.0


rating_table = Table('rating', meta.metadata,
Column('id', types.UnicodeText, primary_key=True, default=_types.make_uuid),
Expand Down
55 changes: 0 additions & 55 deletions ckan/rating.py

This file was deleted.

0 comments on commit 7877ba6

Please sign in to comment.