def pulish_article(file=None, title=None, desc=None, cls=None, pic=None): sql_db = query.DB(SQLDB_PATH) if hasattr(file, 'file') and hasattr(file, 'filename'): print(file.filename) with open(UPLOAD + file.filename, 'wb') as new_file: new_file.write(file.file.read()) if hasattr(pic, 'file') and hasattr(pic, 'filename'): with open(UPLOAD + 'pic/' + pic.filename, 'wb') as new_pic: new_pic.write(pic.file.read()) if hasattr(file, 'filename'): try: sql_db.insert("blog", (title, file.filename, pic.filename, desc, datetime.datetime.now(), cls)) except sqlite3.OperationalError: sql_db.close() return json.dumps({ "success": "false", "msg": "database write fail!" }) sql_db.close() return json.dumps({"success": "true"}) else: sql_db.close() return json.dumps({"success": "false", "msg": "filename is necessary"})
def get_recent_article(limit=100): sql_db = query.DB(SQLDB_PATH) if limit == '': limit = 100 blog = sql_db.all("blog")[:int(limit)] blog.sort(key=lambda k: k['date'], reverse=True) r = [] for i in wrap_article_result(blog): r.append(i) sql_db.close() return json.dumps({"success": "true", "result": r})
def comment(bobo_request, title=None, content=None, name="anonymous"): sql_db = query.DB(SQLDB_PATH) if not content or not title: return json.dumps({ "success": "false", "msg": "content and title can not be null" }) sql_db.insert('comment', (title, name, datetime.datetime.now(), content, bobo_request.remote_addr)) sql_db.close() return json.dumps({"success": "true"})
def get_cls(): r = [] sql_db = query.DB(SQLDB_PATH) try: for i in sql_db.all("cls "): r.append(i['name']) except: sql_db.close() return json.dumps({"success": "false"}) sql_db.close() return json.dumps({"success": "true", "result": r})
def get_click_article(limit=5): if limit == '': limit = 5 sql_db = query.DB(SQLDB_PATH) click = sql_db.all("click")[:limit] click.sort(key=lambda it: it['number'], reverse=True) r = [] for i in click: d = {} d.update(i) r.append(d) sql_db.close() return json.dumps({"success": "true", "result": r})
def create_cls(cls): """ meta table only has one json object like {"key":"meta","cls":["note"]} """ sql_db = query.DB(SQLDB_PATH) try: sql_db.insert("cls", (cls, )) except: print(traceback.format_exc()) sql_db.close() return json.dumps({"success": "false"}) sql_db.close() return json.dumps({"success": "true"})
def click(title=None): sql_db = query.DB(SQLDB_PATH) db_r = sql_db.search('click', ('blog_title', title)) if title: if len(db_r) == 0: sql_db.insert('click', (title, 1)) else: sql_db.update('click', ('number', ), (db_r[0]['number'] + 1, ), ('blog_title', title)) sql_db.close() return json.dumps({"success": "true"}) else: sql_db.close() return json.dumps({"success": "false", "msg": "title can not be null"})
def get_article(title=None, cls=None): sql_db = query.DB(SQLDB_PATH) blog = [] if title: blog = sql_db.search("blog", ('title', title)) elif cls: blog = sql_db.search("blog", ('cls', cls)) r = [] for i in wrap_article_result(blog): d = {} d.update(i) r.append(d) resp = {"success": "true", "result": r} sql_db.close() return json.dumps(resp)
def show_article(title): sql_db = query.DB(SQLDB_PATH) article = sql_db.search('blog', ('title', title)) if len(article) == 0: sql_db.close() return bobo.redirect('/') else: html = '' with open(cdr + '/template/show.html', 'r', encoding='utf-8') as show: html = show.read() m = re.search('{article_my}', html) html = html[:m.span()[0]] + next(wrap_article_result(article))['content']+\ html[m.span()[1]:] sql_db.close() return html
def del_cls(cls): """ delete a given class """ sql_db = query.DB(SQLDB_PATH) if len(sql_db.search("blog", ("cls", cls))) != 0: sql_db.close() return json.dumps({ "success": "false", "msg": "this class is not null" }) else: sql_db.delete("cls", ("name", cls)) sql_db.close() return json.jumps({"success": "true"})
def get_comment(title=None, limit=None): sql_db = query.DB(SQLDB_PATH) if not title: sql_db.close() return json.dumps({ "success": "false", "msg": "we must have a title to get the comments" }) r = sql_db.search('comment', ('article_title', title)) r = r[:int(limit)] if limit else r result = [] for i in r: d = {} d.update(i) result.append(d) sql_db.close() return json.dumps({"success": "true", "result": result})
def get_access(start=None, end=None): sql_db = query.DB(SQLDB_PATH) if not start and not end: num = len(db.table(name='access').all()) sql_db.close() return json.dumps({"success": "true", "result": num}) elif not end and start: num = len( db.table(name='access').search( tinydb.Query().date == [int(i) for i in start.split('-')])) sql_db.close() return json.dumps({"success": "true", "result": num}) elif end and start: num = len( db.table(name='access').search( (tinydb.Query().date >= [int(i) for i in start.split('-')]) & (tinydb.Query().date <= [int(i) for i in end.split('-')]))) sql_db.close() return json.dumps({"success": "true", "result": num}) else: sql_db.close() return json.dumps({"success": "false", "msg": "date is not illege"})