From b02c8cc365ade162c32c42d9ff85df17d35d706b Mon Sep 17 00:00:00 2001 From: ewandor Date: Wed, 25 Jan 2023 16:41:47 +0100 Subject: [PATCH] Fixture import --- .gitignore | 2 + back/app/fixtures/git.keep | 0 back/migration.py | 41 +++++++++++++++++++ .../src/common/crud/types/foreignkey.type.ts | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 back/app/fixtures/git.keep create mode 100644 back/migration.py diff --git a/.gitignore b/.gitignore index 6d8bc87b..a820ad5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .idea/ */__pycache__ +back/app/fixtures/ + front/app-back/ front/app-back2/ front/app-back3/ diff --git a/back/app/fixtures/git.keep b/back/app/fixtures/git.keep new file mode 100644 index 00000000..e69de29b diff --git a/back/migration.py b/back/migration.py new file mode 100644 index 00000000..90d5c638 --- /dev/null +++ b/back/migration.py @@ -0,0 +1,41 @@ +import argparse +import asyncio +import json +from os import path + +from app.db import init_db, Entity, Order, Contract, User, AccessToken + + +models = [Entity, Order, Contract, User] + + +async def handle_migration(args): + await init_db() + if args.action == "import-fixtures": + for m in models: + fname = './fixtures/{}.json'.format(m.__name__) + if path.isfile(fname): + with open(fname, 'r') as f: + content = json.load(f) + for r in content: + obj = m(**r) + obj.save() + + elif args.action == "export-fixtures": + for m in models: + if await m.count() > 0: + with open('./fixtures/{}.json'.format(m.__name__), 'w') as f: + body = [o.json() for o in await m.find_all().to_list()] + f.write('[\n' + ',\n'.join(body) + '\n]\n') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + prog='migration.py', + description='Migrate data and schemas on the crud project', + epilog='Bisou') + + parser.add_argument('action', type=str, help="The action to perform") + args = parser.parse_args() + + asyncio.run(handle_migration(args)) diff --git a/front/app/src/common/crud/types/foreignkey.type.ts b/front/app/src/common/crud/types/foreignkey.type.ts index 74714d64..46088a8a 100644 --- a/front/app/src/common/crud/types/foreignkey.type.ts +++ b/front/app/src/common/crud/types/foreignkey.type.ts @@ -91,7 +91,7 @@ export class ForeignkeyTypeComponent extends FieldType implemen getList(term: string) : Observable { return this.crudService.getList( this.foreignResource, - 0, + 1, 10, [], [new Filters('fulltext', 'eq', term)]