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_rating(self, postid): ''' only the used who logged in would voting. ''' post_data = self.get_post_data() rating = float(post_data['rating']) postinfo = MPost.get_by_uid(postid) if postinfo and self.userinfo: MRating.update(postinfo.uid, self.userinfo.uid, rating=rating) self.update_post(postid) else: return False
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)
def add_message(self): MRating.update(self.post_id, self.userid,self.rating) aa = MRating.query_by_post(self.post_id) for i in aa: if i.user_id == self.userid: self.uid = i.uid