From 67ca110bea11f0748f5311603529e0abb38198bf Mon Sep 17 00:00:00 2001 From: AlexandrovRoman <56388249+AlexandrovRoman@users.noreply.github.com> Date: Sun, 15 Mar 2020 12:56:01 +0400 Subject: [PATCH] Update manage.py startapp --- .gitignore | 2 ++ Flask_DJ/__project_info__.py | 2 +- Flask_DJ/manage.py | 28 +++++++++++++++++------ Flask_DJ/project_creator.py | 38 ++++++++++++-------------------- Flask_DJ/templates.py | 5 +++-- Flask_DJ/utils.py | 7 ++++++ README.md | 3 +++ docs/lib_docs/helloworld.rst | 7 ++++++ docs/lib_docs/manage_methods.rst | 3 +++ setup.py | 18 +++++++-------- 10 files changed, 70 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 9f24df1..581c1a2 100644 --- a/.gitignore +++ b/.gitignore @@ -132,3 +132,5 @@ test_project/ # Habr post habr.md + +upload.bat diff --git a/Flask_DJ/__project_info__.py b/Flask_DJ/__project_info__.py index 408c83b..dcb0576 100644 --- a/Flask_DJ/__project_info__.py +++ b/Flask_DJ/__project_info__.py @@ -1,7 +1,7 @@ __title__ = 'Flask-DJ' __description__ = 'Django (mvc) structure for your Flask project.' __url__ = 'https://github.com/AlexandrovRoman/Flask-DJ' -__version__ = '0.1.6' +__version__ = '0.1.7' __author__ = 'Alexandrov Roman' __author_email__ = 'flask-dj.feedback@yandex.com' __license__ = 'MIT License' diff --git a/Flask_DJ/manage.py b/Flask_DJ/manage.py index 971e52d..c4c3e44 100644 --- a/Flask_DJ/manage.py +++ b/Flask_DJ/manage.py @@ -3,14 +3,15 @@ from importlib import import_module from Flask_DJ.templates import views_file, models_file, urls_file, forms_file from waitress import serve -from .utils import get_project_name, create_folder, create_file +from os.path import join +from .utils import get_project_name, create_folder, create_file, valid_folder_name """database-methods: https://flask-migrate.readthedocs.io/en/latest/""" manager = None app_ = None commands = [ - 'startapp', + ] @@ -33,6 +34,18 @@ def runserver(host, port): serve(app_, host=host, port=port) +def startapp(need_templates, need_static, name): + """Create folder containing forms.py, models.py, urls.py, views.py""" + valid_folder_name(name) + create_folder(name) + create_app_files(name) + if need_templates: + create_app_templates(name) + if need_static: + create_app_static(name) + print(f'app {name} created') + + def create_app_files(app_name): project_name = get_project_name() create_file(app_name, 'views', views_file) @@ -41,8 +54,9 @@ def create_app_files(app_name): create_file(app_name, 'forms', forms_file) -def startapp(name): - """Create folder containing forms.py, models.py, urls.py, views.py""" - create_folder(name) - create_app_files(name) - print(f'app {name} created') +def create_app_templates(app_name): + create_folder(join("templates", app_name)) + + +def create_app_static(app_name): + create_folder(join("static", app_name)) diff --git a/Flask_DJ/project_creator.py b/Flask_DJ/project_creator.py index cb616f5..9de0d83 100644 --- a/Flask_DJ/project_creator.py +++ b/Flask_DJ/project_creator.py @@ -1,9 +1,10 @@ +from argparse import ArgumentParser from Flask_DJ.templates import config_file, init_file, urls_file, manage_file, utils_urls -from string import ascii_letters, digits +from string import ascii_letters from random import choices, randint from sys import argv from os.path import join -from .utils import create_file, create_folder +from .utils import create_file, create_folder, valid_folder_name class ProjectConstructor: @@ -16,29 +17,12 @@ class ProjectConstructor: """ def __init__(self, project_name=None, path='', need_templates=False, need_static=False): self.path = path - self.project_name = self._get_project_name(project_name) - self._valid_project_name() + self.project_name = project_name + valid_folder_name(project_name) self.project_path = join(self.path, self.project_name) if self.path else self.project_name self.main_app_path = join(self.project_path, self.project_name) - self.need_templates = self._get_flag_value('-t', need_templates) - self.need_static = self._get_flag_value('-st', need_static) - - def _valid_project_name(self): - if set(self.project_name).issubset(set(ascii_letters + digits + "_")) and self.project_name[0] not in digits: - return - raise ValueError("Invalid project name") - - @staticmethod - def _get_project_name(project_name): - try: - return project_name or argv[2] - except IndexError: - print(argv) - raise ValueError("project_name is not defined") - - @staticmethod - def _get_flag_value(flag: str, default=False) -> bool: - return flag in argv or default + self.need_templates = need_templates + self.need_static = need_static def startproject(self): self._create_project_folder() @@ -99,7 +83,13 @@ def _create_static(self): def console_creation(): - ProjectConstructor().startproject() + parser = ArgumentParser() + parser.add_argument("command") + parser.add_argument("project_name") + parser.add_argument("--templates", "-t", action="store_true", default=False) + parser.add_argument("--static", "-st", action="store_true", default=False) + args = parser.parse_args() + ProjectConstructor(args.project_name, need_templates=args.templates, need_static=args.static).startproject() def command(): diff --git a/Flask_DJ/templates.py b/Flask_DJ/templates.py index 24e77df..a503e69 100644 --- a/Flask_DJ/templates.py +++ b/Flask_DJ/templates.py @@ -57,8 +57,9 @@ class DevelopConfig(BaseConfig): manage.init_manage(app) manage.init_db(config.models) -for command in manage.commands: - setattr(manage, command, manage.manager.command(getattr(manage, command))) + +manager.option("--templates", "-t", action="store_true")( + manager.option("--static", "-st", action="store_true")(manager.option("name")(manage.manager.startapp))) @manage.manager.command diff --git a/Flask_DJ/utils.py b/Flask_DJ/utils.py index bfb270c..20ca049 100644 --- a/Flask_DJ/utils.py +++ b/Flask_DJ/utils.py @@ -1,5 +1,6 @@ from os.path import join, split from os import makedirs, getcwd +from string import ascii_letters, digits from Flask_DJ.exceptions import CreationError @@ -20,3 +21,9 @@ def create_folder(path): def get_project_name(): return split(getcwd())[-1] + + +def valid_folder_name(name): + if set(name).issubset(set(ascii_letters + digits + "_")) and name[0] not in digits: + return + raise ValueError("Invalid name") diff --git a/README.md b/README.md index 91e9ba7..f9487fe 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +![image](docs/_static/flask-dj_logo.png) + + # Flask-DJ Since the flask has no strict architecture,
diff --git a/docs/lib_docs/helloworld.rst b/docs/lib_docs/helloworld.rst index bb1a8a3..390ada5 100644 --- a/docs/lib_docs/helloworld.rst +++ b/docs/lib_docs/helloworld.rst @@ -10,6 +10,13 @@ If your project need templates and static files $ flask-dj startproject app -t -st +or + +.. code-block:: shell + + $ flask-dj startproject app --templates --static + + If something went wrong .. code-block:: python diff --git a/docs/lib_docs/manage_methods.rst b/docs/lib_docs/manage_methods.rst index bd39048..36aa5e7 100644 --- a/docs/lib_docs/manage_methods.rst +++ b/docs/lib_docs/manage_methods.rst @@ -3,6 +3,9 @@ Manage: startapp {name} ~~~~~~~~~~~~~~~~~~~ +Flags: -t (--templates) create app folder in templates +-st (--static) create app folder in static + Creates a folder {name} with forms, models, urls, views .py files runserver diff --git a/setup.py b/setup.py index 4d9bcce..50cf8ca 100644 --- a/setup.py +++ b/setup.py @@ -31,15 +31,15 @@ keywords=__keywords__, # Optional packages=find_packages(), # Required install_requires=[ - 'waitress==1.4.3', - 'flask==1.1.1', - 'Flask-Migrate==2.5.2', - 'Flask-Script==2.0.6', - 'Flask-SQLAlchemy==2.4.1', - 'WTForms==2.2.1', - 'SQLAlchemy==1.3.13', - 'Flask-WTF==0.14.3', - 'Flask-Login==0.5.0' + 'waitress', + 'flask', + 'Flask-Migrate', + 'Flask-Script', + 'Flask-SQLAlchemy', + 'WTForms', + 'SQLAlchemy', + 'Flask-WTF', + 'Flask-Login' ], entry_points={ 'console_scripts': [