Skip to content

Commit

Permalink
Update manage.py startapp
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrovRoman committed Mar 15, 2020
1 parent 24c2ed7 commit 67ca110
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@ test_project/

# Habr post
habr.md

upload.bat
2 changes: 1 addition & 1 deletion Flask_DJ/__project_info__.py
Original file line number Diff line number Diff line change
@@ -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__ = '[email protected]'
__license__ = 'MIT License'
Expand Down
28 changes: 21 additions & 7 deletions Flask_DJ/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',

]


Expand All @@ -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)
Expand All @@ -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))
38 changes: 14 additions & 24 deletions Flask_DJ/project_creator.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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():
Expand Down
5 changes: 3 additions & 2 deletions Flask_DJ/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Flask_DJ/utils.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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")
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
![image](docs/_static/flask-dj_logo.png)


# Flask-DJ

Since the flask has no strict architecture, <br>
Expand Down
7 changes: 7 additions & 0 deletions docs/lib_docs/helloworld.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/lib_docs/manage_methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down

0 comments on commit 67ca110

Please sign in to comment.