def parse_and_save(data, current_template): """Parse the data to create custom resources and save them. Args: data: dict current_template: Returns: """ for key in data.keys(): resource = data[key] custom_resource = CustomResource(template=current_template) if _is_type_all(resource): custom_resource = _create_custom_resource_type_all( custom_resource, resource, key) # TODO: make sure only one type = 'all' ? else: custom_resource = _create_custom_resource(custom_resource, resource, key) try: _check_curate(custom_resource) custom_resource.save() except Exception as e: raise exceptions.ModelError(str(e))
def delete_custom_resources_by_template(template): """Delete all custom resources related to a template. Args: template: Returns: """ CustomResource.delete_custom_resources_by_template(template)
def test_create_custom_resource_wrong_type_return_except(self): # Act template = self.fixture.create_and_save_template() # Assert with self.assertRaises(ValidationError): custom_resource = CustomResource( template=template, title="title", type="wrong", icon="icon", sort=0 ) custom_resource.save()
def test_save_list(self): # Act template = self.fixture.create_and_save_template() custom_resource1 = CustomResource( template=template, title="title1", type="resource", icon="icon", sort=0 ) custom_resource2 = CustomResource( template=template, title="title2", type="resource", icon="icon", sort=1 ) list_custom = [custom_resource1, custom_resource2] custom_resource_api.save_list(list_custom) result = custom_resource_api.get_all_by_template(template) # Assert self.assertEquals(len(list_custom), len(result))
def create_custom_resource( template=None, name_in_schema="", title="", description="", slug="", type=CUSTOM_RESOURCE_TYPE.RESOURCE.value, icon="", icon_color="", display_icon=False, role_choice="", role_type="", sort=0, ): if template is None: template = CustomResourceFixtures.create_and_save_template() return CustomResource( template=template, name_in_schema=name_in_schema, title=title, description=description, slug=slug, type=type, icon=icon, icon_color=icon_color, display_icon=display_icon, role_choice=role_choice, role_type=role_type, sort=sort, ).save()
def test_check_curate_is_resource_return_except(self): # Act custom_resource = CustomResource(type=CUSTOM_RESOURCE_TYPE.RESOURCE.value) # Assert with mock.patch.object(settings, "INSTALLED_APPS", ["core_curate_app"]): with self.assertRaises(exceptions.ModelError): custom_resource_api._check_curate(custom_resource)
def test_is_all_resource_return_type_all(self): # Act custom_resource = CustomResource(type=CUSTOM_RESOURCE_TYPE.ALL.value) # Assert self.assertFalse( custom_resource_api._is_custom_resource_type_resource(custom_resource) )
def test_check_curate_is_not_resource_return_none(self): # Act custom_resource = CustomResource() # Assert with mock.patch.object(settings, "INSTALLED_APPS", ["core_curate_app"]): self.assertIsNone( custom_resource_api._check_curate(custom_resource))
def get_all_by_template(template): """Return all custom resource by template. Args: template: Returns: custom registry collection """ return CustomResource.get_all_by_template(template)
def test_check_curate_is_resource_with_all_return_none(self): # Act custom_resource = CustomResource( type=CUSTOM_RESOURCE_TYPE.RESOURCE.value, role_type="role_type", role_choice="role_choice", ) # Assert with mock.patch.object(settings, "INSTALLED_APPS", ["core_curate_app"]): self.assertIsNone(custom_resource_api._check_curate(custom_resource))
def get_by_role_for_current_template(role, request): """Get the custom resource by template and slug Args: role: request: Returns: """ return CustomResource.get_by_role_for_current_template( _get_current_template(request=request), role)
def get_by_current_template_and_slug(slug, request): """Get the custom resource by template and slug Args: slug: request: Returns: """ return CustomResource.get_custom_resource_by_template_and_slug( _get_current_template(request=request), slug)
def test_get_returns_custom_resource( self, get_current, get_custom_resource_by_template_and_slug): # Arrange get_custom_resource_by_template_and_slug.return_value = CustomResource( ) get_current.return_value = Template() # Assert self.assertTrue( isinstance( custom_resource_api.get_by_current_template_and_slug("test"), CustomResource, ))
def get_current_custom_resource_type_all(request): """Return the custom resource of the current template with type 'all'. Args: request Returns: custom registry collection """ custom_resources = CustomResource.get_custom_resource_by_template_and_type( _get_current_template(request=request), CUSTOM_RESOURCE_TYPE.ALL.value) if len(custom_resources) > 1: raise exceptions.ModelError( "Multiple custom resources with type 'all' were found.") elif len(custom_resources) == 0: raise exceptions.DoesNotExist( "The custom resource with type 'all' does not exist.") return custom_resources[0]
def test_get_returns_custom_resource( self, get_current, get_custom_resource_by_template_and_slug ): # Arrange mock_user = create_mock_user("1", is_superuser=True) mock_request = create_mock_request(user=mock_user) get_custom_resource_by_template_and_slug.return_value = CustomResource() get_current.return_value = Template() # Assert self.assertTrue( isinstance( custom_resource_api.get_by_current_template_and_slug( "test", request=mock_request ), CustomResource, ) )
def test_check_curate_core_curate_app_not_installed_return_none(self): # Act custom_resource = CustomResource() # Assert self.assertIsNone(custom_resource_api._check_curate(custom_resource))