def POST(self): import json json_dict = {'success':0, 'msg':'', 'script':''} post_id = web.input(post_id=None)['post_id'] post = post_model().get_one({'id':post_id}) if post_id and post: if session.user_id is None: json_dict['msg'] = '你要先登录的亲' json_dict['script'] = 'location.href=\'/login?next=/post/'+post.id+'\'' elif post.user_id != session.user_id: post_thanks_id = post_thanks_model().unique_insert({'user_id':session.user_id, 'post_id':post_id}) if post_thanks_id: post_thanks_model().update({'id':post_thanks_id}, {'time':int(time.time())}) cost = money_model().cal_thanks() money_type_id = money_type_model().get_one({'name':'post_thanks'})['id'] money_model().insert({'user_id':session.user_id, 'money_type_id':money_type_id, 'amount':-cost, 'balance':user_model().update_money(session.user_id, -cost), 'foreign_id':post_thanks_id}) money_model().insert({'user_id':post.user_id, 'money_type_id':money_type_id, 'amount':cost, 'foreign_id':post_thanks_id, 'balance':user_model().update_money(post.user_id, cost)}) post_model().count_thanks(post_id) user_model().update_session(session.user_id) json_dict['success'] = 1 else: json_dict['msg'] = '你已经感谢过了不是吗?' else: json_dict['msg'] = '你不能感谢你自己不是吗?' else: json_dict['message'] = '评论不存在' return json.dumps(json_dict)
def POST(self): import json json_dict = {'success':0, 'msg':'', 'script':''} post_id = web.input(post_id=None)['post_id'] post = post_model().get_one({'id':post_id}) if post_id and post: if session.user_id is None: json_dict['msg'] = '你要先登录的亲' json_dict['script'] = 'location.href=\'/login?next=/post/'+post.id+'\'' elif post.user_id != session.user_id: post_thanks_id = post_thanks_model().unique_insert({'user_id':session.user_id, 'post_id':post_id}) if post_thanks_id: post_thanks_model().update({'id':post_thanks_id}, {'time':int(time.time())}) cost = money_model().cal_thanks() money_type_id = money_type_model().get_one({'name':'post_thanks'})['id'] money_model().insert({'user_id':session.user_id, 'money_type_id':money_type_id, 'amount':-cost, 'balance':user_model().update_money(session.user_id, -cost), 'foreign_id':post_thanks_id}) money_model().insert({'user_id':post.user_id, 'money_type_id':money_type_id, 'amount':cost, 'foreign_id':post_thanks_id, 'balance':user_model().update_money(post.user_id, cost)}) post_model().count_thanks(post_id) user_model().update_session(session.user_id) json_dict['success'] = 1 else: json_dict['msg'] = '你已经感谢过了不是吗?' else: json_dict['msg'] = '你不能感谢你自己不是吗?' else: json_dict['message'] = '评论不存在' return json.dumps(json_dict)
def count_thanks(self, post_id): super(post_model, self).query('update ' + self._tb + ' set thanks = (select count(*) from ' + post_thanks_model().table_name() + ' where ' + post_thanks_model().table_name() + '.post_id = ' + str(post_id) + ') where id = ' + str(post_id))
def GET(self): limit = 20 total = money_model().count_table({'user_id':session.user_id}) pagination = Pagination('/balance', total, limit = limit) page = pagination.true_page(web.input(p=1)['p']) records_result = money_model().get_all({'user_id':session.user_id}, limit = limit, offset = (page-1)*limit, order = 'id DESC') money_types_result = money_type_model().get_all() money_type = {} for money_type_result in money_types_result: money_type[money_type_result.id] = money_type_result.name records = [] for record_result in records_result: # 发布的帖子或者是评论的帖子 post = None # 发布或者收到的评论 post_user = None post_thanks = None comment_thanks = None sender = None comment = None # 评论的用户 comment_user = None try: type = money_type[record_result.money_type_id] if type == 'post': post = post_model().get_one({'id':record_result.foreign_id}) if type == 'comment': comment = comment_model().get_one({'id':record_result.foreign_id}) comment_user = user_model().get_one({'id':comment.user_id}) post = post_model().get_one({'id':comment.post_id}) if type == 'post_thanks': post_thanks = post_thanks_model().get_one({'id':record_result.foreign_id}) post = post_model().get_one({'id':post_thanks.post_id}) sender = user_model().get_one({'id':post_thanks.user_id}) post_user = user_model().get_one({'id':post.user_id}) if type == 'comment_thanks': comment_thanks = comment_thanks_model().get_one({'id':record_result.foreign_id}) comment = comment_model().get_one({'id':comment_thanks.comment_id}) post = post_model().get_one({'id':comment.post_id}) comment_user = user_model().get_one({'id':comment.user_id}) sender = user_model().get_one({'id':comment_thanks.user_id}) # 如果数据错误将不把这条记录输出到视图 except AttributeError: continue else: record = {'record':record_result, 'type':type, 'comment':comment, 'post':post, 'post_user':post_user, 'sender':sender, 'comment_user':comment_user, 'post_thanks':post_thanks, 'comment_thanks':comment_thanks} records.append(record) self.crumb.append('账户余额') return render.money_record('账户余额', records, self.crumb.output(), pagination.output())
def GET(self, id): limit = 10 post_model().add_view(id) post = post_model().get_one({'id':id}) if post is None: self.crumb.append('主题未找到') return render.post_nf('主题未找到', self.crumb.output()) else: post_fav = False if session.user_id: if user_meta_model().get_one({'user_id':session.user_id, 'meta_key':'post_fav', 'meta_value':post.id}): post_fav = True favs = user_meta_model().count_meta({'meta_key':'post_fav','meta_value':id}) node = node_model().get_one({'id':post.node_id}) user = user_model().get_one({'id':post.user_id}) #return user.name self.crumb.append(node.display_name, '/node/'+node.name) thanks = False if session.user_id is not None: if post_thanks_model().get_one({'user_id':session.user_id, 'post_id':post.id}): thanks = True condition = {'post_id' : post.id} # Pagination total = comment_model().count_table(condition) pagination = Pagination('/post/'+str(post.id), total, limit = 100) page = pagination.true_page(web.input(p=1)['p']) comments_result = comment_model().get_all(condition, order = 'time ASC', limit = 100, offset = (page-1)*100) comments = [] if comments_result is not None: for comment_result in comments_result: comment_user = user_model().get_one({'id':comment_result.user_id}) comment_thanks = False if session.user_id is not None: if comment_thanks_model().get_one({'user_id':session.user_id, 'comment_id':comment_result.id}): comment_thanks = True comments.append({'comment':comment_result, 'user':comment_user, 'thanks':comment_thanks}) form = comment_model().form return render.post_view(post, user, comments, form, post_fav, favs, thanks, self.crumb.output(), pagination)
def GET(self, id): limit = 10 post_model().add_view(id) post = post_model().get_one({'id':id}) if post is None: self.crumb.append('主题未找到') return render.post_nf('主题未找到', self.crumb.output()) else: post_fav = False if session.user_id: if user_meta_model().get_one({'user_id':session.user_id, 'meta_key':'post_fav', 'meta_value':post.id}): post_fav = True favs = user_meta_model().count_meta({'meta_key':'post_fav','meta_value':id}) node = node_model().get_one({'id':post.node_id}) user = user_model().get_one({'id':post.user_id}) #return user.name self.crumb.append(node.display_name, '/node/'+node.name) thanks = False if session.user_id is not None: if post_thanks_model().get_one({'user_id':session.user_id, 'post_id':post.id}): thanks = True condition = {'post_id' : post.id} # Pagination total = comment_model().count_table(condition) pagination = Pagination('/post/'+str(post.id), total, limit = 100) page = pagination.true_page(web.input(p=(total/100)*100 + 1)['p']) comments_result = comment_model().get_all(condition, order = 'time ASC', limit = 100, offset = (page-1)*100) comments = [] if comments_result is not None: for comment_result in comments_result: comment_user = user_model().get_one({'id':comment_result.user_id}) comment_thanks = False if session.user_id is not None: if comment_thanks_model().get_one({'user_id':session.user_id, 'comment_id':comment_result.id}): comment_thanks = True comments.append({'comment':comment_result, 'user':comment_user, 'thanks':comment_thanks}) form = comment_model().form return render.post_view(post, user, comments, form, post_fav, favs, thanks, self.crumb.output(), pagination)
def count_thanks(self, post_id): super(post_model, self).query('update ' + self._tb + ' set thanks = (select count(*) from ' + post_thanks_model().table_name() + ' where ' + post_thanks_model().table_name() + '.post_id = ' + str(post_id) + ') where id = ' + str(post_id))