def create_post(): if request.json is None: abort(400) title = str(request.json["title"]).strip() name = str(request.json["name"]).replace('/', "").strip() content = str(request.json["content"]) sign = str(request.json["sign"]) send_time = int(request.json["send_time"]) status = False hash_content = title + name + hashlib.sha512( str(content).encode('utf-8')).hexdigest() post_uuid = "" if check_password(hash_content, sign, send_time): if len(name) == 0: pinyin = True if 'Pinyin' in system_config: pinyin = system_config["Pinyin"] name = get.get_name(title, pinyin) while os.path.exists("./document/{0}.md".format(name)): name = "{}-repeat".format(name) post_uuid = str(uuid.uuid5(uuid.NAMESPACE_URL, name)) file.write_file("./document/{0}.md".format(name), content) config = {"title": title, "name": name, "uuid": post_uuid} post_manage.new_post(config) status = True build_rss.build_rss() result = {"status": status, "name": name, "uuid": post_uuid} return jsonify(result)
def edit(request_type): file_url = select_type(request_type) is_menu = False if request_type == "menu": is_menu = True if file_url is None: abort(404) if request.json is None: abort(400) page_list = json.loads(file.read_file(file_url)) post_uuid = str(request.json["post_uuid"]) post_index = get_index(page_list, post_uuid) name = str(request.json["name"]).replace('/', "").strip() title = str(request.json["title"]).strip() content = str(request.json["content"]) sign = str(request.json["sign"]) send_time = int(request.json["send_time"]) status = False hash_content = post_uuid + title + name + hashlib.sha512( str(content).encode('utf-8')).hexdigest() if check_password(hash_content, sign, send_time): status = True config = {"name": name, "title": title} post_manage.edit_post(page_list, post_index, config, None, is_menu) file.write_file("./document/{0}.md".format(name), content) post_manage.update_post() build_rss.build_rss() result = {"status": status, "name": name} return jsonify(result)
def edit(request_type): file_url = select_type(request_type) is_menu = False if request_type == "menu": is_menu = True if file_url is None: abort(404) if request.json is None: abort(400) page_list = json.loads(file.read_file(file_url)) post_id = int(request.json["post_id"]) name = str(request.json["name"]).replace('/', "").strip() title = str(request.json["title"]).strip() content = str(request.json["content"]) sign = str(request.json["sign"]) status = False if page_list[post_id].get("lock", False): return json.dumps({"status": False}) if check_password(title, sign): status = True config = {"name": name, "title": title} post_manage.edit_post(page_list, post_id, config, None, is_menu) file.write_file("./document/{0}.md".format(name), content) post_manage.update_post() build_rss.build_rss() return json.dumps({"status": status, "name": name})
def use_text_mode(args): if args.command == "qrcode": system_config = json.loads(file.read_file("./config/system.json")) from common import install_module install_module.install_and_import("qrcode_terminal") import qrcode_terminal if len(system_config["API_Password"]) == 0 or len(system_config["Project_URL"]) == 0: console.log("Error", "Check the API_Password and Project_URL configuration items") exit(1) try: password_md5 = json.loads(system_config["API_Password"])["hash_password"] except (ValueError, KeyError, TypeError): exit(1) console.log("Info", "Please use the client to scan the following QR Code") config_json = json.dumps({"url": system_config["Project_URL"], "password": password_md5}) qrcode_terminal.draw(config_json) exit(0) if args.command == "upgrade": from manage import upgrade if upgrade.upgrade_check(): if not args.yes: start_to_pull = input('Find new version, do you want to upgrade? [y/N]') if start_to_pull.lower() == 'yes' or start_to_pull.lower() == 'y' or args.yes: upgrade.upgrade_pull() exit(0) console.log("Info", "No upgrade found") exit(0) if args.command == "build-page": from manage import build_static_page build_static_page.publish() exit(0) from manage import build_rss, post_manage if args.command == "new": config = None if args.config is not None: config = json.loads(file.read_file(args.config)) if config is None: print("Please enter the title of the article:") title = input().strip() if len(title) == 0: console.log("Error", "The title can not be blank.") exit(1) name = get.get_name(title) print("Please enter the slug [{}]:".format(name)) name2 = input().strip() if len(name2) != 0: name = get.filter_name(name2) if os.path.exists("./document/{}.md".format(name)): console.log("Error", "File [./document/{}.md] already exists".format(name)) exit(1) if len(name) != 0 and len(title) != 0: config = {"title": title, "name": name} post_manage.new_post(config, args.independent) build_rss.build_rss() exit(0) if args.command == "update": if post_manage.update_post(): build_rss.build_rss() exit(0)
def delete(): if request.json is None: abort(400) page_list = json.loads(file.read_file("./config/page.json")) post_id = int(request.json["post_id"]) sign = str(request.json["sign"]) status = False if page_list[post_id].get("lock", False): return json.dumps({"status": False}) if check_password(str(post_id) + page_list[post_id]["title"], sign): status = True post_manage.delete_post(page_list, post_id) build_rss.build_rss() return json.dumps({"status": status})
def delete(): if request.json is None: abort(400) page_list = json.loads(file.read_file("./config/page.json")) post_uuid = str(request.json["post_uuid"]) post_index = get_index(page_list, post_uuid) sign = str(request.json["sign"]) send_time = int(request.json["send_time"]) status = False hash_content = page_list[post_index]["uuid"] + page_list[post_index][ "title"] + page_list[post_index]["name"] if check_password(hash_content, sign, send_time): status = True post_manage.delete_post(page_list, post_index) build_rss.build_rss() result = {"status": status} return jsonify(result)
def article_manager(): dialog.title = "Article manager" from manage import build_rss, update_post menu_list = ["New", "Update", "Edit", "Delete"] result = dialog.menu("Please select an action", menu_list) if result == "New": new_post() if result == "Edit": edit_post() if not update_post.update(): return if result == "Delete": delete_post() if result == "Update": if not update_post.update(): return build_rss.build_rss()
def article_manager(): from manage import build_rss, post_manage while True: dialog.title = "Article manager" menu_list = [ "New", "Update", "Edit", "Delete", "=" * 25, "Back", "Exit" ] result = dialog.menu("Please select an action", menu_list) if result == "Exit": exit(0) if result == "Back": break if result == "New": dialog.title = "New post" post_info = get_post_info() if os.path.exists("./document/{}.md".format(post_info["name"])): console.log( "Error", "File [./document/{}.md] already exists".format( post_info["name"])) exit(1) if post_info["name"] is not None: post_manage.new_post( post_info, dialog.confirm("Is this an independent page?", "no")) if result == "Edit": dialog.title = "Edit post" page_list, post_index = select_list("./config/page.json") if page_list: config = get_post_info(page_list[post_index]["title"], page_list[post_index]["name"], page_list[post_index]["time"]) system_info = json.loads( file.read_file("./config/system.json")) post_manage.edit_post(page_list, post_index, config, system_info["Editor"]) post_manage.update_post() if result == "Delete": page_list, post_index = select_list("./config/page.json") if page_list and dialog.confirm( "Are you sure you want to delete this article?", "no"): post_manage.delete_post(page_list, post_index) if result == "Update": post_manage.update_post() build_rss.build_rss() time.sleep(0.5)
def create_post(): if request.json is None: abort(400) title = str(request.json["title"]).strip() name = str(request.json["name"]).replace('/', "").strip() content = str(request.json["content"]) sign = str(request.json["sign"]) status = False if len(name) == 0: name = get.get_name(title) while os.path.exists("./document/{0}.md".format(name)): name = "{}-repeat".format(name) if check_password(title, sign): file.write_file("./document/{0}.md".format(name), content) config = {"title": title, "name": name} post_manage.new_post(config) status = True build_rss.build_rss() return json.dumps({"status": status, "name": name})
def use_text_mode(args): if args.command == "qrcode": system_config = json.loads(file.read_file("./config/system.json")) try: import qrcode_terminal except ImportError: console.log( "Error", "Please install the qrcode-terminal package to support this feature" ) install_dependency = input( 'Do you want to install qrcode-terminal now? [y/N]') if install_dependency.lower() == 'yes' or install_dependency.lower( ) == 'y': import os os.system("python3 -m pip install qrcode-terminal") if len(system_config["API_Password"]) == 0 or len( system_config["Project_URL"]) == 0: console.log( "Error", "Check the API_Password and Project_URL configuration items") exit(1) try: password_md5 = json.loads( system_config["API_Password"])["hash_password"] except (ValueError, KeyError, TypeError): exit(1) console.log("Info", "Please use the client to scan the following QR Code") config_json = json.dumps({ "url": system_config["Project_URL"], "password": password_md5 }) qrcode_terminal.draw(config_json) exit(0) if args.command == "upgrade": from manage import upgrade if upgrade.upgrade_check(): start_to_pull = input( 'Find new version, do you want to upgrade? [y/N]') if start_to_pull.lower() == 'yes' or start_to_pull.lower() == 'y': upgrade.upgrade_pull() exit(0) console.log("info", "No upgrade found") exit(0) if args.command == "build-page": from manage import build_static_page build_static_page.publish() exit(0) from manage import build_rss if args.command == "new": from manage import new_post config = None if args.config is not None: config = json.loads(file.read_file(args.config)) if config is None: print("Please enter the title of the article:") title = input() if len(title) != 0: name = new_post.get_name(title) print("Please enter the slug [{}]:".format(name)) name2 = input() if len(name2) != 0: name = name2 if len(name) != 0 and len(title) != 0: config = {"title": title, "name": name} new_post.new_post_init(config, args.independent) build_rss.build_rss() exit(0) if args.command == "update": from manage import update_post if update_post.update(): build_rss.build_rss() exit(0)
def publish(): load_config() if os.path.isdir("./static_page"): os.system( "cd ./static_page && rm -r static post index && rm *.html *.xml") if not os.path.isdir("./static_page"): os.mkdir("./static_page") page_row = page.get_page_row(system_config["Paging"], len(page_list)) os.mkdir("./static_page/index/") console.log("Build", "Processing file: ./static_page/index.html") content = page.build_index(1, page_row, system_config, page_list, menu_list, template_config, i18n) file.write_file("./static_page/index.html", content) if page_row != 1: file.write_file("./static_page/index/1.html", "<meta http-equiv='refresh' content='0.1; url=/'>") for page_id in range(2, page_row + 1): console.log( "Build", "Processing file: ./static_page/index/{0}.html".format( str(page_id))) content = page.build_index(page_id, page_row, system_config, page_list, menu_list, template_config, i18n) file.write_file( "./static_page/index/{0}.html".format(str(page_id)), content) os.mkdir("./static_page/post/") loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) tasks = [ build_post_page(filename, page_name_list, page_list, system_config, menu_list, template_config, i18n) for filename in glob.glob("./document/*.md") ] if len(tasks) != 0: loop.run_until_complete(asyncio.wait(tasks)) loop.close() if not os.path.exists("./document/rss.xml"): from manage import build_rss build_rss.build_rss() shutil.copyfile("./document/rss.xml", "./static_page/rss.xml") shutil.copytree("./templates/{0}/static".format(system_config["Theme"]), "./static_page/static/{0}/".format(system_config["Theme"])) if os.path.exists("./templates/static/user_file"): shutil.copytree("./templates/static/user_file", "./static_page/static/user_file") console.log("Success", "Create page success!") if not os.path.exists("./static_page/.git"): return False import git localtime = time.asctime(time.localtime(time.time())) try: repo = git.Repo("./static_page") repo.git.add("--all") if not repo.is_dirty(): console.log("Success", "Build complete,No changes found.") return False repo.git.commit("-m Release timeļ¼{0}".format(localtime)) except git.exc.GitCommandError as e: console.log("Error", e.args[2].decode()) return False console.log("Info", "Push to the remote.") remote = repo.remote() remote.push() console.log("Success", "Push to the remote success.") return True
if config is None: print("Please enter the title of the article:") title = input().strip() if len(title) == 0: console.log("Error", "The title can not be blank.") exit(1) name = get.get_name(title, system_config["Pinyin"]) print("Please enter the slug [{}]:".format(name)) name2 = input().strip() if len(name2) != 0: name = get.filter_name(name2) if os.path.exists("./document/{}.md".format(name)): console.log( "Error", "File [./document/{}.md] already exists".format(name)) exit(1) if len(name) != 0 and len(title) != 0: config = {"title": title, "name": name} post_manage.new_post(config, args.independent) build_rss.build_rss() exit(0) if args.command == "update": if post_manage.update_post(): build_rss.build_rss() exit(0) except KeyboardInterrupt: print("User cancelled operation.") exit(0) parser.print_help()