def load_data(ctx, app, recursive, fixture): """ Load data from fixtures. Loads all data contained in either .yaml or .json file within the fixtures directory of the specified app. For example: \b D run load-data --app blog articles This will load the articles.json or articles.yaml fixtures into the database. By specifying the --recursive flag, one can load all fixtures contained within the app's fixtures directory. """ if recursive and fixture: log_error(DEFAULT_TOO_MANY_ARGS_ERROR) raise click.Abort elif not recursive and not fixture: log_error( DEFAULT_MISSING_ARGS_ERROR.format('--fixture or --recursive')) raise click.Abort path = ctx.obj['path'] management = ctx.obj['management'] print(path) print(management)
def admin(ctx, name, inline, fields, stub_permissions): """ Generates an admin model within the admin package. """ path = ctx.obj['admin'] fields = [f for f in fields] try: if inline: path = ctx.obj['admin_inlines'] helper = AdminHelper(cwd=path, dry=ctx.obj['dry'], force=ctx.obj['force'], verbose=ctx.obj['verbose']) helper.create( model=name, fields=fields, inline=inline, permissions=stub_permissions, ) except (KeyboardInterrupt, SystemExit): log_error('Exited!')
def inspect(ctx, scope, paths, suppress_output): """ Inspect your Django project. """ ctx.ensure_object(dict) p, m, f = find_project_files(os.getcwd()) ctx.obj['path'] = p ctx.obj['file'] = f ctx.obj['management'] = m ctx.obj['project'] = get_project_name(f) if not_in_project(ctx): log_error(DEFAULT_DIRECTORY_ERROR) raise click.Abort try: helper = InspectorHelper( cwd=m, dry=ctx.obj['dry'], verbose=ctx.obj['verbose'], ) if scope == 'apps': return helper.get_apps(show_paths=paths, suppress_output=suppress_output) return helper.get_classes(scope=scope, show_paths=paths) except (AttributeError, FileNotFoundError, KeyError, TypeError) as e: log_error('An error occurred while running the command!')
def delete(self, model, **kwargs): name = sanitized_string(model) filename = f'{name}.py' template_import = 'generic-import.tpl' if self.default_destroy_file( model=name, templates_directory=self.TEMPLATES_DIRECTORY, template_import=template_import): log_error(f'Successfully deleted {filename}.')
def resource(ctx, name, fields, inherits, api, is_managed, soft_delete): """ Generates an app resource. This is ideal to add a model along with admin, serializer, view, viewset, template, and tests. You can invoke this command the same way you would the model command: D g resource track int:number char:title fk:album bool:is_featured This will generate a model with the specified attributes and all the related modules specified above. In case you're building an api, and don't need forms, templates and views, you can pass the --api flag to the command in order to prevent these files from being created. """ name = ModelHelper.check_noun(name) try: ctx.invoke(model, name=name, api=api, register_admin=api, register_inline=api, fields=fields, test_case=True, inherits=inherits, is_managed=is_managed, soft_delete=soft_delete) ctx.invoke(admin, name=name) ctx.invoke(admin, name=name, inline=True) ctx.invoke(serializer, name=name) ctx.invoke(viewset, name=name) if not api: ctx.invoke(form, name=name) ctx.invoke(view, name=name, class_type='list') ctx.invoke(view, name=name, class_type='detail') except (KeyboardInterrupt, SystemExit) as e: log_error('Exited!')
def run(self, **kwargs): project_name = kwargs['project_name'] env_file = kwargs['filepath'] if kwargs[ 'filepath'] else kwargs['path'] + '/.env' try: f = open(env_file) f.close() except FileNotFoundError: log_error("No environment file found.") raise click.Abort destination = kwargs['destination'] if kwargs['no_dokku'] and kwargs['no_example']: log_error('Nothing to export. Skipping...') click.Abort() if not kwargs['no_example']: self.copy_example(env_file, destination) if not kwargs['no_dokku']: self.copy_dokku(env_file, destination, project_name)
def model(ctx, name, full, abstract, fields, register_admin, register_inline, test_case, inherits, api, app, is_managed, soft_delete): """ Generates a model under the models directory. One can specify multiple attributes after the model's name, like so: D g model track int:number char:title fk:album bool:is_favorite This will generate a Track model and add a foreign key of Album. If the model is to be added to admin.site one can optionally opt in by specifying the --register-admin flag. """ name = ModelHelper.check_noun(name) # Ensure --app is used only if --inherits is used if app and not inherits: log_error( "You've specified an app inheritance scope but did not specify the model to inherit from." ) log_error("Please rerun the command like so:") log_standard( f"D generate model {name} --inherits BASE_MODEL --app {app}") raise click.Abort path = ctx.obj['models'] helper = ModelHelper(cwd=path, dry=ctx.obj['dry'], force=ctx.obj['force'], verbose=ctx.obj['verbose']) model_fields = helper.create(model=name, api=api, abstract=abstract, fields=fields, inherits=inherits, scope=app, project=ctx.obj['project_name'], is_managed=is_managed, soft_delete=soft_delete) if api: ctx.invoke(test, name=name, scope="model") ctx.invoke(serializer, name=name) ctx.invoke(viewset, name=name) if register_admin or full: ctx.invoke(admin, name=name, fields=model_fields) if register_inline or full: ctx.invoke(admin, name=name, inline=True) if (test_case or full) and not api: ctx.invoke(test, name=name, scope="model") if full and not api: ctx.invoke(serializer, name=name) ctx.invoke(viewset, name=name) if full: ctx.invoke(form, name=name) ctx.invoke(template, name=name, class_type='list') ctx.invoke(template, name=name, class_type='detail') ctx.invoke(view, name=name, class_type="list") ctx.invoke(view, name=name, class_type="detail") # Retuning model fields return model_fields