Skip to content

Commit

Permalink
generate dummy generator with Model Bakery
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad88me committed Sep 19, 2023
1 parent 37de863 commit c5f79d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "django-rest-gen"
version = "0.0.6"
version = "0.0.7"
authors = [
{ name="Ahmad Alobaid", email="[email protected]" },
]
Expand Down
5 changes: 4 additions & 1 deletion src/django-rest-gen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ def main():
parser.add_argument('--apppath', required=True, help="The path to the app")
parser.add_argument('--overwrite', action='store_true',
help="Whether to overwrite existing files if any")
parser.add_argument('--dummy', action='store_true',
help="Whether to generate dummy data generator")
args = parser.parse_args()
print(f"args: {args}")
base_path = os.path.abspath('.')
apigen.workflow(python_path=base_path, settings_fpath=args.settings, app_path=args.apppath, overwrite=args.overwrite)
apigen.workflow(python_path=base_path, settings_fpath=args.settings, app_path=args.apppath,
overwrite=args.overwrite, dummy=args.dummy)


main()
34 changes: 32 additions & 2 deletions src/django-rest-gen/apigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,48 @@ def write_admin(classes, app_path, admin_path):
print(content)


def workflow(python_path, app_path, settings_fpath, overwrite):
def write_dummy(classes, app_path, dummy_path, overwrite):
"""
:param classes:
:param app_path:
:param dummy_path:
:param overwrite:
:return:
"""
content = "from model_bakery import baker\nfrom django.contrib.auth.models import User\n\n"
content += "def run(*args):\n"
app_name = app_path.split(os.sep)[-1]
for c in classes:
if c[0] == "User":
line = f"\tbaker.make({c[0]})\n"
else:
line = f"\tbaker.make('{app_name}.{c[0]}')\n"
content += line
empty = utils.empty_fpath(dummy_path)
if empty or overwrite:
with open(dummy_path, "w") as f:
f.write(content)
else:
print(content)


def workflow(python_path, app_path, settings_fpath, overwrite, dummy):
"""
This includes the main workflow of the API generator.
:param python_path:
:param app_path:
:param settings_fpath:
:param overwrite: bool
:param dummy: bool
:return:
"""
models_fpath = os.path.join(app_path, "models.py")
serializers_path = os.path.join(app_path, "serializers.py")
views_path = os.path.join(app_path, "views.py")
urls_path = os.path.join(app_path, "urls.py")
admin_path = os.path.join(app_path, "admin.py")
dummy_path = os.path.join(app_path, "dummygen.py")
if overwrite:
for fpath in [serializers_path, views_path, urls_path, admin_path]:
with open(fpath, 'w') as f:
Expand All @@ -287,4 +315,6 @@ def workflow(python_path, app_path, settings_fpath, overwrite):
write_serializers(classes=classes, serializers_path=serializers_path, app_path=app_path)
write_views(classes=classes, views_path=views_path, app_path=app_path)
write_urls(classes=classes, app_path=app_path, urls_path=urls_path)
write_admin(classes=classes, app_path=app_path, admin_path=admin_path)
write_admin(classes=classes, app_path=app_path, admin_path=admin_path)
if dummy:
write_dummy(classes, app_path, dummy_path, overwrite)

0 comments on commit c5f79d5

Please sign in to comment.