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 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)
def test_update(self): self.add_message() aa=MRating.query_by_post(self.post_id) tf=False for i in aa: if i.user_id==self.userid: assert i.rating==float(self.rating) self.uid=i.uid tf=True self.tearDown() assert tf
def render(self, postinfo, userinfo): rating = False if userinfo: rating = MRating.get_rating(postinfo.uid, userinfo.uid) if rating: pass else: rating = postinfo.rating return self.render_string( 'modules/widget/star_rating.html', unescape=tornado.escape.xhtml_unescape, postinfo=postinfo, userinfo=userinfo, rating=rating, )
def render(self, *args, **kwargs): postinfo = args[0] userinfo = args[1] rating = False if userinfo: rating = MRating.get_rating(postinfo.uid, userinfo.uid) if rating: pass else: rating = postinfo.rating return self.render_string( 'modules/widget/star_rating.html', postinfo=postinfo, userinfo=userinfo, rating=rating, )
def test_get_rating(self): self.add_message() rat=MRating.get_rating(self.post_id, self.userid) self.tearDown() assert rat==float(self.rating)
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
# -*- coding:utf-8 -*- import tornado.escape import tornado.web import tornado.web from torcms.model.category_model import MCategory from torcms.model.reply_model import MReply from torcms.model.page_model import MPage from torcms.model.rating_model import MRating mreply = MReply() mpage = MPage() mrating = MRating() class baidu_share(tornado.web.UIModule): def render(self): # out_str = '''<div class="bdsharebuttonbox"><a class="bds_more" href="#" data-cmd="more"></a><a title="分享到QQ空间" class="bds_qzone" href="#" data-cmd="qzone"></a><a title="分享到新浪微博" class="bds_tsina" href="#" data-cmd="tsina"></a><a title="分享到腾讯微博" class="bds_tqq" href="#" data-cmd="tqq"></a><a title="分享到人人网" class="bds_renren" href="#" data-cmd="renren"></a><a title="分享到微信" class="bds_weixin" href="#" data-cmd="weixin"></a></div> # <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>''' # return out_str return self.render_string('modules/widget/baidu_share.html') class reply_panel(tornado.web.UIModule): def render(self, uid, userinfo): return self.render_string('modules/widget/reply_panel.html', uid=uid, replys=mreply.query_by_post(uid), userinfo=userinfo,
def initialize(self): self.init() self.mpost = MPost() self.mrating = MRating()