def POST(self, post_id): if session.user_id is None: raise web.SeeOther('/login') post = post_model().get_one({'id':post_id}) if post is not None: if not self.form.validates(): raise web.SeeOther('/post/' + post_id) else: user_model().update_session(session.user_id) length, cost = money_model().cal_comment(self.form.d.content) if session.money < cost: self.crumb.append('财富不够') return render.no_money('财富不够', '你的财富值不够,不能创建改主题 :(', self.crumb.output()) content = html2db(self.form.d.content) content, receiver_list = notify_model().convert_content(content) create_time = time.time() comment_id = comment_model().insert({'user_id' : session.user_id, 'post_id' : post_id, 'content' : content, 'time' : create_time}) money_type_id = money_type_model().get_one({'name':'comment'})['id'] money_model().insert({'user_id':session.user_id, 'money_type_id':money_type_id, 'amount':-cost, 'length':length, 'balance':user_model().update_money(session.user_id, -cost), 'foreign_id':comment_id}) if session.user_id != post.user_id: money_model().insert({'user_id':post.user_id, 'money_type_id':money_type_id, 'amount':cost, 'length':length, 'foreign_id':comment_id, 'balance':user_model().update_money(post.user_id, cost)}) # notify notify_model().insert({'user_id':session.user_id, 'receiver':post.user_id, 'type_id':notify_type_model().get_one({'name':'comment'}).id, 'foreign_id':comment_id}) # notify receiver_list = list_diff(receiver_list, [session.name, user_model().get_one({'id':post.user_id}).name]) notify_model().insert_notify(session.user_id, receiver_list, 'comment_at', comment_id) user_model().update_session(session.user_id) post_model().update({'id':post_id}, {'last_update':create_time}) post_model().count_comment(post_id) raise web.SeeOther('/post/' + post_id) else: raise web.SeeOther('/post/' + post_id)
def GET(self): crumb = Crumb() condition = {"receiver": session.user_id} total = notify_model().count_table(condition) limit = 10 pagination = Pagination("/notifications", total, limit=limit) page = pagination.true_page(web.input(p=1)["p"]) notify_result = notify_model().get_all(condition, order="id DESC", limit=limit, offset=(page - 1) * limit) notifications = [] if notify_result is not None: for notify in notify_result: post = None user = None comment = None notify_type = notify_type_model().get_one({"id": notify.type_id}).name user = user_model().get_one({"id": notify.user_id}) if notify_type == "post_at": post = post_model().get_one({"id": notify.foreign_id}) elif notify_type == "comment" or notify_type == "comment_at": comment = comment_model().get_one({"id": notify.foreign_id}) post = post_model().get_one({"id": comment.post_id}) notifications.append( {"notify": notify, "post": post, "user": user, "comment": comment, "type": notify_type} ) notify_model().mark_as_read(session.user_id) crumb.append("提醒系统") return render.notify("提醒系统", crumb.output(), total, notifications, pagination.output())
def POST(self, node_name): if session.user_id is None: raise web.SeeOther('/login?next=/post/create' + node_name) conditions = {'name' : node_name} node = node_model().get_one(conditions) if node is None: return render.not_found('节点未找到', '节点未找到') if not self.form.validates(): return render.create_post(self.form, '创建失败, 请重创:D', self.crumb.output()) user_model().update_session(session.user_id) length, cost = money_model().cal_post(self.form.d.content) if session.money < cost: self.crumb.append('财富不够') return render.no_money('财富不够', '你的财富值不够,不能创建改主题 :(', self.crumb.output()) title = strip_tags(self.form.d.title) content = html2db(self.form.d.content) content, receiver_list = notify_model().convert_content(content) create_time = time.time() post_id = post_model().insert({'title' : title, 'content' : content, 'node_id' : node.id, 'time' : create_time, 'last_update':create_time, 'user_id' : session.user_id}) # money money_type_id = money_type_model().get_one({'name':'post'})['id'] money_model().insert({'user_id':session.user_id, 'money_type_id':money_type_id, 'amount':-cost, 'length':length, 'balance':user_model().update_money(session.user_id, -cost), 'foreign_id':post_id}) # notify receiver_list = list_diff(receiver_list, [session.name]) notify_model().insert_notify(session.user_id, receiver_list, 'post_at', post_id) user_model().update_session(session.user_id) raise web.seeother('/post/' + str(post_id))
def POST(self, post_id): if session.user_id is None: raise web.SeeOther('/login?next=/edit/post/' + post_id) conditions = {'id': int(post_id)} post = post_model().get_one(conditions) if post is None: return render.not_found('节点未找到', '节点未找到') if not self.form.validates(): return render.create_post(self.form, '创建失败, 请重创:D', self.crumb.output()) user_model().update_session(session.user_id) length, cost = money_model().cal_post(self.form.d.content) if session.money < cost: self.crumb.append('财富不够') return render.no_money('财富不够', '你的财富值不够,不能创建改主题 :(', self.crumb.output()) title = strip_tags(self.form.d.title) content = html2db(self.form.d.content) content, receiver_list = notify_model().convert_content(content) create_time = time.time() post_model().update({'id': int(post_id)}, { 'title': title, 'content': content, 'node_id': post.node_id, 'time': create_time, 'last_update': create_time, 'user_id': session.user_id }) # money money_type_id = money_type_model().get_one({'name': 'post'})['id'] money_model().insert({ 'user_id': session.user_id, 'money_type_id': money_type_id, 'amount': -cost, 'length': length, 'balance': user_model().update_money(session.user_id, -cost), 'foreign_id': post_id }) # notify receiver_list = list_diff(receiver_list, [session.name]) notify_model().insert_notify(session.user_id, receiver_list, 'post_at', post_id) user_model().update_session(session.user_id) raise web.seeother('/post/' + str(post_id))
def POST(self): if web.input(ajax=None): return notify_model().count_table({ 'receiver': session.user_id, 'unread': '1' }) else: return '0'
def GET(self): crumb = Crumb() condition = {'receiver': session.user_id} total = notify_model().count_table(condition) limit = 10 pagination = Pagination('/notifications', total, limit=limit) page = pagination.true_page(web.input(p=1)['p']) notify_result = notify_model().get_all(condition, order='id DESC', limit=limit, offset=(page - 1) * limit) notifications = [] if notify_result is not None: for notify in notify_result: post = None user = None comment = None notify_type = notify_type_model().get_one({ 'id': notify.type_id }).name user = user_model().get_one({'id': notify.user_id}) if notify_type == 'post_at': post = post_model().get_one({'id': notify.foreign_id}) elif notify_type == 'comment' or notify_type == 'comment_at': comment = comment_model().get_one( {'id': notify.foreign_id}) post = post_model().get_one({'id': comment.post_id}) notifications.append({ 'notify': notify, 'post': post, 'user': user, 'comment': comment, 'type': notify_type }) notify_model().mark_as_read(session.user_id) crumb.append('提醒系统') return render.notify('提醒系统', crumb.output(), total, notifications, pagination.output())
from config.urls import * from libraries import helper from libraries import widget from models.site_model import * from models.user_model import * from models.notify_model import * web.template.Template.globals['helper'] = helper web.template.Template.globals['widget'] = widget web.template.Template.globals['site_options'] = site_model().get_options() app = web.application(urls, globals()) if web.config.get('_session') is None: session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'user_id': None}) web.config._session = session else: session = web.config._session #user_model.auth_cookie() app.add_processor(user_model().auth_cookie) app.add_processor(notify_model().check) # 如果这里不 不将 session 赋值给模板全局变量, 模板中将不能得到此变量 web.template.Template.globals['session'] = session #web.template.Template.globals['site_url'] = 'http://127.0.0.1:8080' if __name__ == "__main__": app.run()
import web from config.config import * from config.urls import * from libraries import helper from libraries import widget from models.site_model import * from models.user_model import * from models.notify_model import * web.template.Template.globals['helper'] = helper web.template.Template.globals['widget'] = widget web.template.Template.globals['site_options'] = site_model().get_options() app = web.application(urls, globals()) if web.config.get('_session') is None: session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'user_id': None}) web.config._session = session else: session = web.config._session #user_model.auth_cookie() app.add_processor(user_model().auth_cookie) app.add_processor(notify_model().check) # 如果这里不 不将 session 赋值给模板全局变量, 模板中将不能得到此变量 web.template.Template.globals['session'] = session #web.template.Template.globals['site_url'] = 'http://127.0.0.1:8080' if __name__ == "__main__": app.run()
def GET(self, id): notify = notify_model().get_one({'id': id}) if notify is not None and notify.receiver == session.user_id: notify_model().delete({'id': id}) raise web.SeeOther('/notifications')
def POST(self, post_id): if session.user_id is None: raise web.SeeOther('/login') post = post_model().get_one({'id': post_id}) if post is not None: if not self.form.validates(): raise web.SeeOther('/post/' + post_id) else: user_model().update_session(session.user_id) length, cost = money_model().cal_comment(self.form.d.content) if session.money < cost: self.crumb.append('财富不够') return render.no_money('财富不够', '你的财富值不够,不能创建改主题 :(', self.crumb.output()) content = html2db(self.form.d.content) content, receiver_list = notify_model().convert_content( content) create_time = time.time() comment_id = comment_model().insert({ 'user_id': session.user_id, 'post_id': post_id, 'content': content, 'time': create_time }) money_type_id = money_type_model().get_one({'name': 'comment'})['id'] money_model().insert({ 'user_id': session.user_id, 'money_type_id': money_type_id, 'amount': -cost, 'length': length, 'balance': user_model().update_money(session.user_id, -cost), 'foreign_id': comment_id }) if session.user_id != post.user_id: money_model().insert({ 'user_id': post.user_id, 'money_type_id': money_type_id, 'amount': cost, 'length': length, 'foreign_id': comment_id, 'balance': user_model().update_money(post.user_id, cost) }) # notify notify_model().insert({ 'user_id': session.user_id, 'receiver': post.user_id, 'type_id': notify_type_model().get_one({ 'name': 'comment' }).id, 'foreign_id': comment_id }) # notify receiver_list = list_diff(receiver_list, [ session.name, user_model().get_one({ 'id': post.user_id }).name ]) notify_model().insert_notify(session.user_id, receiver_list, 'comment_at', comment_id) user_model().update_session(session.user_id) post_model().update({'id': post_id}, {'last_update': create_time}) post_model().count_comment(post_id) raise web.SeeOther('/post/' + post_id) else: raise web.SeeOther('/post/' + post_id)
def GET(self, id): notify = notify_model().get_one({"id": id}) if notify is not None and notify.receiver == session.user_id: notify_model().delete({"id": id}) raise web.SeeOther("/notifications")
def POST(self): if web.input(ajax=None): return notify_model().count_table({"receiver": session.user_id, "unread": "1"}) else: return "0"