def init(project_name): """ build a minimal flask project """ # the destination path dst_path = os.path.join(os.getcwd(), project_name) start_init_info(dst_path) # create dst path _mkdir_p(dst_path) os.chdir(dst_path) # create files init_code('manage.py', _manage_basic_code) init_code('requirement.txt', _requirement_code) # create app/ app_path = os.path.join(dst_path, 'app') _mkdir_p(app_path) os.chdir(app_path) # create files init_code('views.py', _views_basic_code) init_code('forms.py', _forms_basic_code) init_code('__init__.py', _init_basic_code) create_templates_static_files(app_path) init_done_info()
def blueprint(blueprint_name): """ create and register a blueprint """ app = os.getcwd().split('/')[-1] if app != 'app': logger.warning('''\033[31m{Warning}\033[0m ==> your current path is \033[32m%s\033[0m\n ==> please create your blueprint under app folder!''' % os.getcwd()) exit(1) # destination path dst_path = os.path.join(os.getcwd(), blueprint_name) if os.path.isdir(dst_path): logger.warning('''\033[31m{Warning}\033[0m ==> bluprint \033[32m%s\033[0m\n exist ==> please try again !''' % dst_path) exit(1) # create dst_path _mkdir_p(dst_path) # change dir os.chdir(dst_path) # create files init_code('__init__.py', _init_blueprint_code % (blueprint_name, blueprint_name)) init_code('views.py', _views_blueprint_code % (blueprint_name, blueprint_name)) init_code('forms.py', _forms_basic_code) # register auth in app os.chdir(os.path.join(dst_path, '..')) with open('__init__.py', 'r+') as f: prev = pos = 0 while f.readline(): prev, pos = pos, f.tell() f.seek(prev) f.write( '\nfrom %s import %s\napp.register_blueprint(%s, url_prefix="/%s")\n\n' % (blueprint_name, blueprint_name, blueprint_name, blueprint_name)) # create blueprint templates templates_path = os.path.join(os.getcwd(), 'templates') os.chdir(templates_path) blueprint_templates_path = os.path.join(templates_path, blueprint_name) _mkdir_p(blueprint_templates_path) logger.info('''\033[33m{Info}\033[0m: create blueprint done!''')
def create_templates_static_files(app_path): """ create templates and static """ templates_path = os.path.join(app_path, 'templates') static_path = os.path.join(app_path, 'static') _mkdir_p(templates_path) _mkdir_p(static_path) # create {img, css, js} os.chdir(static_path) img_path = os.path.join(static_path, 'img') css_path = os.path.join(static_path, 'css') js_path = os.path.join(static_path, 'js') _mkdir_p(img_path) _mkdir_p(css_path) _mkdir_p(js_path) return css_path, templates_path
def create_blueprint(app_path, blueprint, views_code, forms_code, templates_path): """ create blueprint """ blueprint_path = os.path.join(app_path, blueprint) _mkdir_p(blueprint_path) # create blueprint files os.chdir(blueprint_path) init_code('__init__.py', _init_blueprint_code % (blueprint, blueprint)) init_code('views.py', views_code) init_code('forms.py', forms_code) # main blueprint templates os.chdir(templates_path) blueprint_templates_path = os.path.join(templates_path, blueprint) _mkdir_p(blueprint_templates_path) return blueprint_templates_path
def startproject(project_name): """ build a full status project """ # the destination path dst_path = os.path.join(os.getcwd(), project_name) start_init_info(dst_path) # create dst path _mkdir_p(dst_path) # create project tree os.chdir(dst_path) # create files init_code('manage.py', _manage_admin_code) init_code('requirement.txt', _requirement_admin_code) init_code('config.py', _config_sql_code) # create app/ app_path = os.path.join(dst_path, 'app') _mkdir_p(app_path) # create files os.chdir(app_path) init_code('models.py', _models_admin_code) init_code('__init__.py', _init_admin_code) # create templates and static css_path, templates_path = create_templates_static_files(app_path) # create css files os.chdir(css_path) init_code('sign.css', _auth_login_css_code) # create main blueprint create_blueprint(app_path, 'main', _views_blueprint_code % ('main', 'main'), _forms_basic_code, templates_path) # create auth blueprint auth_templates_path = create_blueprint(app_path, 'auth', _auth_views_code, _auth_forms_code, templates_path) # create auth templates files os.chdir(auth_templates_path) init_code('login.html', _auth_login_html_code) # create admin site admin_path = os.path.join(app_path, 'admin') _mkdir_p(admin_path) # create admin files os.chdir(admin_path) init_code('__init__.py', '') init_code('views.py', _admin_views_code) # create admin templates os.chdir(templates_path) admin_templates_path = os.path.join(templates_path, 'admin') _mkdir_p(admin_templates_path) # create admin templates files os.chdir(admin_templates_path) init_code('index.html', _admin_index_html_code) init_code('logout.html', _admin_logout_html_code) init_done_info()