Skip to content

Commit

Permalink
add overwrite option
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad88me committed Sep 18, 2023
1 parent 16e0f5f commit 37de863
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ specify the appropriate arguments (e.g., `python -m django-rest-gen --settings

## Arguments
```
usage: django-rest-gen [-h] [--pythonpath PYTHONPATH] --settings SETTINGS --apppath APPPATH
usage: django-rest-gen [-h] [--pythonpath PYTHONPATH] --settings SETTINGS --apppath APPPATH [--overwrite]
Generate Django REST API code
Expand All @@ -20,6 +20,7 @@ optional arguments:
Python Path directory.
--settings SETTINGS The path to the django project settings
--apppath APPPATH The path to the app
--overwrite Whether to overwrite existing files if any
```


Expand Down
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.5"
version = "0.0.6"
authors = [
{ name="Ahmad Alobaid", email="[email protected]" },
]
Expand Down
4 changes: 3 additions & 1 deletion src/django-rest-gen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ def main():
parser.add_argument('--pythonpath', default=".", help="Python Path directory. ")
parser.add_argument('--settings', required=True, help="The path to the django project settings")
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")
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)
apigen.workflow(python_path=base_path, settings_fpath=args.settings, app_path=args.apppath, overwrite=args.overwrite)


main()
7 changes: 6 additions & 1 deletion src/django-rest-gen/apigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,24 @@ def write_admin(classes, app_path, admin_path):
print(content)


def workflow(python_path, app_path, settings_fpath):
def workflow(python_path, app_path, settings_fpath, overwrite):
"""
This includes the main workflow of the API generator.
:param python_path:
:param app_path:
:param settings_fpath:
:param overwrite: 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")
if overwrite:
for fpath in [serializers_path, views_path, urls_path, admin_path]:
with open(fpath, 'w') as f:
f.write('')
models_obj = load_models(python_path=python_path, settings_fpath=settings_fpath, models_fpath=models_fpath)
classes = get_classes(models_obj)
write_serializers(classes=classes, serializers_path=serializers_path, app_path=app_path)
Expand Down

0 comments on commit 37de863

Please sign in to comment.