def obj_delete_list(self, bundle, **kwargs): with transaction.atomic(): caches = CachePackageSource.objects.all() # 需要单独调用自定义 delete 方法 for cache in caches: cache.delete() for origin_type, origin_model in source_cls_factory.items(): origins = origin_model.objects.all() # 需要单独调用自定义 delete 方法 for origin in origins: origin.delete()
def destroy(self, request, *args, **kwargs): with transaction.atomic(): caches = CachePackageSource.objects.all() # 需要单独调用自定义 delete 方法 for cache in caches: cache.delete() for origin_type, origin_model in list(source_cls_factory.items()): origins = origin_model.objects.all() # 需要单独调用自定义 delete 方法 for origin in origins: origin.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def obj_delete_list(self, bundle, **kwargs): verify_or_raise_immediate_response( principal_type='user', principal_id=bundle.request.user.username, resource=admin_operate_resource, action_ids=[admin_operate_resource.actions.edit.id], instance=None) with transaction.atomic(): caches = CachePackageSource.objects.all() # 需要单独调用自定义 delete 方法 for cache in caches: cache.delete() for origin_type, origin_model in source_cls_factory.items(): origins = origin_model.objects.all() # 需要单独调用自定义 delete 方法 for origin in origins: origin.delete()
def obj_delete_list(self, bundle, **kwargs): allow_or_raise_immediate_response( iam=iam, system=IAMMeta.SYSTEM_ID, subject=Subject("user", bundle.request.user.username), action=Action(IAMMeta.ADMIN_EDIT_ACTION), resources=[], ) with transaction.atomic(): caches = CachePackageSource.objects.all() # 需要单独调用自定义 delete 方法 for cache in caches: cache.delete() for origin_type, origin_model in list(source_cls_factory.items()): origins = origin_model.objects.all() # 需要单独调用自定义 delete 方法 for origin in origins: origin.delete()
def obj_update(self, bundle, skip_errors=False, **kwargs): try: origins = bundle.data.pop('origins') caches = bundle.data.pop('caches') except KeyError: raise BadRequest('Invalid params, please check origins and caches') with transaction.atomic(): # collect packages of all origin to cache if caches: cache_packages = {} for origin_type, origin_model in source_cls_factory.items(): origins_from_db = origin_model.objects.all().values('packages') for origin in origins_from_db: cache_packages.update(origin['packages']) for origin in origins: try: jsonschema.validate(origin, UPDATE_SOURCE_SCHEMA) except jsonschema.ValidationError as e: message = 'Invalid origin source params: %s' % e logger.error(message) raise BadRequest(message) cache_packages.update(origin['packages']) # create or update cache first caches_to_update = [cache['id'] for cache in caches if 'id' in cache] # delete caches whom id not in param caches CachePackageSource.objects.exclude(id__in=caches_to_update).delete() for cache in caches: try: jsonschema.validate(cache, UPDATE_SOURCE_SCHEMA) except jsonschema.ValidationError as e: message = 'Invalid cache source params: %s' % e logger.error(message) raise BadRequest(message) if 'id' in cache: try: CachePackageSource.objects.update_base_source(cache['id'], cache['type'], cache_packages, **cache['details']) except CachePackageSource.DoesNotExist: message = 'Invalid cache source id: %s, which cannot be found' % cache['id'] logger.error(message) raise BadRequest(message) if cache.get('desc', ''): CachePackageSource.objects.filter(id=cache['id']).update(desc=cache['desc']) else: try: CachePackageSource.objects.add_cache_source(cache['name'], cache['type'], cache_packages, cache.get('desc', ''), **cache['details']) except exceptions.GcloudExternalPluginsError as e: message = 'Create cache source raise error: %s' % e.message logger.error(message) raise BadRequest(message) else: CachePackageSource.objects.all().delete() # delete origins whom id not in param origins for origin_type, origin_model in source_cls_factory.items(): origins_to_update = [origin['id'] for origin in origins if 'id' in origin and origin['type'] == origin_type] origin_model.objects.exclude(id__in=origins_to_update).delete() # create origins after for origin in origins: source_type = origin['type'] details = origin['details'] # divide details into two parts,base_kwargs mains fields in base model(e.g. fields of # pipeline.contrib.external_plugins.models.GitRepoSource) # original_kwargs mains field in origin model but not in base model(e.g. repo_address、desc) source_model = source_cls_factory[source_type] original_kwargs, base_kwargs = source_model.objects.divide_details_parts(source_type, details) if origin.get('desc', ''): original_kwargs['desc'] = origin['desc'] if 'id' in origin: source_model.objects.update_original_source(origin['id'], origin['packages'], original_kwargs, **base_kwargs) else: source_model.objects.add_original_source(origin['name'], source_type, origin['packages'], original_kwargs, **base_kwargs)
def obj_update(self, bundle, skip_errors=False, **kwargs): allow_or_raise_immediate_response( iam=iam, system=IAMMeta.SYSTEM_ID, subject=Subject("user", bundle.request.user.username), action=Action(IAMMeta.ADMIN_EDIT_ACTION), resources=[], ) try: origins = bundle.data.pop("origins") caches = bundle.data.pop("caches") except KeyError: raise BadRequest("Invalid params, please check origins and caches") with transaction.atomic(): # collect packages of all origin to cache if caches: cache_packages = {} for origin_type, origin_model in list( source_cls_factory.items()): origins_from_db = origin_model.objects.all().values( "packages") for origin in origins_from_db: cache_packages.update(origin["packages"]) for origin in origins: try: jsonschema.validate(origin, UPDATE_SOURCE_SCHEMA) except jsonschema.ValidationError as e: message = "Invalid origin source params: %s" % e logger.error(message) raise BadRequest(message) cache_packages.update(origin["packages"]) # create or update cache first caches_to_update = [ cache["id"] for cache in caches if "id" in cache ] # delete caches whom id not in param caches CachePackageSource.objects.exclude( id__in=caches_to_update).delete() for cache in caches: try: jsonschema.validate(cache, UPDATE_SOURCE_SCHEMA) except jsonschema.ValidationError as e: message = "Invalid cache source params: %s" % e logger.error(message) raise BadRequest(message) if "id" in cache: try: CachePackageSource.objects.update_base_source( cache["id"], cache["type"], cache_packages, **cache["details"]) except CachePackageSource.DoesNotExist: message = "Invalid cache source id: %s, which cannot be found" % cache[ "id"] logger.error(message) raise BadRequest(message) if cache.get("desc", ""): CachePackageSource.objects.filter( id=cache["id"]).update(desc=cache["desc"]) else: try: CachePackageSource.objects.add_cache_source( cache["name"], cache["type"], cache_packages, cache.get("desc", ""), **cache["details"]) except exceptions.GcloudExternalPluginsError as e: message = "Create cache source raise error: %s" % str( e) logger.error(message) raise BadRequest(message) else: CachePackageSource.objects.all().delete() # delete origins whom id not in param origins for origin_type, origin_model in list(source_cls_factory.items()): origins_to_update = [ origin["id"] for origin in origins if "id" in origin and origin["type"] == origin_type ] origin_model.objects.exclude(id__in=origins_to_update).delete() # create origins after for origin in origins: source_type = origin["type"] details = origin["details"] # divide details into two parts,base_kwargs mains fields in base model(e.g. fields of # pipeline.contrib.external_plugins.models.GitRepoSource) # original_kwargs mains field in origin model but not in base model(e.g. repo_address、desc) source_model = source_cls_factory[source_type] original_kwargs, base_kwargs = source_model.objects.divide_details_parts( source_type, details) if origin.get("desc", ""): original_kwargs["desc"] = origin["desc"] if "id" in origin: source_model.objects.update_original_source( origin["id"], origin["packages"], original_kwargs, **base_kwargs) else: source_model.objects.add_original_source( origin["name"], source_type, origin["packages"], original_kwargs, **base_kwargs)
def partial_update(self, request, *args, **kwargs): origins = request.data.pop("origins") caches = request.data.pop("caches") with transaction.atomic(): # collect packages of all origin to cache if caches: cache_packages = {} for origin_type, origin_model in list( source_cls_factory.items()): origins_from_db = origin_model.objects.all().values( "packages") for origin in origins_from_db: cache_packages.update(origin["packages"]) for origin in origins: try: jsonschema.validate(origin, UPDATE_SOURCE_SCHEMA) except jsonschema.ValidationError as e: message = "Invalid origin source params: %s" % e logger.error(message) raise NotAcceptable(message) cache_packages.update(origin["packages"]) # create or update cache first caches_to_update = [ cache["id"] for cache in caches if "id" in cache ] # delete caches whom id not in param caches CachePackageSource.objects.exclude( id__in=caches_to_update).delete() for cache in caches: try: jsonschema.validate(cache, UPDATE_SOURCE_SCHEMA) except jsonschema.ValidationError as e: message = "Invalid cache source params: %s" % e logger.error(message) raise NotAcceptable(message) if "id" in cache: try: CachePackageSource.objects.update_base_source( cache["id"], cache["type"], cache_packages, **cache["details"]) except CachePackageSource.DoesNotExist: message = "Invalid cache source id: %s, which cannot be found" % cache[ "id"] logger.error(message) raise NotAcceptable(message) if cache.get("desc", ""): CachePackageSource.objects.filter( id=cache["id"]).update(desc=cache["desc"]) else: try: CachePackageSource.objects.add_cache_source( cache["name"], cache["type"], cache_packages, cache.get("desc", ""), **cache["details"]) except exceptions.GcloudExternalPluginsError as e: message = "Create cache source raise error: %s" % str( e) logger.error(message) raise NotAcceptable(message) else: CachePackageSource.objects.all().delete() # delete origins whom id not in param origins for origin_type, origin_model in list(source_cls_factory.items()): origins_to_update = [ origin["id"] for origin in origins if "id" in origin and origin["type"] == origin_type ] origin_model.objects.exclude(id__in=origins_to_update).delete() # create origins after for origin in origins: source_type = origin["type"] details = origin["details"] # divide details into two parts,base_kwargs mains fields in base model(e.g. fields of # pipeline.contrib.external_plugins.models.GitRepoSource) # original_kwargs mains field in origin model but not in base model(e.g. repo_address、desc) source_model = source_cls_factory[source_type] original_kwargs, base_kwargs = source_model.objects.divide_details_parts( source_type, details) if origin.get("desc", ""): original_kwargs["desc"] = origin["desc"] if "id" in origin: source_model.objects.update_original_source( origin["id"], origin["packages"], original_kwargs, **base_kwargs) else: source_model.objects.add_original_source( origin["name"], source_type, origin["packages"], original_kwargs, **base_kwargs) return Response(status=status.HTTP_204_NO_CONTENT)