diff --git a/README.md b/README.md index ce7354c..1b882c8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` diff --git a/pyproject.toml b/pyproject.toml index 7084579..bf62096 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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="ahmad88me@gmail.com" }, ] diff --git a/src/django-rest-gen/__main__.py b/src/django-rest-gen/__main__.py index b9f8b7e..9411441 100644 --- a/src/django-rest-gen/__main__.py +++ b/src/django-rest-gen/__main__.py @@ -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() diff --git a/src/django-rest-gen/apigen.py b/src/django-rest-gen/apigen.py index 684bd82..5829c49 100644 --- a/src/django-rest-gen/apigen.py +++ b/src/django-rest-gen/apigen.py @@ -264,12 +264,13 @@ 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") @@ -277,6 +278,10 @@ def workflow(python_path, app_path, settings_fpath): 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)