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 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 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 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 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 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 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')
def index(): arcList = get_article_list() LogInfo().logger.info('/ 获取到所有数据') return render_template('home/index.html', arcList=arcList)
def ex_handle(*args, **kargs): try: return view(*args, **kargs) except Exception as e: LogInfo().logger.error('An exception happenning: %s' % str(e))
#config = None: print(os.path.abspath(os.path.join('.', 'instance'))) #logging.basicConfig(filename="./log/log.txt",format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') print(app.instance_path.replace('-', '/')) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(os.path.abspath(os.path.join('.', 'instance')), 'xeekweb.sqlite.db'), ) # if config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) # else: # # load the test config if passed in # app.config.from_mapping(config) try: os.makedirs(app.instance_path) except OSError: pass app.add_url_rule('/', 'home.index') app.register_blueprint(home.bp) app.register_blueprint(article.bp) app.register_blueprint(auth.bp) #app.add_url_rule('/index', endpoint='index2',view_func = index.index) app.register_blueprint(index.bp) LogInfo().init_log(app)