def publish(): data = request.get_data(as_text=True) json_re = json.loads(data) db = get_db() uid = uuid.uuid1() is_exist = db.execute('SELECT 1 FROM article_markdown WHERE markdown_id = ?',(json_re['markdown_id'],)).fetchone() if is_exist == None: db.execute( 'INSERT INTO article_markdown (id, markdown_id, author_id,title,content,create_time,modefied_time,tag)' ' VALUES (null, ?, ?, ?, ?, ?, ?, ?)', (json_re['markdown_id']+str(uid),json_re['author_id'],json_re['title'],json_re['content'].replace('\n','\\n').strip('"'), datetime.datetime.now().strftime('%Y-%m-%d'), datetime.datetime.now().strftime('%Y-%m-%d'),json_re['tag']) ) db.commit() LogInfo().logger.info('/article/publish => 新增数据') else: db.execute( 'UPDATE article_markdown SET content = ? WHERE markdown_id = ?', (json_re['content'].replace('\n','\\n').strip('"'),json_re['markdown_id']) ) db.execute( 'UPDATE article_markdown SET title = ? WHERE markdown_id = ?', (json_re['title'].strip('"'),json_re['markdown_id']) ) db.execute( 'UPDATE article_markdown SET tag = ? WHERE markdown_id = ?', (json_re['tag'].strip('"'),json_re['markdown_id']) ) db.commit() LogInfo().logger.info('/article/publish => 更新数据%s' % json_re['markdown_id']) return json.dumps({'status':'success'})
def search(indistince_name=''): arcList = [] article_sql = "select * from article as ar inner join author as au where ar.author_id=au.author_id and ar.article_title like ? order by ar.article_modefiedtime limit 0,12" markdown_sql = "SELECT * FROM article_markdown AS am INNER JOIN author AS au WHERE am.author_id = au.author_id and am.title like ? ORDER BY am.create_time limit 0,12" db = get_db() indistince_name = '%%%s%%' % indistince_name articles = db.execute(article_sql, (indistince_name, )).fetchall() markdowns = db.execute(markdown_sql, (indistince_name, )).fetchall() for arc in articles: item = { 'id': arc['article_id'], 'modefiedtime': arc['article_modefiedtime'], 'tag': arc['article_tag'], 'imgpath': arc['article_imgpath'], 'title': arc['article_title'], 'subtitle': arc['article_subtitle'] } arcList.append(item) for mkitem in markdowns: item = { 'id': mkitem['markdown_id'], 'modefiedtime': mkitem['modefied_time'], 'tag': mkitem['tag'], 'imgpath': '/static/img/mkdown.jpg', 'title': mkitem['title'], 'subtitle': 'markdown need not subtitle' } arcList.append(item) LogInfo().logger.info('/ 搜索到数据') return render_template('home/index.html', arcList=arcList)
def get_article_list(): arcList = [] article_sql = 'select * from article as ar inner join author as au where ar.author_id=au.author_id order by ar.article_modefiedtime limit 0,12' markdown_sql = 'SELECT * FROM article_markdown AS am INNER JOIN author AS au WHERE am.author_id = au.author_id ORDER BY am.create_time limit 0,12' db = get_db() articles = db.execute(article_sql).fetchall() markdowns = db.execute(markdown_sql).fetchall() for arc in articles: item = { 'id': arc['article_id'], 'modefiedtime': arc['article_modefiedtime'], 'tag': arc['article_tag'], 'imgpath': arc['article_imgpath'], 'title': arc['article_title'], 'subtitle': arc['article_subtitle'] } arcList.append(item) for mkitem in markdowns: item = { 'id': mkitem['markdown_id'], 'modefiedtime': mkitem['modefied_time'], 'tag': mkitem['tag'], 'imgpath': 'static/img/mkdown.jpg', 'title': mkitem['title'], 'subtitle': 'markdown need not subtitle' } arcList.append(item) return arcList
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: author_id = str(uuid.uuid1()) db.execute( 'INSERT INTO user (username, password, author_id) VALUES (?, ?, ?)', (username, generate_password_hash(password), author_id)) db.execute( 'INSERT INTO author (author_id,author_name,headimg) VALUES (?, ?, ?)', (author_id, 'xer', '#')) db.commit() LogInfo().logger.info('/auth/register 注册用户:%s' % username) return redirect(url_for('auth.login')) LogInfo().logger.error('/auth/register 发生了错误:%s' % str(error)) flash(error) return render_template('auth/register.html')
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE author_id = ?', (user_id, )).fetchone()
def markdown(id): sql = 'SELECT * FROM article_markdown WHERE markdown_id = ?' LogInfo().logger.info('查询markdown:%s,id:%s' % (sql,id)) markdown = {} result = get_db().execute(sql,(id,)).fetchone() markdown['markdown_id'] = result['markdown_id'] markdown['content'] = result['content'] markdown['title'] = result['title'] markdown['tag'] = result['tag'] return render_template('article/markdown.html',markdown=markdown)
def edit(id=None): article = { 'content':'' } if id != '0' or id == None: db=get_db() sql = 'SELECT * FROM article_markdown WHERE markdown_id=?' article_tuple = db.execute(sql,(id,)).fetchone() LogInfo().logger.info('接收到ID为:%s,sql:%s' % (id,sql)) article['markdown_id'] = article_tuple['markdown_id'] article['content'] = article_tuple['content'] article['title'] = article_tuple['title'] article['tag'] = article_tuple['tag'] return render_template('article/edit.html',article=article)
def index(id): print("Get id %s" % id) db = get_db() sql = "SELECT * FROM article inner join article_content where article.article_id == article_content.article_id and article.article_id = '%s' order by article_index;" % id result = db.execute(sql) article_list = {} content_list = [] articles = result.fetchall() for item in articles: content_list.append(json.loads(str(item['article_value']).replace('\'','\"'))) article_list['introduction'] = articles[0]['article_introduction'] article_list['view_count'] = articles[0]['article_view_count'] article_list['scan_count'] = articles[0]['article_scan_count'] article_list['content_list'] = content_list return render_template('article/index.html',article_list=article_list)
def delete(id): db=get_db() is_markdown_exist = db.execute('SELECT 1 FROM article_markdown WHERE markdown_id = ?',(id,)).fetchone() is_artice_exist = db.execute('SELECT 1 FROM article WHERE article_id = ?',(id,)).fetchone() if is_markdown_exist == None and is_artice_exist == None: LogInfo().logger.info('查不到要删除的Markdown或者article ID:%s') return json.dumps({'status':'查不到要删除的Markdown或者article'}) elif is_markdown_exist != None: LogInfo().logger.info('删除的Markdown ID:%s') sql = 'DELETE FROM article_markdown WHERE markdown_id = ? ' db.execute(sql,(id,)) db.commit() return json.dumps({'status':'markdown delete success!'}) else: LogInfo().logger.info('删除的article ID:%s') sql1 = 'DELETE FROM article_content WHERE article_id = ? ' sql2 = 'DELETE FROM article WHERE article_id = ? ' db.execute(sql1,(id,)) db.execute(sql2,(id,)) db.commit() return json.dumps({'status':'article delete success!'})
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error = 'Incorrect username.(账号不正确)...' elif not check_password_hash(user['password'], password): error = 'Incorrect password.(密码不正确)...' if error is None: session.clear() session['user_id'] = user['author_id'] #,userinfo={ 'username':username,'author_id':author_id } LogInfo().logger.info('/auth/register 用户登录:%s' % username) return redirect(url_for('home.index')) LogInfo().logger.error('/auth/register 发生了错误:%s' % str(error)) flash(error) return render_template('auth/login.html')