def POST(self): account_email = web.input()['account_email'].strip().lower() account_nickname = web.input()['account_nickname'].strip() account_passwd = web.input()['account_passwd'].strip() account_passwd_confirm = web.input()['account_passwd_confirm'].strip() if not len(account_email) or not len(account_nickname) or not len( account_passwd) or not len(account_passwd_confirm): return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "注册失败: [所有字段都必须填写]。", [['javascript:history.go(-1)', '返回重新注册']])) if account_passwd != account_passwd_confirm: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "注册失败: [两次密码输入不一致]。", [['javascript:history.go(-1)', '返回重新注册']])) r = MUser.AddUser(account_email, account_nickname, account_passwd) if r['Status'] == -1: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "注册失败: [" + r['ErrorMsg'] + ']。', [['javascript:history.go(-1)', '返回重新注册']])) session['UserID'] = r['user_id'] session['UserName'] = r['nickname'] session['UserEmail'] = r['email'] return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_NORMAL, "注册成功,系统已自动为您登陆。", [['/', '返回主页']]))
def POST(self, short_link): if session['UserID'] == -1: web.seeother("/user/login") code_link = web.input()['code_link'].strip() if code_link != short_link: return render.TMessage("<span class='msg-error'>参数传递异常</span><br /><br /><a href='/'>返回主页</a>") if not code_link: return render.TMessage("<span class='msg-error'>参数传递异常</span><br /><br /><a href='/'>返回主页</a>") #先检查该Post是否为当前用户所有 if not MPost.Post.CheckPostOwner(code_link, session['UserID']): return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "你没有权限修改该文章", [['javascript:history.go(-1)', '返回之前的页面']])) r = MPost.Post.DeleteFromDBByPostShortLink(code_link) if r['Status'] == -1: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "操作失败: [" + r['ErrorMsg'] + "]。", [['javascript:history.go(-1)', '返回之前的页面']])) msg = "" msg += "<div style='margin-top:5px;'>" msg += "您还可以执行以下操作:" msg += "</div>" msg += "<div style='margin-top:5px;'>" msg += "<a class='button-a' href='/post/add'>新建代码片段</a>" msg += "<a class='button-a' href='/post/my'>我的代码片段</a>" msg += "</div>" return render.TMessage("<span class='msg-success'>删除成功.</span><br />" + msg)
def POST(self, c_link): if session['UserID'] == -1: web.seeother("/user/login") code_link = web.input()['code_link'].strip() code_title = web.input()['code_title'].strip() code_priviledge = int(web.input()['code_priviledge'].strip()) code_content = web.input()['code_content'].strip() code_language_type = int(web.input()['code_language_type'].strip()) if code_link != c_link: return render.TMessage("<span class='msg-error'>参数传递异常</span><br /><br /><a href='/'>返回主页</a>") if not code_link: return render.TMessage("<span class='msg-error'>参数传递异常</span><br /><br /><a href='/'>返回主页</a>") #先检查该Post是否为当前用户所有 if not MPost.Post.CheckPostOwner(code_link, session['UserID']): return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "你没有权限修改该文章", [['javascript:history.go(-1)', '返回之前的页面']])) if not code_content: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "外面PM2.5这么严重, 宅在家里至少写点代码再提交吧", [['javascript:history.go(-1)', '返回之前的页面']])) if not MLanguage.IsSupportLang(code_language_type): return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "未知的语言类型", [['javascript:history.go(-1)', '返回之前的页面']])) if not len(code_title): code_title = GLOBAL_DEFAULT_POST_TITLE post = { 'link' : code_link, 'priviledge':code_priviledge, 'language_type':code_language_type, 'title':code_title, 'content':code_content, 'last_edit_time':int(time.time()) } r = MPost.Post.UpdateDB(post) if r['Status'] == -1: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "操作失败: [" + r['ErrorMsg'] + "]。", [['javascript:history.go(-1)', '返回之前的页面']])) msg = "" msg += "<div style='margin-top:5px;'>" msg += "还可以继续执行以下操作:" msg += "</div>" msg += "<div style='margin-top:5px;'>" msg += "<a class='button-a' href='/"+ str(post['link']) + "'>查看</a>" if session['UserID'] != -1: msg += "<a class='button-a' href='/post/edit/"+ post['link'] + "'>编辑</a>" msg += "<a class='button-a' href='/post/del/"+ post['link'] + "'>删除</a>" msg += "<a class='button-a' href='/post/add' target='_blank'>新建代码片段</a>" msg += "<a class='button-a' href='/post/my'>我的代码片段</a>" msg += "</div>" return render.TMessage("<span class='msg-success'>编辑成功啦.</span><br />" + msg)
def POST(self): interval = int(time.time()) - session['Status']['LastPublishTime'] if interval < GLOBAL_DEFAULT_POST_PUBLISH_INTERVAL: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "写的也忑快了吧,慢点儿提交吧,请 %d 秒之后重新尝试" % (GLOBAL_DEFAULT_POST_PUBLISH_INTERVAL - interval), [['javascript:history.go(-1)', '返回之前的页面']])) code_title = web.input()['code_title'].strip() code_priviledge = int(web.input()['code_priviledge'].strip()) code_content = web.input()['code_content'].strip() code_language_type = int(web.input()['code_language_type'].strip()) if session['UserID'] == -1 and code_priviledge == GLOBAL_PRIVILEDGE_PRIVATE: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "匿名用户不允许将代码设置为仅自己可见状态", [['javascript:history.go(-1)', '返回之前的页面']])) if not len(code_content): return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "外面PM2.5这么严重, 宅在家里至少写点代码再提交吧", [['javascript:history.go(-1)', '返回之前的页面']])) if not MLanguage.IsSupportLang(code_language_type): return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "未知的语言类型", [['javascript:history.go(-1)', '返回之前的页面']])) if not len(code_title): code_title = GLOBAL_DEFAULT_POST_TITLE post = { 'user_id':session['UserID'], 'priviledge':code_priviledge, 'language_type':code_language_type, 'title':code_title, 'content':code_content, 'publish_time':int(time.time()), 'last_edit_time':int(time.time()) } r = MPost.Post.Insert2DB(post) if r['Status'] == -1: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "操作失败: [" + r['ErrorMsg'] + "]。", [['javascript:history.go(-1)', '返回之前的页面']])) #更新一些状态 session['Status']['LastPublishTime'] = int(time.time()) short_url = "http://snippet-code.com/" + r['link'] msg = "<div style='margin-top:15px;'>快使用该链接与大家分享代码吧<input type='text' onclick='javascript:this.select()' class='shorturl-input' value='" + short_url + "' /></div>" msg += "<div style='margin-top:5px;'>" msg += "还可以执行以下操作:" msg += "</div>" msg += "<div style='margin-top:5px;'>" msg += "<a class='button-a' href='/"+ r['link'] + "'>查看</a>" msg += "<a class='button-a' href='/"+ r['link'] + ".gif'>生成图片</a>" if session['UserID'] != -1: msg += "<a class='button-a' href='/post/edit/"+ r['link'] + "'>编辑</a>" msg += "<a class='button-a' href='/post/del/"+ r['link'] + "'>删除</a>" msg += "<a class='button-a' href='/post/add' target='_blank'>新建代码片段</a>" msg += "<a class='button-a' href='/post/my'>我的代码片段</a>" msg += "</div>" return render.TMessage("<span class='msg-success'>发布成功啦.</span><br />" + msg)
def GET(self, account_id): try: account_id = int(account_id) except: return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "您要查看的用户不存在", [['/', '进入主页']])) user = MUser.GetUserByAccountID(account_id) if not user: return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_NORMAL, "无此用户", [['/', '返回主页']])) return render.TUserProfile(user)
def GET(self, page_idx = 1): if session["UserID"] == -1: web.seeother("/user/login") page_idx = int(page_idx) if page_idx <= 0: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "参数传递错误", [['javascript:history.go(-1)', '返回']])) (posts, total_count) = MPost.Post.GetPostList(session["UserID"], page_idx - 1, GLOBAL_POST_LIST_PAGE_SIZE) max_page_idx = (total_count + GLOBAL_POST_LIST_PAGE_SIZE - 1) / GLOBAL_POST_LIST_PAGE_SIZE if max_page_idx > 0 and page_idx > max_page_idx: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "参数传递错误", [['javascript:history.go(-1)', '返回']])) return render.TPostList(posts, page_idx, max_page_idx)
def GET(self): session.kill() return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_NORMAL, "您已经成功退出。", [['/', '返回主页'], ['/user/login', '重新登陆'], ['/user/reg', '注册新账号']]))
def POST(self, account_id): try: account_id = int(account_id) except: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "参数传递不正确", [['javascript:history.go(-1)', '返回上一页']])) if account_id != session['UserID']: return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "您不能修改其他人的资料", [['/', '返回主页']])) #验证用户当前输入的密码 account_current_passwd = web.input().account_current_passwd.strip() account_passwd = web.input().account_passwd.strip() account_passwd_confirm = web.input().account_passwd_confirm.strip() if not len(account_current_passwd): return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "请填写当前账户密码进行验证", [['javascript:history.go(-1)', '返回上一页']])) if not MUser.VerifyPasswd(account_id, account_current_passwd): return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "当前账户密码验证失败,请输入正确的密码", [['javascript:history.go(-1)', '返回上一页']])) if not len(account_passwd): return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "请填写新密码", [['javascript:history.go(-1)', '返回上一页']])) if len(account_passwd): if account_passwd != account_passwd_confirm: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "新密码两次输入不一致", [['javascript:history.go(-1)', '返回上一页']])) if not MUser.UpdateUserPasswd(account_id, account_passwd): return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "更新您的密码失败", [['/', '返回主页']])) return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_NORMAL, "您的资料已更新,请在下次登陆的时候使用新密码", [['/', '返回主页']]))
def POST(self): short_link = web.input().post_link.strip() user_name = web.input().user_name.strip() comment_content = web.input().comment_content.strip() if not user_name or not comment_content: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "所有字段均不能为空", [['javascript:history.go(-1)', '返回之前的页面']])) post = MPost.Post.QueryDB(short_lnk=short_link) if not post: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "参数传递出错", [['javascript:history.go(-1)', '返回之前的页面']])) comment = { 'post_id': post.id, 'user_name': user_name, 'comment_content': comment_content, 'publish_time': int(time.time()), } r = MComment.Comment.Insert2DB(comment) if r['Status'] == -1: return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_ERROR, "提交评论失败: [" + r['ErrorMsg'] + "]。", [['javascript:history.go(-1)', '返回之前的页面']])) return render.TMessage( MMessage.ConstructCommonMessage(GLOBAL_MSG_NORMAL, "发布评论成功啦!", [['/' + post.link, '返回']]))
def GET(self, page_idx = 1): page_idx = int(page_idx) if page_idx <= 0: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "参数传递错误", [['javascript:history.go(-1)', '返回']])) params = { 'count' : GLOBAL_POST_LIST_PAGE_SIZE, 'order' : 'publish_time DESC', 'offset' : (page_idx - 1) * GLOBAL_POST_LIST_PAGE_SIZE, 'where' : 'priviledge=%d' % GLOBAL_PRIVILEDGE_PUBLIC, } (posts, total_count) = MPost.Post.GetPosts(params) max_page_idx = (total_count + GLOBAL_POST_LIST_PAGE_SIZE - 1) / GLOBAL_POST_LIST_PAGE_SIZE if max_page_idx > 0 and page_idx > max_page_idx: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "参数传递错误", [['javascript:history.go(-1)', '返回']])) return render.TIndexV2(posts, page_idx, max_page_idx)
def GET(self, short_link): if not short_link: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "参数传递错误", [['javascript:history.go(-1)', '返回之前的页面']])) post = MPost.Post.QueryDB(short_lnk = short_link) if not post: return render.TMessage("<span class='msg-error'>失败: [查看该代码片段发生异常]</span><br /><a href='/'>返回主页</a>") #检查查看的权限 if post['priviledge'] == GLOBAL_PRIVILEDGE_PRIVATE: if session['UserID'] != post['user_id']: return render.TMessage("<span class='msg-error'>对不起,您没有权限查看此代码片段</span><br /><a href='/'>返回主页</a>") filename = GLOBAL_PIC_STORE_DIRECTORY + '/' + post.link + ".gif" regen = not os.path.exists(filename) if not regen: #check the post ever be edited after the pic generated st = os.stat(filename) regen = regen or (int(st.st_ctime) < int(post.last_edit_time_ts)) if regen: if GLOBAL_USING_XVFB: cmd = 'xvfb-run --server-args="-screen 0, 800x600x24"' else: cmd = "" cmd += " " + GLOBAL_PHANTOMJS_BIN_DIRECTORY + "/phantomjs" + " " + GLOBAL_PHANTOMJS_SCRIPTS_DIRECTORY + "/rasterize.js" + " " + "http://" + GLOBAL_PROJECT_DOMAIN + "/F_" + post.link + " "+filename os.system(cmd) f = open(filename, "r") web.header('Content-type', 'image/gif') data = f.read() f.close() return data
def GET(self, short_link): if session['UserID'] == -1: web.seeother("/user/login") if not short_link: return render.TMessage("<span class='msg-error'>参数传递异常</span><br /><br /><a href='/'>返回主页</a>") post = MPost.Post.QueryDB(short_lnk = short_link) if post.user.id != session['UserID']: return render.TMessage(MMessage.ConstructCommonMessage(GLOBAL_MSG_ERROR, "您没有权限修改该代码片段", [['javascript:history.go(-1)', '返回']])) if not post: return render.TMessage("<span class='msg-error'>失败: [查看该代码片段发生异常]</span><br /><a href='/'>返回主页</a>") #检查查看的权限 if post['priviledge'] == GLOBAL_PRIVILEDGE_PRIVATE: if session['UserID'] != post['user_id']: return render.TMessage("<span class='msg-error'>对不起,您没有权限编辑此代码片段</span><br /><a href='/'>返回主页</a>") return render.TPostEditView(post, MLanguage.GetAllLangs())
def POST(self): account_email = web.input()['account_email'].strip().lower() account_passwd = web.input()['account_passwd'].strip() r = MUser.CheckUser(account_email, account_passwd) if not r: return render.TMessage( "<span class='msg-error'>账号或密码不正确。</span><br /><a href='javascript:history.go(-1)'>返回重新登陆</a>" ) session['UserID'] = r.id session['UserName'] = r.nickname session['UserEmail'] = r.email return render.TMessage( MMessage.ConstructCommonMessage( GLOBAL_MSG_NORMAL, "您已经成功登陆。", [['/', '进入主页'], ['/post/add', '新建代码片段'], ['/post/my', '我的代码片段'], ['/user/profile/' + str(r.id), '个人资料']]))