def get(self, object_id): args = self.parser.parse_args() osmanager = ObjectSubjectsManager(**args) somanager = SubjectObjectsManager(**args) votes = osmanager.scoredrange( object_id=object_id, offset=args['offset'], size=args['size'], min_vote=args['vote'] or '-inf', max_vote=args['vote'] or '+inf', reverse=args['sort'] == '-', ) if not votes: return [] reviews = osmanager.reviews(object_id, *[user_id for user_id, _ in votes]) reply = [ get_user_object_reply( object_id=object_id, user_id=user_id, vote=vote, when=somanager.score(user_id=user_id, object_id=object_id), review=reviews[user_id], ) for user_id, vote in votes ] return reply
def get(self, object_id): args = self.parser.parse_args() vote = args['vote'] amount = ObjectsManager(**args).score(object_id=object_id) or 0 votes_no = ObjectSubjectsManager(**args).count( object_id=object_id, min_vote=vote or '-inf', max_vote=vote or '+inf', ) if not votes_no: average = 0 amount = 0 elif vote: average = vote amount = vote * votes_no else: average = amount / votes_no return (dict( votes_no=votes_no, average=average, amount=amount, object_id=object_id, ))
def put(self, object_id, user_id): self.parser.add_argument('vote', type=int, required=True) self.parser.add_argument('review', type=text_type) args = self.parser.parse_args() osmanager = ObjectSubjectsManager(**args) somanager = SubjectObjectsManager(**args) self._perform_correction(object_id, user_id, args['vote'], args) osmanager.create(object_id=object_id, user_id=user_id, vote=args['vote'], review=args['review']) somanager.create(user_id=user_id, object_id=object_id, timestamp=time()) return get_user_object_reply( object_id=object_id, user_id=user_id, vote=osmanager.score(object_id=object_id, user_id=user_id), when=somanager.score(user_id=user_id, object_id=object_id), review=osmanager.review(object_id=object_id, user_id=user_id), )
def _perform_correction(self, object_id, user_id, next_vote, args): prev_vote = ObjectSubjectsManager(**args).score(object_id=object_id, user_id=user_id) or 0 correction = next_vote - prev_vote omanager = ObjectsManager(**args) omanager.incrby(object_id=object_id, delta=correction) amount = omanager.score(object_id=object_id) if amount == 0: omanager.remove(object_id=object_id)
def delete(self, object_id, user_id): args = self.parser.parse_args() self._perform_correction(object_id, user_id, 0, args) SubjectObjectsManager(**args).remove(user_id=user_id, object_id=object_id) ObjectSubjectsManager(**args).remove(object_id=object_id, user_id=user_id) return '', 204
def get(self, object_id, user_id): args = self.parser.parse_args() osmanager = ObjectSubjectsManager(**args) somanager = SubjectObjectsManager(**args) vote = osmanager.score(object_id=object_id, user_id=user_id) when = somanager.score(user_id=user_id, object_id=object_id) if not (vote and when): message = "No vote on {object_id} by {user_id}.".format( object_id=object_id, user_id=user_id) abort(404, message=message) return get_user_object_reply( object_id=object_id, user_id=user_id, vote=vote, when=when, review=osmanager.review(object_id=object_id, user_id=user_id), )
def get(self): args = self.parser.parse_args() amounts = ObjectsManager(**args).scoredrange( offset=args['offset'], size=args['size'], reverse=args['sort'] == '-', ) reply = [] osmanager = ObjectSubjectsManager(**args) for object_id, amount in amounts: votes_no = osmanager.count(object_id=object_id) if votes_no: # skipping objects with no votes reply.append( dict( votes_no=votes_no, average=amount / votes_no, amount=amount, object_id=object_id, )) return reply
def get(self): args = self.parser.parse_args() amounts = ObjectsManager(**args).scoredrange( offset=args['offset'], size=args['size'], reverse=args['sort'] == '-', ) reply = [] osmanager = ObjectSubjectsManager(**args) for object_id, amount in amounts: votes_no = osmanager.count(object_id=object_id) if votes_no: # skipping objects with no votes reply.append( dict( votes_no=votes_no, average=amount / votes_no, amount=amount, object_id=object_id, ) ) return reply
def get(self, object_id, user_id): args = self.parser.parse_args() osmanager = ObjectSubjectsManager(**args) somanager = SubjectObjectsManager(**args) vote = osmanager.score(object_id=object_id, user_id=user_id) when = somanager.score(user_id=user_id, object_id=object_id) if not (vote and when): message = "No vote on {object_id} by {user_id}.".format( object_id=object_id, user_id=user_id ) abort(404, message=message) return get_user_object_reply( object_id=object_id, user_id=user_id, vote=vote, when=when, review=osmanager.review(object_id=object_id, user_id=user_id), )
def get(self, user_id): args = self.parser.parse_args() osmanager = ObjectSubjectsManager(**args) somanager = SubjectObjectsManager(**args) votetimes = somanager.scoredrange( user_id=user_id, offset=args['offset'], size=args['size'], reverse=args['sort'] == '-', ) reply = [ get_user_object_reply( object_id=object_id, user_id=user_id, vote=osmanager.score(object_id=object_id, user_id=user_id), review=osmanager.review(object_id=object_id, user_id=user_id), when=when, ) for object_id, when in votetimes ] return reply