def viewset_for_model(model_obj): """ Given a Model instance or class, return the registered ViewSet for that Model """ # model_obj can be an instance or class, force it to class model_class = model_obj._meta.model if model_class in _model_viewset_cache: return _model_viewset_cache[model_class] # cache miss, fill in the cache while we look for our matching viewset model_viewset = None # go through the viewset registry to find the viewset for the passed-in model for app in pulp_plugin_configs(): for model, viewset in app.named_viewsets.items(): _model_viewset_cache.setdefault(model, viewset) if model is model_class: model_viewset = viewset break if model_viewset is not None: break if model_viewset is None: raise LookupError( 'Could not determine ViewSet base name for model {}'.format( model_class)) return viewset
def get_schema(self, request=None, public=False): """ Generate a OpenAPI schema. """ reset_generator_stats() result = build_root_object( paths=self.parse(request, public), components=self.registry.build( spectacular_settings.APPEND_COMPONENTS), version=self.api_version or getattr(request, "version", None), ) for hook in spectacular_settings.POSTPROCESSING_HOOKS: result = hook(result=result, generator=self, request=request, public=public) # Basically I'm doing it to get pulp logo at redoc page result["info"]["x-logo"] = { "url": "https://pulp.plan.io/attachments/download/517478/pulp_logo_word_rectangle.svg" } # Adding plugin version config result["info"]["x-pulp-app-versions"] = {} for app in pulp_plugin_configs(): result["info"]["x-pulp-app-versions"][app.label] = app.version # Adding current host as server (it will provide a default value for the bindings) server_url = "http://localhost:24817" if not request else request.build_absolute_uri( "/") result["servers"] = [{"url": server_url}] return normalize_result_object(result)
def get_viewset_for_model(model_obj): """ Given a Model instance or class, return the registered ViewSet for that Model """ # model_obj can be an instance or class, force it to class model_class = model_obj._meta.model if model_class in _model_viewset_cache: return _model_viewset_cache[model_class] # cache miss, fill in the cache while we look for our matching viewset model_viewset = None # go through the viewset registry to find the viewset for the passed-in model for app in pulp_plugin_configs(): for model, viewset in app.named_viewsets.items(): _model_viewset_cache.setdefault(model, viewset) if model is model_class: model_viewset = viewset break if model_viewset is not None: break if model_viewset is None: raise LookupError('Could not determine ViewSet base name for model {}'.format( model_class)) return viewset
async def server(*args, **kwargs): for pulp_plugin in pulp_plugin_configs(): if pulp_plugin.name != "pulpcore.app": content_module_name = '{name}.{module}'.format(name=pulp_plugin.name, module=CONTENT_MODULE_NAME) with suppress(ModuleNotFoundError): import_module(content_module_name) app.add_routes([web.get(settings.CONTENT_PATH_PREFIX + '{path:.+}', Handler().stream_content)]) return app
async def server(*args, **kwargs): for pulp_plugin in pulp_plugin_configs(): if pulp_plugin.name != "pulpcore.app": content_module_name = '{name}.{module}'.format( name=pulp_plugin.name, module=CONTENT_MODULE_NAME) with suppress(ModuleNotFoundError): import_module(content_module_name) app.add_routes([ web.get(settings.CONTENT_PATH_PREFIX + '{path:.+}', Handler().stream_content) ]) return app
async def server(*args, **kwargs): asyncio.ensure_future(_heartbeat()) for pulp_plugin in pulp_plugin_configs(): if pulp_plugin.name != "pulpcore.app": content_module_name = "{name}.{module}".format( name=pulp_plugin.name, module=CONTENT_MODULE_NAME) with suppress(ModuleNotFoundError): import_module(content_module_name) app.add_routes( [web.get(settings.CONTENT_PATH_PREFIX, Handler().list_distributions)]) app.add_routes([ web.get(settings.CONTENT_PATH_PREFIX + "{path:.+}", Handler().stream_content) ]) return app
def get(self, request): """ Returns status and app information about Pulp. Information includes: * version of pulpcore and loaded pulp plugins * known workers * known content apps * database connection status * redis connection status * disk usage information """ versions = [] for app in pulp_plugin_configs(): versions.append({"component": app.label, "version": app.version}) if settings.CACHE_ENABLED: redis_status = {"connected": self._get_redis_conn_status()} else: redis_status = None db_status = {"connected": self._get_db_conn_status()} try: online_workers = Worker.objects.online_workers() except Exception: online_workers = None try: online_content_apps = ContentAppStatus.objects.online() except Exception: online_content_apps = None data = { "versions": versions, "online_workers": online_workers, "online_content_apps": online_content_apps, "database_connection": db_status, "redis_connection": redis_status, "storage": _disk_usage(), } context = {"request": request} serializer = StatusSerializer(data, context=context) return Response(serializer.data)
def reset(self, request, pk=None): """ Reset the access policy to its uncustomized default value. """ access_policy = self.get_object() for plugin_config in pulp_plugin_configs(): for viewset_batch in plugin_config.named_viewsets.values(): for viewset in viewset_batch: if get_view_urlpattern( viewset) == access_policy.viewset_name: default_access_policy = viewset.DEFAULT_ACCESS_POLICY access_policy.statements = default_access_policy[ "statements"] access_policy.creation_hooks = default_access_policy.get( "creation_hooks") or default_access_policy.get( "permissions_assignment") access_policy.customized = False access_policy.save() serializer = AccessPolicySerializer( access_policy, context={"request": request}) return Response(serializer.data) raise RuntimeError("Viewset for access policy was not found.")
# If we created a new router for the parent, recursively register the children with it for child in self.children: created_routers = created_routers + child.register_with(router) return created_routers def __repr__(self): if not self.viewset: return "Root" else: return str(self.viewset) all_viewsets = [] plugin_patterns = [] # Iterate over each app, including pulpcore and the plugins. for app_config in pulp_plugin_configs(): for viewsets in app_config.named_viewsets.values(): all_viewsets.extend(viewsets) if app_config.urls_module: plugin_patterns.append(app_config.urls_module) sorted_by_depth = sorted(all_viewsets, key=lambda vs: vs._get_nest_depth()) vs_tree = ViewSetNode() for viewset in sorted_by_depth: vs_tree.add_decendent(ViewSetNode(viewset)) #: The Pulp Platform v3 API router, which can be used to manually register ViewSets with the API. root_router = routers.DefaultRouter() urlpatterns = [ url(r"^{api_root}repair/".format(api_root=API_ROOT), RepairView.as_view()),
# If we created a new router for the parent, recursively register the children with it for child in self.children: created_routers = created_routers + child.register_with(router) return created_routers def __repr__(self): if not self.viewset: return "Root" else: return str(self.viewset) all_viewsets = [] plugin_patterns = [] # Iterate over each app, including pulpcore and the plugins. for app_config in pulp_plugin_configs(): for viewset in app_config.named_viewsets.values(): all_viewsets.append(viewset) if app_config.urls_module: plugin_patterns.append(app_config.urls_module) sorted_by_depth = sorted(all_viewsets, key=lambda vs: vs._get_nest_depth()) vs_tree = ViewSetNode() for viewset in sorted_by_depth: vs_tree.add_decendent(ViewSetNode(viewset)) #: The Pulp Platform v3 API router, which can be used to manually register ViewSets with the API. root_router = routers.DefaultRouter() urlpatterns = [ url(r'^{}/'.format(ContentView.BASE_PATH), ContentView.as_view(), name='content-app'),