async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = "home_schema -> CreateForm" with ax_model.try_catch(info.context['session'], err) as db_session: name = args.get('name') db_name = args.get('db_name') default_tab_name = args.get('default_tab_name') default_grid_name = args.get('default_grid_name') default_start = args.get('default_start') default_all = args.get('default_all') default_create = args.get('default_create') default_state = args.get('default_state') default_delete = args.get('default_delete') default_deleted = args.get('default_deleted') default_update = args.get('default_update') delete_confirm = args.get('delete_confirm') name_avalible = await is_db_name_avalible(db_name=db_name) if name_avalible is False: return CreateForm(form=None, avalible=False, ok=True) await create_db_table(db_session=db_session, db_name=db_name) new_form = await create_ax_form(db_session=db_session, name=name, db_name=db_name) # Add Admins role to object states = await create_default_states( db_session=db_session, ax_form=new_form, default_start=default_start, default_state=default_state, default_all=default_all, default_deleted=default_deleted) await create_default_actions(db_session=db_session, ax_form=new_form, states=states, default_create=default_create, default_delete=default_delete, default_update=default_update, delete_confirm=delete_confirm) await create_default_grid(db_session=db_session, ax_form=new_form, name=default_grid_name) await create_default_tab(db_session=db_session, ax_form=new_form, name=default_tab_name) db_session.commit() ax_schema.init_schema(db_session) # TODO: check for multiple workers and load balancers. # https://community.sanicframework.org/t/removing-routes-necessary-functionality/29 return CreateForm(form=new_form, avalible=True, ok=True)
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = 'Error in gql mutation - grids_schema -> CreateGrid.' with ax_model.try_catch(info.context['session'], err) as db_session: form_guid = args.get('form_guid') name = args.get('name') ax_form = db_session.query(AxForm).filter( AxForm.guid == uuid.UUID(form_guid)).first() db_name = "Grid" cur_num = 1 name_is_checked = False while name_is_checked is False: error_flag = False if cur_num > 1: cur_name = name + " " + str(cur_num) else: cur_name = name cur_db_name = db_name + str(cur_num) for grid in ax_form.grids: if grid.name == cur_name or grid.db_name == cur_db_name: error_flag = True if error_flag is True: cur_num = cur_num + 1 else: name_is_checked = True break ax_grid = AxGrid() ax_grid.name = cur_name ax_grid.db_name = db_name + str(cur_num) ax_grid.form_guid = ax_form.guid ax_grid.code = None ax_grid.position = len(ax_form.grids) + 1 default_options = { "enableQuickSearch": False, "enableFlatMode": False, "enableColumnsResize": True, "enableFiltering": True, "enableSorting": True, "enableOpenForm": True, "enableActions": True, "rowHeight": 45, "pinned": 0 } ax_grid.options_json = json.dumps(default_options) ax_grid.is_default_view = False db_session.add(ax_grid) db_session.commit() ax_schema.init_schema(db_session) return CreateGrid(grid=ax_grid, ok=True)
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = 'Error in gql mutation - grids_schema -> UpdateGrid.' with ax_model.try_catch(info.context['session'], err) as db_session: guid = args.get('guid') name = args.get('name') db_name = args.get('db_name') options_json = args.get('options_json') is_default_view = args.get('is_default_view') schema_reload_needed = False tom_sync_needed = False ax_grid = db_session.query(AxGrid).filter( AxGrid.guid == uuid.UUID(guid)).first() old_db_name = ax_grid.db_name if name: ax_grid.name = name if db_name: ax_grid.db_name = db_name schema_reload_needed = True if old_db_name != db_name: tom_sync_needed = True if options_json: ax_grid.options_json = options_json if is_default_view: all_grids = db_session.query(AxGrid).filter( AxGrid.form_guid == ax_grid.form_guid and AxGrid.guid != ax_grid.guid).all() for grid in all_grids: grid.is_default_view = False ax_grid.is_default_view = is_default_view schema_reload_needed = True db_session.commit() if schema_reload_needed: ax_schema.init_schema(db_session) if tom_sync_needed: tom_sync_grid(db_session=db_session, form_db_name=ax_grid.form.db_name, old_db_name=old_db_name, new_db_name=ax_grid.db_name) return UpdateGrid(grid=ax_grid, ok=True)
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = "home_schema -> UpdateForm" with ax_model.try_catch(info.context['session'], err) as db_session: ax_form = db_session.query(AxForm).filter( AxForm.guid == uuid.UUID(args.get('guid'))).first() old_form_db_name = ax_form.db_name db_name_changed = None tom_sync_needed = False schema_reload_needed = False if str(args.get('db_name')) != str(ax_form.db_name): db_name_changed = args.get('db_name') name_avalible = await is_db_name_avalible( db_name=args.get('db_name')) if name_avalible is False: return UpdateForm(form=None, avalible=False, db_name_changed=None, ok=True) else: await ax_dialects.dialect.rename_table( db_session=db_session, old=ax_form.db_name, new=args.get('db_name')) tom_sync_needed = True schema_reload_needed = True ax_form.name = args.get('name') ax_form.db_name = args.get('db_name') ax_form.icon = args.get('icon') ax_form.tom_label = args.get('tom_label') db_session.flush() if schema_reload_needed: ax_schema.init_schema(db_session) if tom_sync_needed: await tom_sync_form(db_session=db_session, old_form_db_name=old_form_db_name, new_form_db_name=ax_form.db_name) return UpdateForm(form=ax_form, avalible=True, db_name_changed=db_name_changed, ok=True)
async def install_from_dir(db_session, app_dir): """ Installs app from yaml files in /tmp folder """ import backend.schema as ax_schema with open(app_dir / 'ax_app.yaml', 'r', encoding="utf-8") as app_yaml_file: app_yaml = yaml.load(app_yaml_file) # Check if package can be installed await check_package(db_session=db_session, app_yaml=app_yaml, package_directory=app_dir) # Create pages await terminal_log('Creating pages:\n') ax_root_page = db_session.query(AxPage).filter( AxPage.parent.is_(None)).first() ax_root_guid = None if ax_root_page: ax_root_guid = str(ax_root_page.guid) app_root_guid = app_yaml.get('Root page', None) for page in app_yaml.get('Pages', None): await create_page(db_session=db_session, page=page, package_directory=app_dir, ax_root_guid=ax_root_guid, app_root_guid=app_root_guid) await terminal_log(f' {page["name"]} ✔\n') # Create folders await terminal_log('Creating folders:\n') for folder in app_yaml['Folders']: await create_folder(db_session=db_session, folder=folder) await terminal_log(f' {folder["name"]} ✔\n') # For each form - create form for form_name in app_yaml['Forms']: await create_form_objects(db_session=db_session, form_name=form_name, package_directory=app_dir) ax_schema.init_schema(db_session) await terminal_log('------------------\n Application is installed!')
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = 'Error in gql mutation - form_schema -> DeleteField.' with ax_model.try_catch(info.context['session'], err) as db_session: current_user = info.context['user'] guid = args.get('guid') ax_field = db_session.query(AxField).filter( AxField.guid == uuid.UUID(guid)).first() db_session.query(AxRoleFieldPermission).filter( AxRoleFieldPermission.field_guid == ax_field.guid).delete() db_session.query(AxColumn).filter( AxColumn.field_guid == ax_field.guid).delete() if ax_field.is_tab is False and ax_field.is_virtual is False: await ax_dialects.dialect.drop_column( db_session=db_session, table=ax_field.form.db_name, column=ax_field.db_name) # if backend avalible if ax_field.field_type.is_backend_available: tag = ax_field.field_type_tag field_py = globals().get(f'AxField{tag}', None) if field_py and hasattr(field_py, "before_field_delete"): method_to_call = getattr(field_py, "before_field_delete") await method_to_call(db_session=db_session, field=ax_field, before_form=ax_field.form, tobe_form=ax_field.form, action=None, current_user=current_user) roles = ax_field.form.roles db_session.delete(ax_field) ax_schema.init_schema(db_session) # re-create GQL schema ok = True return DeleteField(deleted=guid, roles=roles, ok=ok)
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = 'Error in gql mutation - grids_schema -> DeleteGrid.' with ax_model.try_catch(info.context['session'], err) as db_session: guid = args.get('guid') ax_grid = db_session.query(AxGrid).filter( AxGrid.guid == uuid.UUID(guid)).first() form_db_name = ax_grid.form.name old_grid_db_name = ax_grid.db_name default_db_name = None for grid in ax_grid.form.grids: if grid.is_default_view: default_db_name = grid.db_name db_session.delete(ax_grid) db_session.commit() ax_schema.init_schema(db_session) tom_sync_grid(db_session=db_session, form_db_name=form_db_name, old_db_name=old_grid_db_name, new_db_name=default_db_name) return DeleteGrid(deleted=guid, ok=True)
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = 'Error in gql mutation - form_schema -> UpdateField.' with ax_model.try_catch(info.context['session'], err) as db_session: guid = args.get('guid') name = args.get('name') db_name = args.get('db_name') is_required = args.get('is_required') is_whole_row = args.get('is_whole_row') options_json = args.get('options_json') private_options_json = args.get('private_options_json') schema_needs_update = False ax_field = db_session.query(AxField).filter( AxField.guid == uuid.UUID(guid)).first() if name: ax_field.name = name if db_name and db_name != ax_field.db_name: db_name_error = False for field in ax_field.form.fields: if field.db_name == db_name and (field.guid != uuid.UUID(guid)): db_name_error = True if db_name_error: db_name = db_name + '_enother' await ax_dialects.dialect.rename_column( db_session=db_session, table=ax_field.form.db_name, old_name=ax_field.db_name, new_name=db_name, type_name=ax_field.field_type.value_type) ax_field.db_name = db_name db_session.flush() schema_needs_update = True if options_json: ax_field.options_json = json.dumps(options_json) if private_options_json: ax_field.private_options_json = json.dumps( private_options_json) if is_required is not None: ax_field.is_required = is_required if is_whole_row is not None: if ax_field.field_type.is_always_whole_row: ax_field.is_whole_row = True else: ax_field.is_whole_row = is_whole_row # db_session.flush() if schema_needs_update: ax_schema.init_schema(db_session) # re-create GQL schema ok = True return CreateTab(field=ax_field, ok=ok)
async def mutate(self, info, **args): # pylint: disable=missing-docstring import backend.schema as ax_schema err = 'Error in gql mutation - form_schema -> CreateTab' with ax_model.try_catch(info.context['session'], err) as db_session: current_user = info.context['user'] form_guid = args.get('form_guid') name = args.get('name') tag = args.get('tag') position = args.get('position') positions = args.get('positions') parent = args.get('parent') cur_name = None cur_db_name = None name_is_checked = False cur_num = 1 ax_field_type = db_session.query(AxFieldType).filter( AxFieldType.tag == tag).first() ax_form = db_session.query(AxForm).filter( AxForm.guid == uuid.UUID(form_guid)).first() # If db table already have {db_name} column -> add digit to db_name while name_is_checked is False: error_flag = False if cur_num > 1: cur_name = name + " " + str(cur_num) cur_db_name = ax_field_type.default_db_name + str(cur_num) else: cur_name = name cur_db_name = ax_field_type.default_db_name for field in ax_form.fields: if field.name == cur_name or field.db_name == cur_db_name: error_flag = True if error_flag is True: cur_num = cur_num + 1 else: name_is_checked = True break if not positions: positions = [] if not parent: for fld in ax_form.fields: if fld.is_tab and not parent: parent = fld.guid if not position: flds_num = 0 for fld in ax_form.fields: if str(fld.parent) == str(parent): flds_num += 1 position = flds_num ax_field = AxField() ax_field.name = cur_name ax_field.db_name = cur_db_name ax_field.form_guid = ax_form.guid ax_field.value_type = ax_field_type.value_type ax_field.field_type_tag = ax_field_type.tag ax_field.options_json = "{}" ax_field.position = position ax_field.parent = ax_misc.guid_or_none(parent) if ax_field_type.is_always_whole_row: ax_field.is_whole_row = True db_session.add(ax_field) if not ax_field_type.is_virtual: await ax_dialects.dialect.add_column( db_session=db_session, table=ax_form.db_name, db_name=ax_field.db_name, type_name=ax_field_type.value_type) db_session.flush() # Update positions of all fields that are lower then created field for field in ax_form.fields: for pos in positions: if field.guid == uuid.UUID(pos.guid): current_parent = None if pos.parent != '#': current_parent = uuid.UUID(pos.parent) field.position = pos.position field.parent = current_parent # Run after_create if needed if ax_field.field_type.is_backend_available: tag = ax_field.field_type_tag field_py = globals().get(f'AxField{tag}', None) if field_py and hasattr(field_py, "after_field_create"): method_to_call = getattr(field_py, "after_field_create") await method_to_call(db_session=db_session, field=ax_field, before_form=ax_form, tobe_form=ax_form, action=None, current_user=current_user) db_session.commit() ax_schema.init_schema(db_session) # re-create GQL schema roles = ax_form.roles ok = True return CreateField(field=ax_field, roles=roles, ok=ok)