def get(self, request, consumer_id, repo_id, distributor_id): """ Fetch a specific bind object which represents a specific association between a consumer and repo-distributor. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :param repo_id: A repo ID. :type repo_id: str :param distributor_id: A distributor ID. :type distributor_id: str :return: Response representing the binding :rtype: django.http.HttpResponse """ manager = factory.consumer_bind_manager() bind = manager.get_bind(consumer_id, repo_id, distributor_id) serialized_bind = serial_binding.serialize(bind) return generate_json_response_with_pulp_encoder(serialized_bind)
def get(self, request, consumer_id, repo_id=None): """ Fetch all bind objects referencing the specified consumer_id. Optionally, specify a repo_id to fetch all bind objects for the consumer_id to the repo_id. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: The specified consumer. :type consumer_id: str :param repo_id: The repository to retrieve bindings for (optional) :type repo_id: str :raises MissingResource: if some resource is missing :return: Response representing the bindings :rtype: django.http.HttpResponse """ # Check to make sure the resources exist missing_resources = {} if repo_id is not None: repo = model.Repository.objects(repo_id=repo_id).first() if repo is None: missing_resources['repo_id'] = repo_id # If get_consumer raises MissingResource we might miss reporting a bad repo_id try: factory.consumer_manager().get_consumer(consumer_id) except MissingResource: missing_resources['consumer_id'] = consumer_id if missing_resources: raise MissingResource(**missing_resources) manager = factory.consumer_bind_manager() bindings = manager.find_by_consumer(consumer_id, repo_id) bindings = [serial_binding.serialize(b) for b in bindings] return generate_json_response_with_pulp_encoder(bindings)
def expand_consumers(details, bindings, consumers): """ Expand a list of users based on the flag specified in the query parameters. The _href is always added by the serialization function used. Supported options: details - include details bindings - include bindings :param details: if True, details will be included in the response :type details: bool :param bindings: if True, bindings will be included with each returned consumer :type bindings: bool :param consumers: A list of consumers :type consumers: list :return: A list of expanded consumers. :rtype: list """ if details: bindings = True # add bindings if bindings: ids = [c['id'] for c in consumers] manager = factory.consumer_bind_manager() criteria = Criteria({'consumer_id': {'$in': ids}}) bindings = manager.find_by_criteria(criteria) collated = {} for b in bindings: lst = collated.setdefault(b['consumer_id'], []) lst.append(b) for c in consumers: c['bindings'] = [ serial_binding.serialize(b, False) for b in collated.get(c['id'], []) ] return consumers