def test_query_average_rating(self): self.add_message() ave1=MRating.query_average_rating(self.post_id) assert ave1==self.rating MRating.update(self.post_id, 'fin', 4) ave=MRating.query_average_rating(self.post_id) assert ave==3.5 self.tearDown() aa = MRating.query_by_post(self.post_id) for i in aa: if i.user_id == 'fin': self.uid2 = i.uid MHelper.delete(TabRating,self.uid2)
class RatingHandler(BaseHandler): def initialize(self): self.init() self.mpost = MPost() self.mrating = MRating() def post(self, url_str=''): url_arr = self.parse_url(url_str) print(url_arr) if len(url_arr) == 2 and url_arr[0] == '_update': self.update_rating(url_arr[1]) elif len(url_arr) == 2 and url_arr[0] == '_updatepost': self.update_post(url_arr[1]) def update_post(self, postid): uu = self.mrating.query_by_post(postid) if uu.count() > 10: rating = self.mrating.query_average_rating(postid) else: rating = 5 print('Get post rating:', rating) self.mpost.update_rating(postid, rating) def update_rating(self, postid): post_data = self.get_post_data() rating = float(post_data['rating']) postinfo = self.mpost.get_by_uid(postid) if postinfo and self.userinfo: self.mrating.update(postinfo.uid, self.userinfo.uid, rating=rating) else: return False
def update_post(self, postid): ''' The rating of Post should be updaed if the count is greater than 10 ''' voted_recs = MRating.query_by_post(postid) if voted_recs.count() > 10: rating = MRating.query_average_rating(postid) else: rating = 5 logger.info('Get post rating: {rating}'.format(rating=rating)) # MPost.__update_rating(postid, rating) MPost.update_misc(postid, rating=rating)