def update_title(): postid = request.form.get("postid", None) newtitle = request.form.get("newtitle", None) print(request.form) if postid and newtitle: posts = va_query( db_name, """ select owner from post where postid = ?""", postid) if posts: owner = posts[0][0] if owner == current_user.username: va_alter( db_name, """ update post set title = ? where postid = ?""", newtitle, postid) return Response(json.dumps({ "status": "success", "href": ("/blog/%s/%s" % (owner, newtitle)), "text": newtitle, "postid": postid }), mimetype='application/json') return Response(json.dumps({"status": "failed"}), mimetype='application/json')
def index(): name = "" if current_user.is_anonymous else current_user.get_username() user_list = [t[0] for t in va_query(db_name, "select username from user")] return render_template('index.html', user_list=user_list, logging=not current_user.is_anonymous, name=name)
def theme_user_post(theme, user, title): if theme not in ['mist', 'muse', 'gemini', 'pisces']: return abort(404) if not users.exist(user): return abort(404) post = va_query(db_name, """ select title, postdate, content,postid from post where owner = ? and title = ? """, user, title) if post == []: return abort(404) mkd = markdown2.markdown(post[0][2], extras=["header-ids", "fenced-code-blocks","tables"]) markdown_html = markdown2.markdown(post[0][2], extras=["toc","fenced-code-blocks","tables"]) toc = markdown_html.toc_html return render_template( 'next_' + theme + '_post.html', user_name = user, post_title = post[0][0], post_href = '/blog/' + user + '/' + title, post_time = post[0][1], post_content = mkd, post_id = post[0][3], blog_name = get_blog_theme(user)[0], blog_href = '/blog/' + user, post_wedge = '\n', post_tocs = toc, cat_href = ("/" if current_user.is_anonymous else "/manage"))
def manage(): posts = query( db_name, # load articles """ select postid, title, postdate from post where owner = """ + "'" + current_user.username + "'") blogname = va_query( db_name, """ select blogname from blog where owner = ? """, current_user.username)[0][0] postid = [] title = [] date = [] for pid, tit, dat in posts: postid.append(pid) title.append(tit) date.append(dat) length = list(range(len(title))) #posts = list(map(lambda x: x[0], posts)) # delete function return render_template('manage.html', blog=blogname, name=current_user.username, title=title, postid=postid, date=date, length=length)
def get_post_message(post_id, methods=['GET']): print(post_id) print(current_user.is_anonymous) loging = not current_user.is_anonymous who = current_user.username if loging else '' msgs = va_query(db_name, """ select poster, msgdate, content, msgid from message where postid = ? order by msgdate desc """, post_id) blogger = va_query(db_name, """ select owner from post where postid = ? """, post_id)[0][0] msgs = [(a,b,markdown2.markdown(c, extras=["header-ids", "fenced-code-blocks"]), d) for (a,b,c,d) in msgs] next_href = '/login?' + urlencode( {'next':request.args["next"]}, quote_via=quote_plus) return render_template('message.html', who = who, messages = msgs, blogger = blogger, logining = loging, post_id = post_id, prev_href = request.args.get("next", default=""), submit_url = ("/message_upload" if loging else '/login'), submit_btn_name = ("留言" if loging else "登入"), method = ("post" if loging else "get"), msg_count = len(msgs) )
def message_del(): msgid = request.args.get("id", None) if not msgid or current_user.is_anonymous: return Response("not ok") postid, writer = va_query(db_name,""" select postid, poster from message where msgid = ?""", msgid)[0] owner = va_query(db_name,""" select owner from post where postid = ? """, postid)[0][0] if current_user.username == owner or \ current_user.username == writer: va_alter(db_name, """ delete from message where msgid = ?""", msgid) return Response("ok")
def editor(): blogname = request.args.get('blog', default=None) if not blogname: blogname = va_query( db_name, """ select blogname from blog where owner = ? """, current_user.username)[0][0] postid = request.args.get('postid', default=None) if not postid: return render_template('editor.html', title="new post", name=current_user.username, blog=blogname) #return abort(404) name = current_user.get_username() posts = va_query( db_name, """ select content , title, owner from post where postid = ? """, postid) if not posts: return abort(404) else: mkd, title, owner = posts[0] if name != owner: return abort(403) return render_template('editor.html', mkd=mkd, title=title, postid=postid, name=current_user.username, blog=blogname)
def theme_home_page(theme, user): if theme not in ['mist', 'muse', 'gemini', 'pisces']: return abort(404) if not users.exist(user): return abort(404) posts = va_query(db_name, """ select title, postdate from post where owner = ? """, user) return render_template('next_' + theme + '.html', user_name = user, posts = posts, blog_name = get_blog_theme(user)[0], post_wedge = '\n', cat_href = ("/" if current_user.is_anonymous else "/manage"))
def update_post(): mkd = request.form.get('mkd') postid = request.form.get('postid', default=None) blogname = request.form.get('blog', default=None) name = current_user.get_username() if postid: # update posts = va_query( db_name, """ select title, owner from post where postid = ? """, postid) if not posts: return abort(404) title, owner = posts[0] if owner != name: return abort(403) title = request.form.get("title", title) va_alter( db_name, """update post set content = ?, title = ? where postid = ? """, mkd, title, postid) return ("success") elif blogname: # insert print("name is", blogname) title = request.form.get("title", "new post") va_alter( db_name, """ INSERT INTO POST (TITLE, CONTENT, POSTDATE, OWNER, BLOGNAME) VALUES (?,?, ?, ?, ?);""", title, mkd, time.strftime('%Y-%m-%d %H:%M:%S'), name, blogname) return ("success") else: return ("failed")
def home_page(user): if not users.exist(user): return abort(404) posts = va_query(db_name, """ select title, postdate from post where owner = ? """, user) blogname, theme = get_blog_theme(user) return render_template('next_' + theme + '.html', user_name = user, posts = posts, blog_name = blogname, post_wedge = '\n', cat_href = ("/" if current_user.is_anonymous else "/manage")) if not users.exist(user): return abort(404) return render_template('home_page.html', blog_name , blog_href)
def get_blog_theme(user): return va_query(db_name, """ select blogname, layoutname from blog where owner = ? """, user)[0]