Skip to content

Commit

Permalink
Merge pull request #3 from mnaile/master
Browse files Browse the repository at this point in the history
added tests modified structure
  • Loading branch information
sabuhish authored Dec 11, 2020
2 parents 23cfe33 + ef50311 commit ee876c3
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ __pycache__/
# C extensions
*.so
.vscode/

mytest.py
# Distribution / packaging
.Python
build/
Expand Down
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ startapp --help
```
- Type startapp on terminal press enter, questions will promt on terminal choose accroding to your taste.

[![asciicast](https://asciinema.org/a/PuyuQDl1R1OrRkGWU6t5SblHo.svg)](https://asciinema.org/a/PuyuQDl1R1OrRkGWU6t5SblHo)


```bash
Expand Down Expand Up @@ -66,6 +65,87 @@ export settings=dev

flask run

```
Flask Structure

```bash
├── app
│   ├── controllers
│   │   ├── app.py
│   │   └── __init__.py
│   ├── __init__.py
│   ├── models
│   │   ├── __init__.py
│   │   └── models.py
│   ├── serializers
│   │   ├── __init__.py
│   │   └── serializer.py
│   └── utils.py
├── app_init
│   ├── app_factory.py
│   ├── __init__.py
│  
│  
│  
├── extensions
│   ├── db_conf.py
│   ├── extension.py
│   ├── __init__.py
│  
│  
│  
├── prestart.sh
│  
├── README.md
├── requirements.txt
├── server.py
├── settings
│   ├── devsettings.py
│   ├── prodsettings.py
│   ├── settings.py
│   └── testsettings.py
└── tests
├── __init__.py
└── test.py

```

FastApi Structure
```bash
├── app
│   ├── controllers
│   │   ├── controller
│   │   │   ├── controller.py
│   │   │   └── schemas.py
│   │   └── __init__.py
│   ├── data
│   │   ├── __init__.py
│   │   └── models.py
│   ├── __init__.py
│   ├── main.py
│   └── utils
│   ├── helpers.py
│   └── __init__.py
├── container.sh
├── core
│   ├── dbsetup.py
│   ├── extensions.py
│   ├── factories.py
│   ├── __init__.py
│   └── settings
│   ├── devsettings.py
│   ├── __init__.py
│   ├── prodsettings.py
│   └── settings.py
├── Dockerfile
├── prestart.sh
├── README.md
├── req.txt
├── requirements.txt
└── start.sh


```

## Supported OS
Expand Down
43 changes: 37 additions & 6 deletions takeaway/controllers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
from takeaway.settings.fastapi.schemas import schema
from takeaway.settings.fastapi.settings import setting, devsetting, prodsettings

from takeaway.settings.flask.settings import flask_setting, dev_settings, prod_settings
from takeaway.settings.flask.settings import flask_setting, dev_settings, prod_settings, test_settings
from takeaway.settings.flask.app import app
from takeaway.settings.flask.extension import flask_extension
from takeaway.settings.flask.db_conf import db_conf
from takeaway.settings.flask.models import flask_model
from takeaway.settings.flask.readme import flask_readme
from takeaway.settings.flask.requirements import req
from takeaway.settings.flask.serializer import serilizer
from takeaway.settings.flask.utils import utils
from takeaway.settings.flask.app_factory import factory
from takeaway.settings.flask.test import test
from takeaway.settings.flask.env import env
from takeaway.settings.flask.server import server


# from .commands import Operation
Expand Down Expand Up @@ -173,16 +177,24 @@ def install_dependencies(self):


class FlaskApp:
def __init__(self, app, folder_name, db_driver, db_name,git_repo):
def __init__(self, app, folder_name, db_driver, db_name, testdb_name, git_repo):
self.app = app
self.folder_name = folder_name
self.root_directory = f"{self.folder_name}/"

self.git_repo = git_repo
self.app_directory = f"{self.root_directory}app"
self.app_directory = f"{self.root_directory}app/"

self.controllers_directory = f"{self.app_directory}controllers"
self.models_directory = f"{self.app_directory}models"
self.serializers_directory = f"{self.app_directory}serializers"

self.tests = f"{self.root_directory}tests"

self.settings = f"{self.root_directory}settings"
self.db_driver = db_driver
self.db_name = db_name
self.testdb_name = testdb_name
self.extensions = f"{self.root_directory}extensions"
self.app_init = f"{self.root_directory}app_init"

Expand All @@ -206,13 +218,21 @@ def create_app_structure(self):
self.root_folder_create()

os.makedirs(self.app_directory)
os.makedirs(self.controllers_directory)
os.makedirs(self.models_directory)
os.makedirs(self.serializers_directory)
os.makedirs(self.tests)
os.makedirs(self.settings)
os.makedirs(self.extensions)
os.makedirs(self.app_init)

self.create_init_file(self.app_directory)
self.create_init_file(self.app_init)
self.create_init_file(self.extensions)
self.create_init_file(self.controllers_directory)
self.create_init_file(self.models_directory)
self.create_init_file(self.serializers_directory)
self.create_init_file(self.tests)

self.file_create(self.root_directory, ".gitignore", gitignore)
self.file_create(self.root_directory, "README.md", flask_readme)
Expand All @@ -226,12 +246,16 @@ def create_app_structure(self):
),
)

self.file_create(self.app_directory, "app.py", app)
self.file_create(self.app_directory, "models.py", flask_model)
self.file_create(self.app_directory, "serializer.py", serilizer)
self.file_create(self.controllers_directory, "app.py", app)
self.file_create(self.models_directory, "models.py", flask_model)
self.file_create(self.serializers_directory, "serializer.py", serilizer)
self.file_create(self.app_directory, "utils.py", utils)
self.file_create(self.app_init, "app_factory.py", factory)
self.file_create(self.extensions, "extension.py", flask_extension)
self.file_create(self.extensions, "db_conf.py", db_conf)
self.file_create(self.tests, "test.py", test )
self.file_create(self.root_directory, ".env", env)
self.file_create(self.root_directory, "server.py", server)
self.file_create(
self.root_directory,
"prestart.sh",
Expand All @@ -251,6 +275,13 @@ def create_app_structure(self):
"prodsettings.py",
prod_settings.replace("DB_DRIVER", self.db_driver),
)
self.file_create(
self.settings,
"testsettings.py",
test_settings.replace("DB_DRIVER", self.db_driver).replace(
"unittest_db" , self.testdb_name
),
)
self.file_create(self.settings, "settings.py", flask_setting)

self.virtaul_env()
Expand Down
5 changes: 3 additions & 2 deletions takeaway/controllers/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@


class Operation:
def __init__(self, app, folder_name, db_driver, db_name,git_repo):
def __init__(self, app, folder_name, db_driver, db_name, testdb_name,git_repo):
self.app = app
self.folder_name = folder_name
self.program = "takeaway"
self.db_driver = db_driver
self.db_name = db_name
self.testdb_name = testdb_name
self.git_repo = git_repo
self.issue_url = "https://github.com/marlin-dev/startapp"
self.PR = "https://github.com/marlin-dev/startapp/pulls"
Expand Down Expand Up @@ -58,6 +59,6 @@ def execute(self):
"""Main execution function"""
self.check_file()

comand = BaseComand(self.app, self.folder_name, self.db_driver, self.db_name,self.git_repo)
comand = BaseComand(self.app, self.folder_name, self.db_driver, self.db_name, self.testdb_name, self.git_repo)
comand.start()
self.thanks_using()
5 changes: 3 additions & 2 deletions takeaway/controllers/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@


class BaseComand:
def __init__(self, app, folder_name, db_driver, db_name,git_repo):
def __init__(self, app, folder_name, db_driver, db_name, testdb_name, git_repo):
self.app = app
self.folder_name = folder_name
self.db_driver = db_driver
self.db_name = db_name
self.testdb_name = testdb_name
self.git_repo = git_repo

def start(self):
Expand All @@ -18,5 +19,5 @@ def start(self):

elif self.app == "flask":

app = FlaskApp(self.app, self.folder_name, self.db_driver, self.db_name,self.git_repo)
app = FlaskApp(self.app, self.folder_name, self.db_driver, self.db_name, self.testdb_name, self.git_repo)
app.start()
5 changes: 3 additions & 2 deletions takeaway/script/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def clear():
@click.option('--db_driver','-dd',type=click.Choice(['mysql', 'postgresql'], case_sensitive=True), prompt='Choose one of the database drivers',show_default=True,default='postgresql',required=True, help='the db driver name')
@click.option('--name',"-n",metavar='the name of yor app',prompt='Name for your app',show_default=True,default='myproject',required=True, help='the app name')
@click.option('--db_name',"-d",metavar='the name of yor db',prompt='Name for your db',show_default=True,default='testdb',required=True, help='the db name')
@click.option('--testdb_name', "-td", metavar='the name of yor testdb', prompt='Name for your testdb', show_default=True, default='unittestdb', required=True, help='the testdb name')
@click.version_option(VERSION)
def cli(app,name,db_driver,db_name,count=4000):
def cli(app,name,db_driver,db_name,testdb_name, count=4000):
click.echo(click.style('Starting app %s at directory %s!' % (app,name), fg='green'))
click.progressbar(iterable="8", length=None, label=None, show_eta=True, show_percent=None, show_pos=False, item_show_func=None, fill_char='#', empty_char='-', bar_template='%(label)s [%(bar)s] %(info)s', info_sep=' ', width=36, file=None, color=None)
items = range(count)
Expand All @@ -50,7 +51,7 @@ def filter(items):



management = Operation(app,name,db_driver,db_name,git_repo)
management = Operation(app,name,db_driver,db_name,testdb_name,git_repo)
management.execute()


Expand Down
50 changes: 50 additions & 0 deletions takeaway/settings/flask/db_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
db_conf = """
from flask import current_app
from extensions.extensions import db
from sqlalchemy.dialects.postgresql import UUID
from uuid import uuid4
from datetime import datetime
from sqlalchemy.orm import relationship
Integer, Column, String, DateTime, Boolean, Date, relationship, ForeignKey = db.Integer, db.Column, db.String, db.DateTime, db.Boolean, db.Date, db.relationship, db.ForeignKey
class Operation:
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
created_at = Column(DateTime(timezone=True), default=datetime.utcnow, server_default=db.func.now())
updated_at = Column(DateTime(timezone=True),default=datetime.utcnow,onupdate=datetime.utcnow,server_default=db.func.now())
class Model(db.Model, Operation):
__abstract__= True
def save_db(self):
try:
db.session.add(self)
db.session.commit()
return self
except Exception as err:
current_app.logger.error(err)
return False
def delete_from_db(self):
try:
db.session.delete(self)
db.session.commit()
return self
except Exception as err:
current_app.logger.error(err)
return False
def update_db(self, **kwargs):
try:
for key, val in kwargs.items():
setattr(self, key, val)
self.save_db()
except Exception as err:
current_app.logger.error(err)
return False
"""
10 changes: 10 additions & 0 deletions takeaway/settings/flask/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
env = """
export FLASK_APP=server
export FLASK_ENV=development
export settings=dev
#export FLASK_ENV=prodaction
#export settings=prod
"""
2 changes: 1 addition & 1 deletion takeaway/settings/flask/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
migrate = Migrate()
jwt = JWTManager()
Model,Column,String,Integer,DateTime,ForeignKey,relationship = db.Model,db.Column,db.String,db.Integer,db.DateTime,db.ForeignKey,db.relationship
"""

12 changes: 11 additions & 1 deletion takeaway/settings/flask/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
flask_model ='''
from extensions.extension import Model,String,Integer,Column,DateTime,ForeignKey,relationship
from extensions.extension import db
from sqlalchemy.sql import func
from sqlalchemy.exc import InvalidRequestError,IntegrityError
from extensions.db_conf import (
Model,
DateTime,
Integer,
String,
Column,
Boolean,
Date,
relationship,
ForeignKey,
)
'''
23 changes: 23 additions & 0 deletions takeaway/settings/flask/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
server = """
import os
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
from app_init.app_factory import create_app
if os.environ["settings"]:
settings_name = os.environ["settings"]
else:
raise Exception("'settings' environment " "variable is not defined")
flask_app = create_app(settings_name)
app = DispatcherMiddleware(flask_app)
if __name__ == "__main__":
run_simple("127.0.0.1", 8000, app, use_debugger=True, threaded=True)
"""
Loading

0 comments on commit ee876c3

Please sign in to comment.