def put(self, request, upload_id, offset): """ Upload to a specific file upload. :param request: WSGI request object, body contains bits to upload :type request: django.core.handlers.wsgi.WSGIRequest :param upload_id: id of the initialized upload :type upload_id: str :param offset: place in the uploaded file to start writing :type offset: str of an integer :return: response containing null :rtype: django.http.HttpResponse :raises: pulp.server.exceptions.MissingResource if upload ID does not exist :raises: InvalidValue if offset cannot be converted to an integer """ try: offset = int(offset) except ValueError: raise InvalidValue(["offset"]) upload_manager = factory.content_upload_manager() # If the upload ID doesn't exists, either because it was not initialized # or was deleted, the call to the manager will raise missing resource upload_manager.save_data(upload_id, offset, request.body) return generate_json_response(None)
def get(self, request, repo_id, importer_id): """ Retrieve a list of all scheduled syncs for the given importer and repo. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param importer_id: retrieve the scheduled syncs of this importer :type importer_id: str :return: Response containing a list of dicts, one for each scheduled sync :rtype : django.http.HttpResponse """ manager = manager_factory.repo_sync_schedule_manager() schedules = manager.list(repo_id, importer_id) for_display = [schedule.for_display() for schedule in schedules] for entry in for_display: entry['_href'] = reverse( 'repo_sync_schedule_resource', kwargs={'repo_id': repo_id, 'importer_id': importer_id, 'schedule_id': entry['_id']} ) return generate_json_response(for_display)
def post(self, request): """ Grant permissions to a role. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: An empty response :rtype: django.http.HttpResponse """ params = request.body_as_json role_id = params.get('role_id', None) resource = params.get('resource', None) operation_names = params.get('operations', None) _check_invalid_params({'role_id': role_id, 'resource': resource, 'operation_names': operation_names}) # Grant permission synchronously role_manager = factory.role_manager() permission_manager = factory.permission_manager() operations = permission_manager.operation_names_to_values(operation_names) add_perm = role_manager.add_permissions_to_role(role_id, resource, operations) return generate_json_response(add_perm)
def put(self, request, repo_id, importer_id, schedule_id): """ Update a scheduled repository sync. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param importer_id: id of the importer :type importer_id: str :param schedule_id: id of the scheduled repository sync to update :type schedule)id: str :return: information about the updated scheduled sync :rtype: django.http.HttpResponse """ if 'schedule' in request.body_as_json: request.body_as_json['iso_schedule'] = request.body_as_json.pop('schedule') schedule = self.manager.update(repo_id, importer_id, schedule_id, request.body_as_json) ret = schedule.for_display() ret['_href'] = reverse( 'repo_sync_schedule_resource', kwargs={'repo_id': repo_id, 'importer_id': importer_id, 'schedule_id': schedule_id} ) return generate_json_response(ret)
def get(self, request, type_id): """ Return a response containing a dict with info about a content type. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param type_id: type of content unit :type type_id: str :return: response containing a dict that contains info about a content type or 404 response if the specified content type is not found. :rtype : django.http.HttpResponse or HttpResponseNotFound """ cqm = factory.content_query_manager() content_type = cqm.get_content_type(type_id) if content_type is None: msg = _('No content type resource: %(r)s') % {'r': type_id} return generate_json_response(msg, response_class=HttpResponseNotFound) resource = serialization.content.content_type_obj(content_type) # These urls are not valid endpoints but are left here for for semantic versioning. # BZ - 1187287 links = { 'actions': {'_href': '/'.join([request.get_full_path().rstrip('/'), 'actions/'])}, 'content_units': {'_href': '/'.join([request.get_full_path().rstrip('/'), 'units/'])} } resource.update(links) return generate_json_response_with_pulp_encoder(resource)
def put(self, request, repo_id, distributor_id, schedule_id): """ Update a scheduled publish. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param distributor_id: id of the distributor :type distributor_id: str :param schedule_id: id of the scheduled publish to be updated :type schedule_id: str :return: Response containing serialized dict for the scheduled publish :rtype : django.http.HttpResponse """ updates = request.body_as_json if 'schedule' in updates: updates['iso_schedule'] = updates.pop('schedule') schedule = self.manager.update(repo_id, distributor_id, schedule_id, updates) ret = schedule.for_display() ret['_href'] = reverse('repo_publish_schedule_resource', kwargs={ 'repo_id': repo_id, 'distributor_id': distributor_id, 'schedule_id': schedule_id }) return generate_json_response(ret)
def post(self, request): """ Revoke permissions from a role. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: An empty response :rtype: django.http.HttpResponse """ params = request.body_as_json role_id = params.get('role_id', None) resource = params.get('resource', None) operation_names = params.get('operations', None) _validate_params({'role_id': role_id, 'resource': resource, 'operation_names': operation_names}) role_manager = factory.role_manager() permission_manager = factory.permission_manager() operations = permission_manager.operation_names_to_values(operation_names) remove_perm = role_manager.remove_permissions_from_role(role_id, resource, operations) return generate_json_response(remove_perm)
def _get(self, schedule_id, resource_href): """ Gets and returns a schedule by ID, in dict form suitable for json serialization and end-user presentation. :param resource_href: href of the schedule :type resource_href: str :param schedule_id: unique ID of a schedule :type schedule_id: basestring :return: dictionary representing the schedule :rtype: dict :raise pulp.server.exceptions.MissingResource: if schedule is not found """ # If schedule_id is not a valid bson ObjectId, this will raise InvalidValue. If the # schedule_id is a valid bson ObjectId but doesn't exist it will raise StopIteration. # Either should be a 404. try: schedule = next(iter(schedule_utils.get([schedule_id]))) except (StopIteration, pulp_exceptions.InvalidValue): raise pulp_exceptions.MissingResource(schedule_id=schedule_id) ret = schedule.for_display() ret['_href'] = resource_href return generate_json_response(ret)
def post(self, request, repo_id, importer_id): """ Create a new scheduled sync. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param importer_id: create a new scheduled sync for this importer :type importer_id: str :return: Response containing a serialized dict of the new scheduled sync :rtype : django.http.HttpResponse :raises exceptions.UnsupportedValue: if there are unsupported request body params """ manager = manager_factory.repo_sync_schedule_manager() sync_options = {'override_config': request.body_as_json.pop('override_config', {})} schedule = request.body_as_json.pop('schedule', None) failure_threshold = request.body_as_json.pop('failure_threshold', None) enabled = request.body_as_json.pop('enabled', True) if request.body_as_json: raise exceptions.UnsupportedValue(request.body_as_json.keys()) scheduled_call = manager.create(repo_id, importer_id, sync_options, schedule, failure_threshold, enabled) display_call = scheduled_call.for_display() display_call['_href'] = reverse( 'repo_sync_schedule_resource', kwargs={'repo_id': repo_id, 'importer_id': importer_id, 'schedule_id': scheduled_call['id']} ) response = generate_json_response(display_call) return generate_redirect_response(response, display_call['_href'])
def format_results(self, data, get_dict, path): """ Format the results and begin streaming out to the caller for the v3 API :param data: The module data to stream back to the caller :type data: dict :param get_dict: The GET parameters :type get_dict: dict :param path: The path starting with parameters :type get_dict: dict :return: the body of what should be streamed out to the caller :rtype: str """ module_name = get_dict.get("module", "") path_parameter = get_dict.get("path", None) module_list = data.get(module_name) if not path_parameter: limit = int(get_dict.get("limit", 20)) current_offset = int(get_dict.get("offset", 0)) module_version = get_dict.get("version", None) first_path = self._format_query_string(path, module_name, module_version, 0, limit) current_path = self._format_query_string(path, module_name, module_version, current_offset, limit) if current_offset > 0: previous_path = self._format_query_string( path, module_name, module_version, current_offset - limit, limit ) else: previous_path = None formatted_results = { "pagination": { "limit": limit, "offset": current_offset, "first": first_path, "previous": previous_path, "current": current_path, "next": None, "total": 1, }, "results": [], } total_count = len(module_list) for module in module_list[current_offset : (current_offset + limit)]: formatted_module = self._format_module(module_name, module) formatted_results["results"].append(formatted_module) formatted_results["pagination"]["total"] = total_count if total_count > (current_offset + limit): next_path = self._format_query_string(path, module_name, module_version, current_offset + limit, limit) formatted_results["pagination"]["next"] = next_path else: formatted_module = self._format_module(module_name, module_list[0]) formatted_results = formatted_module return generate_json_response(formatted_results)
def test_generate_json_response_not_found(self): """ Test that response is correct for non-base HttpResponses """ response = util.generate_json_response(None, HttpResponseNotFound) self.assertTrue(isinstance(response, HttpResponseNotFound)) self.assertEqual(response.status_code, 404) self.assertEqual(response._headers.get('content-type'), ('Content-Type', 'application/json'))
def format_results(self, data, get_dict, path): """ Format the results and begin streaming out to the caller :param data: The module data to stream back to the caller :type data: dict :param get_dict: The GET parameters :type get_dict: dict :return: the body of what should be streamed out to the caller :rtype: str """ return generate_json_response(data)
def test_generate_json_response_default_params(self): """ Make sure that the response is correct under normal conditions. """ test_content = {'foo': 'bar'} response = util.generate_json_response(test_content) self.assertTrue(isinstance(response, HttpResponse)) self.assertEqual(response.status_code, 200) self.assertEqual(response._headers.get('content-type'), ('Content-Type', 'application/json')) response_content = json.loads(response.content) self.assertEqual(response_content, test_content)
def get(self, request, type_id, unit_id): """ Return user metadata for a content unit. :param type_id: The Unit's type id. :type type_id: basestring :param unit_id: The id of the unit that you wish to set the pulp_user_metadata field on :type unit_id: basestring :return: response containing pulp user metadata field :rtype: django.http.HttpResponse or HttpResponseNotFound """ cqm = factory.content_query_manager() try: unit = cqm.get_content_unit_by_id(type_id, unit_id) except MissingResource: msg = _("No content unit resource: %(r)s") % {"r": unit_id} return generate_json_response(msg, HttpResponseNotFound) resource = serial_content.content_unit_obj(unit[constants.PULP_USER_METADATA_FIELDNAME]) return generate_json_response(resource)
def post(self, request): """ Return client SSL certificate and a private key. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: Response containing cert and key :rtype: django.http.HttpResponse """ user = factory.principal_manager().get_principal() key, certificate = factory.cert_generation_manager().make_admin_user_cert(user) key_cert = {'key': key, 'certificate': certificate} return generate_json_response(key_cert)
def get(self, request): """ Return a serialized response containing a dict with a list of upload_ids. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: Serialized response containing a list of upload ids :rtype: django.http.HttpResponse """ upload_manager = factory.content_upload_manager() upload_ids = upload_manager.list_upload_ids() return generate_json_response({"upload_ids": upload_ids})
def delete(self, request, consumer_group_id): """ Delete a specified consumer group. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_group_id: id for the requested group :type consumer_group_id: str :return: An empty response :rtype: django.http.HttpResponse """ manager = factory.consumer_group_manager() result = manager.delete_consumer_group(consumer_group_id) return generate_json_response(result)
def delete(self, request, task_id): """ Dispatch tasks.cancel to delete a single task. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param task_id: The ID of the task you wish to cancel :type task_id: basestring :return: Response containing None :rtype: django.http.HttpResponse """ tasks.cancel(task_id) return generate_json_response(None)
def delete(self, request, upload_id): """ Delete a single upload. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param upload_id: id of the upload to be deleted :type upload_id: str :return: response with None :rtype: django.http.HttpResponse """ upload_manager = factory.content_upload_manager() upload_manager.delete_upload(upload_id) return generate_json_response(None)
def delete(self, request, role_id): """ Delete a role. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param role_id: id for the requested role :type role_id: str :return: An empty response :rtype: django.http.HttpResponse """ manager = factory.role_manager() result = manager.delete_role(role_id) return generate_json_response(result)
def post(self, request, *args, **kwargs): """ Initialize an upload and return a serialized dict containing the upload data. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return : Serialized response containing a url to delete an upload and a unique id. :rtype : django.http.HttpResponse """ upload_manager = factory.content_upload_manager() upload_id = upload_manager.initialize_upload() href = reverse("content_upload_resource", kwargs={"upload_id": upload_id}) response = generate_json_response({"_href": href, "upload_id": upload_id}) response_redirect = generate_redirect_response(response, href) return response_redirect
def put(self, request, type_id, unit_id): """ Set the pulp_user_metadata field on a content unit. :param type_id: The Unit's type id. :type type_id: basestring :param unit_id: The id of the unit that you wish to set the pulp_user_metadata field on :type unit_id: basestring :return: response containing pulp user metadata_field :rtype: django.http.HttpResponse or HttpResponseNotFound """ params = request.body_as_json cqm = factory.content_query_manager() try: cqm.get_content_unit_by_id(type_id, unit_id) except MissingResource: msg = _("No content unit resource: %(r)s") % {"r": unit_id} return generate_json_response(msg, HttpResponseNotFound) cm = factory.content_manager() delta = {constants.PULP_USER_METADATA_FIELDNAME: params} cm.update_content_unit(type_id, unit_id, delta) return generate_json_response(None)
def delete(self, request, repo_group_id): """ Delete the specified repo group. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_group_id: id of repo group to delete :type repo_group_id: str :return: An empty response :rtype: django.http.HttpResponse """ manager = managers_factory.repo_group_manager() manager.delete_repo_group(repo_group_id) return generate_json_response(None)
def delete(self, request, source_id): """ Delete all entries from the catlog that have the provided source id :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param source_id: id of source whose content should be deleted :type source_id: str :return: response containing a dict containing the number if items deleted :rtype : django.http.HttpResponse """ manager = factory.content_catalog_manager() purged = manager.purge(source_id) deleted_info = dict(deleted=purged) return generate_json_response(deleted_info)
def get(self, request): """ Return a response containing a serialized list of importers present in the server. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return : Response containing a serialized list of dicts containing importer data :rtype : django.http.HttpResponse """ manager = factory.plugin_manager() all_importers = manager.importers() for importer in all_importers: importer['_href'] = '/'.join([request.get_full_path().rstrip('/'), importer['id'], '']) return generate_json_response(all_importers)
def delete(self, request, event_listener_id): """ Delete an event listener. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param event_listener_id: id for the requested event listener :type event_listener_id: str :return: An empty response :rtype: django.http.HttpResponse """ manager = factory.event_listener_manager() manager.delete(event_listener_id) # will raise MissingResource return generate_json_response(None)
def delete(self, request, consumer_id): """ Delete a specified consumer. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: id for the requested consumer :type consumer_id: str :return: An empty response :rtype: django.http.HttpResponse """ manager = factory.consumer_manager() response = manager.unregister(consumer_id) return generate_json_response(response)
def delete(self, request, role_id, login): """ Remove user from a role. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param role_id: id for the requested role :type role_id: str :param login: id for the requested user :type login: str :return: An empty response :rtype: django.http.HttpResponse """ role_manager = factory.role_manager() remove_user = role_manager.remove_user_from_role(role_id, login) return generate_json_response(remove_user)
def delete(self, request, consumer_id, schedule_id): """ Delete a specific schedule <action>. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: The consumer ID. :type consumer_id: str :param schedule_id: the schedule id :type schedule_id: str :return: An empty response :rtype: django.http.HttpResponse """ self.manager.delete_schedule(consumer_id, schedule_id) return generate_json_response(None)
def get(self, request): """ Return a response containing a serialized list of importers present in the server. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return : Response containing a serialized list of dicts containing importer data :rtype : django.http.HttpResponse """ manager = factory.plugin_manager() all_importers = manager.importers() for importer in all_importers: importer['_href'] = '/'.join( [request.get_full_path().rstrip('/'), importer['id'], '']) return generate_json_response(all_importers)
def get(self, request): """ Return response containing a serialized list of dicts, one for each distributor. :param request: WSGI Request object :type request: django.core.handlers.wsgi.WSGIRequest :return : Response containing a serialized list of dicts, one for each distributor :rtype : django.http.HttpResponse """ manager = factory.plugin_manager() all_distributors = manager.distributors() for distributor in all_distributors: distributor['_href'] = '/'.join([request.get_full_path().rstrip('/'), distributor['id'], '']) return generate_json_response(all_distributors)
def get(self, request, content_type, unit_id): """ Return a serialized object representing the requested orphan :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param content_type: content type of the requested orphan :type content_type: str :param unit_id: id of the requested unit :type unit_id: str :return: response conainting a serialized dict of the requested orphan :rtype : django.http.HttpResponse """ orphan_manager = factory.content_orphan_manager() orphan_dict = orphan_manager.get_orphan(content_type, unit_id) orphan_dict['_href'] = request.get_full_path() return generate_json_response(orphan_dict)
def get(self, request, content_type, unit_id): """ Return a serialized object representing the requested orphan :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param content_type: content type of the requested orphan :type content_type: str :param unit_id: id of the requested unit :type unit_id: str :return: response conainting a serialized dict of the requested orphan :rtype : django.http.HttpResponse """ orphan_manager = factory.content_orphan_manager() orphan_dict = orphan_manager.get_orphan(content_type, unit_id) orphan_dict["_href"] = request.get_full_path() return generate_json_response(orphan_dict)
def delete(self, request, consumer_id, content_type): """ Delete an association between the specified consumer and profile. Designed to be idempotent. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :param content_type: The content type ID. :type content_type: str :return: An empty response :rtype: django.http.HttpResponse """ manager = factory.consumer_profile_manager() response = manager.delete(consumer_id, content_type) return generate_json_response(response)
def delete(self, request, login): """ Delete a user. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param login: login for the requested user :type login: str :return: An empty response :rtype: django.http.HttpResponse """ user_controller.delete_user(login) # Delete any existing user permissions given to the creator of the user link = reverse('user_resource', kwargs={'login': login}) if Permission.get_collection().find_one({'resource': link}): Permission.get_collection().remove({'resource': link}) return generate_json_response()
def post(self, request, *args, **kwargs): """ Initialize an upload and return a serialized dict containing the upload data. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return : Serialized response containing a url to delete an upload and a unique id. :rtype : django.http.HttpResponse """ upload_manager = factory.content_upload_manager() upload_id = upload_manager.initialize_upload() href = reverse('content_upload_resource', kwargs={'upload_id': upload_id}) response = generate_json_response({ '_href': href, 'upload_id': upload_id }) response_redirect = generate_redirect_response(response, href) return response_redirect
def post(self, request, repo_group_id): """ Unassociate repos that match criteria specified in the body to the specified repo group. Call is idempotent. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_group_id: matching repos are unassociated with this repo group :type repo_group_id: str :return: Response containing a serialized list of unassociated repository names :rtype: django.http.HttpResponse """ criteria = Criteria.from_client_input( request.body_as_json.get('criteria', {})) manager = managers_factory.repo_group_manager() manager.unassociate(repo_group_id, criteria) collection = RepoGroupModel.get_collection() group = collection.find_one({'id': repo_group_id}) return generate_json_response(group['repo_ids'])
def get(self, request): """ Returns a response continaing a list of dicts, one for each available content type. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: response containing a serialized list dicts, one for each content type :rtype : django.http.HttpResponse """ collection = [] cqm = factory.content_query_manager() type_ids = cqm.list_content_types() for type_id in type_ids: link = { '_href': reverse('content_type_resource', kwargs={'type_id': type_id}) } link.update({'content_type': type_id}) collection.append(link) return generate_json_response(collection)
def delete(self, request, repo_group_id, distributor_id): """ Disassociate a distributor from a repository group. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_group_id: repo group the distributor is associated with :type repo_group_id: str :param distributor_id: distributor to disassociate :type distributor_id: str :return: An empty response :rtype: django.http.HttpResponse """ params = request.body_as_json force = params.get('force', False) distributor_manager = managers_factory.repo_group_distributor_manager() distributor_manager.remove_distributor(repo_group_id, distributor_id, force=force) return generate_json_response(None)
def delete(self, request, group_id): """ Dispatch tasks.cancel to delete tasks in a single task_group. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param group_id: The ID of the task group you wish to cancel :type group_id: basestring :return: Response containing None :rtype: django.http.HttpResponse :raises MissingResource: if group id is not found """ raw_tasks = TaskStatus.objects.only('task_id').filter( group_id=group_id) if not raw_tasks: raise MissingResource for task in raw_tasks: tasks.cancel(task.task_id) return generate_json_response(None)
def post(self, request, role_id): """ Add user to a role. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param role_id: id for the requested role :type role_id: str :return: An empty response :rtype: django.http.HttpResponse :raises: InvalidValue some parameters are invalid """ params = request.body_as_json login = params.get('login', None) if login is None: raise pulp_exceptions.InvalidValue(login) role_manager = factory.role_manager() add_user = role_manager.add_user_to_role(role_id, login) return generate_json_response(add_user)
def get(self, request, distributor_id): """ Return a response contaning serialized data for the specified distributor. :param request : WSGI request object :type request : django.core.handlers.wsgi.WSGIRequest :param distributor_id: id of distributor to match :type distributor_id: string :return : Response containing serialized data for the specified distributor :rtype : django.http.HttpResponse :raises : MissingResource if distributor_id is not found """ manager = factory.plugin_manager() all_distributors = manager.distributors() for distributor in all_distributors: if distributor['id'] == distributor_id: distributor['_href'] = request.get_full_path() return generate_json_response(distributor) raise MissingResource(distributor_type_id=distributor_id)
def get(self, request, importer_id): """ Return a response containing serialized data for the specified importer. :param request : WSGI request object :type request : django.core.handlers.wsgi.WSGIRequest :param importer_id : name of importer to return information for :type importer_id : string :return : Response containing serialized data for specified importer :rtype : django.http.HttpResponse :raises : MissingResource if importer_id cannot be found """ manager = factory.plugin_manager() all_importers = manager.importers() for importer in all_importers: if importer['id'] == importer_id: importer['_href'] = request.get_full_path() return generate_json_response(importer) raise MissingResource(importer_type_id=importer_id)
def delete(self, request, repo_id, importer_id, schedule_id): """ Remove a scheduled repository sync from the importer. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param importer_id: remove the scheduled sync from this importer :type importer_id: str :param schedule_id: id of the scheduled repository sync to delete :type schedule)id: str :return: An empty response :rtype: django.http.HttpResponse :raises exceptions.MissingResource: if schedule_id/importer_id/repo_id does not exist """ try: self.manager.delete(repo_id, importer_id, schedule_id) except exceptions.InvalidValue: raise exceptions.MissingResource(schedule_id=schedule_id) return generate_json_response(None)
def get(self, request, repo_id, distributor_id): """ Retrieve information about all scheduled publishes. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param distributor_id: id of the distributor :type distributor_id: str :return: Response containing serialized list of dicts, one for each scheduled publish :rtype : django.http.HttpResponse """ manager = manager_factory.repo_publish_schedule_manager() schedules = manager.list(repo_id, distributor_id) for_display = [schedule.for_display() for schedule in schedules] for entry in for_display: entry['_href'] = reverse('repo_publish_schedule_resource', kwargs={ 'repo_id': repo_id, 'distributor_id': distributor_id, 'schedule_id': entry['_id'] }) return generate_json_response(for_display)
def get(self, request, content_type): """ Returns a response containing a serialized list of all orphans of the specified type. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param content_type: restrict the list of orphans to this content type :type content_type: str :return: response containing a serialized list of all orphans of specified type. :rtype : django.http.HttpResponse """ orphan_manager = factory.content_orphan_manager() matched_orphans = list( orphan_manager.generate_orphans_by_type_with_unit_keys( content_type)) for orphan_dict in matched_orphans: orphan_dict['_href'] = reverse('content_orphan_resource', kwargs={ 'content_type': content_type, 'unit_id': orphan_dict['_id'] }) return generate_json_response(matched_orphans)
def post(self, request): """ Revoke permissions from a user. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: An empty response :rtype: django.http.HttpResponse """ params = request.body_as_json login = params.get('login', None) resource = params.get('resource', None) operation_names = params.get('operations', None) _validate_params({'login': login, 'resource': resource, 'operation_names': operation_names}) permission_manager = factory.permission_manager() operations = permission_manager.operation_names_to_values(operation_names) revoke_perm = permission_manager.revoke(resource, login, operations) return generate_json_response(revoke_perm)
def delete(self, request, repo_id, distributor_id, schedule_id): """ Remove a scheduled publish. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param repo_id: id of the repository :type repo_id: str :param distributor_id: id of the distributor :type distributor_id: str :param schedule_id: id of the scheduled publish to be removed :type schedule_id: str :return: An empty response :rtype : django.http.HttpResponse """ try: self.manager.delete(repo_id, distributor_id, schedule_id) except exceptions.InvalidValue: raise exceptions.MissingResource(schedule_id=schedule_id) return generate_json_response(None)
def get(self, request): """ Return a response containing a dict of dicts, one for each orphaned unit. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :return: response containing a dict that itself contains a dict for each orphan. :rtype: django.http.HttpResponse """ orphan_manager = factory.content_orphan_manager() # convert the counts into sub-documents so we can add _href fields to them # add links to the content type sub-collections rest_summary = {} for key, value in orphan_manager.orphans_summary().items(): rest_summary[key] = { 'count': value, '_href': reverse('content_orphan_type_subcollection', kwargs={'content_type': key}) } return generate_json_response(rest_summary)
def format_results(self, data, get_dict, path, module_name): """ Format the results and begin streaming out to the caller for the v3 API :param data: The module data to stream back to the caller :type data: dict :param get_dict: The GET parameters :type get_dict: dict :param path: The path starting with parameters :type path: str :param module_name: The module name with author :type module_name: unicode :return: the body of what should be streamed out to the caller :rtype: str """ # module_name = get_dict.get('module', '') # clean_name = self._get_module_name(module_name) clean_name = module_name.split('/')[1] clean_author = self._get_module_author(module_name) module_slug = clean_author + '-' + clean_name formatted_results = { 'uri': '/v3/modules/' + module_slug, 'slug': module_slug, 'name': clean_name, 'created_at': '2015-09-11 07:22:37 -0700', 'updated_at': '2016-01-06 12:58:15 -0800', # 'supported': false, 'endorsement': None, 'module_group': 'base', 'current_release': { 'module': { 'uri': '/v3/modules/' + module_slug, 'slug': module_slug, 'name': clean_name, 'owner': { 'slug': clean_author, 'username': clean_author } }, }, 'releases': [] } module_list = data.get(module_name) versions = [] module_data = {} for module in module_list: formatted_dependencies = [] for dep in module.get('dependencies', []): formatted_dependencies.append({ 'name': dep[0], 'version_requirement': dep[1] }) versions.append(module.get('version')) module_data[module.get('version')] = { 'metadata': { 'name': module_slug, 'version': module.get('version'), 'dependencies': formatted_dependencies }, 'file_uri': module.get('file'), 'file_md5': module.get('file_md5'), 'version': module.get('version'), 'slug': module_slug + '-' + module.get('version') } versions.sort(key=StrictVersion) current_version = versions.pop() for attribute, value in module_data[current_version].iteritems(): formatted_results['current_release'][attribute] = value release_data = [] for version, module in module_data.iteritems(): release_data.append({ 'uri': '/v3/releases/' + str(module['slug']), 'slug': module['slug'], 'version': version, 'supported': False, 'created_at': None, 'deleted_at': None }) formatted_results['releases'] = release_data return generate_json_response(formatted_results)
def format_results(self, data, get_dict, path): """ Format the results and begin streaming out to the caller for the v3 API :param data: The module data to stream back to the caller :type data: dict :param get_dict: The GET parameters :type get_dict: dict :param path: The path starting with parameters :type get_dict: dict :return: the body of what should be streamed out to the caller :rtype: str """ limit = int(get_dict.get('limit', 20)) current_offset = int(get_dict.get('offset', 0)) module_name = get_dict.get('module', '') module_version = get_dict.get('version', None) first_path = self._format_query_string(path, module_name, module_version, 0, limit) current_path = self._format_query_string(path, module_name, module_version, current_offset, limit) if current_offset > 0: previous_path = self._format_query_string(path, module_name, module_version, current_offset - limit, limit) else: previous_path = None formatted_results = { 'pagination': { 'limit': limit, 'offset': current_offset, 'first': first_path, 'previous': previous_path, 'current': current_path, 'next': None, 'total': 1 }, 'results': [] } module_list = data.get(self._get_module_name(get_dict)) total_count = len(module_list) for module in module_list[current_offset: (current_offset + limit)]: formatted_dependencies = [] for dep in module.get('dependencies', []): formatted_dependencies.append({ 'name': dep[0], 'version_requirement': dep[1] }) module_data = { 'metadata': { 'name': module_name, 'version': module.get('version'), 'dependencies': formatted_dependencies }, 'file_uri': module.get('file'), 'file_md5': module.get('file_md5') } formatted_results['results'].append(module_data) formatted_results['pagination']['total'] = total_count if total_count > (current_offset + limit): next_path = self._format_query_string(path, module_name, module_version, current_offset + limit, limit) formatted_results['pagination']['next'] = next_path return generate_json_response(formatted_results)
def format_results(self, data, get_dict, path): """ Format the results and begin streaming out to the caller for the v3 API :param data: The module data to stream back to the caller :type data: dict :param get_dict: The GET parameters :type get_dict: dict :param path: The path starting with parameters :type get_dict: dict :return: the body of what should be streamed out to the caller :rtype: str """ module_name = get_dict.get('module', '') path_parameter = get_dict.get('path', None) module_list = data.get(module_name) if not path_parameter: limit = int(get_dict.get('limit', 20)) current_offset = int(get_dict.get('offset', 0)) module_version = get_dict.get('version', None) first_path = self._format_query_string(path, module_name, module_version, 0, limit) current_path = self._format_query_string(path, module_name, module_version, current_offset, limit) if current_offset > 0: previous_path = self._format_query_string( path, module_name, module_version, current_offset - limit, limit) else: previous_path = None formatted_results = { 'pagination': { 'limit': limit, 'offset': current_offset, 'first': first_path, 'previous': previous_path, 'current': current_path, 'next': None, 'total': 1 }, 'results': [] } total_count = len(module_list) for module in module_list[current_offset:(current_offset + limit)]: formatted_module = self._format_module(module_name, module) formatted_results['results'].append(formatted_module) formatted_results['pagination']['total'] = total_count if total_count > (current_offset + limit): next_path = self._format_query_string(path, module_name, module_version, current_offset + limit, limit) formatted_results['pagination']['next'] = next_path else: formatted_module = self._format_module(module_name, module_list[0]) formatted_results = formatted_module return generate_json_response(formatted_results)