def update_local_data_source(request): """Ajax method to update query with local data source Args: request: Returns: """ try: query_id = request.GET["query_id"] selected = json.loads(request.GET["selected"]) # Get query from id query = query_api.get_by_id(query_id, request.user) if selected: # Local data source is selected, add it to the query as a data source add_local_data_source(request, query) else: # Local data source is not selected, remove it from the query local_query_url = get_local_query_absolute_url(request) data_source = query_api.get_data_source_by_name_and_url_query( query, LOCAL_QUERY_NAME, local_query_url, request.user ) query_api.remove_data_source(query, data_source, request.user) return HttpResponse() except Exception as e: return HttpResponseBadRequest(escape(str(e)))
def test_remove_data_source(self, mock_save): # create query query = _create_query() mock_save.return_value = query origin_data_sources = len(query.data_sources) data_source = query.data_sources[0] query_api.remove_data_source(query, data_source) self.assertTrue(len(query.data_sources) == origin_data_sources - 1)
def update_data_source_list_federated(request): """ Ajax method to update query data sources in bdd. Args: request: Returns: """ try: id_query = request.GET.get('id_query', None) id_instance = request.GET.get('id_instance', None) added = request.GET.get('to_be_added', None) to_be_added = json.loads(added) if added is not None else False # Get query from id if id_query is not None: query = api_query.get_by_id(id_query) # Get instance from id if id_instance is not None: instance = instance_api.get_by_id(id_instance) # Generate url from instance information url = _get_url_with_federated_rest_extension(instance) if to_be_added: # Instance have to be added in the query as a datasource authentication = Authentication( type='oauth2', params={'access_token': instance.access_token}) data_source = DataSource(name=instance.name, url_query=url, authentication=authentication) data_source.query_options = {'instance_name': instance.name} api_query.add_data_source(query, data_source) else: # Data source have to be remove from the query data_source = api_query.get_data_source_by_name_and_url_query( query, instance.name, url) api_query.remove_data_source(query, data_source) return HttpResponse() else: return HttpResponseBadRequest( "Error during data source selection.") except Exception as e: return HttpResponseBadRequest("Error during data source selection.")
def update_data_source_list_oaipmh(request): """ Ajax method to update query data sources in data base. Args: request: Returns: """ try: id_query = request.GET.get('id_query', None) id_instance = request.GET.get('id_instance', None) added = request.GET.get('to_be_added', None) to_be_added = json.loads(added) if added is not None else False # Get query from id if id_query is not None: query = api_query.get_by_id(id_query) url_instance = request.build_absolute_uri(reverse("core_explore_oaipmh_rest_execute_query")) instance = oai_registry_api.get_by_id(id_instance) if to_be_added: # Instance have to be added in the query as a data source authentication = Authentication(type='session') data_source = DataSource(name=instance.name, url_query=url_instance, authentication=authentication) data_source.query_options = {'instance_id': str(instance.id)} api_query.add_data_source(query, data_source) else: # Data source have to be remove from the query data_source = api_query.get_data_source_by_name_and_url_query(query, instance.name, url_instance) api_query.remove_data_source(query, data_source) return HttpResponse() else: return HttpResponseBadRequest("Error during data source selection.") except Exception as e: return HttpResponseBadRequest("Error during data source selection: %s" % e.message)
def remove_oaipmh_data_source(query, instance_id, user): """Remove an oaipmh data source to the query Args: query: instance_id: Returns: """ data_source = None for data_source_item in query.data_sources: if ("instance_id" in data_source_item.query_options and data_source_item.query_options["instance_id"] == instance_id): data_source = data_source_item if data_source: return api_query.remove_data_source(query, data_source, user)
def update_data_source_list_federated(request): """Ajax method to update query data sources in bdd. Args: request: Returns: """ try: id_query = request.GET.get("id_query", None) id_instance = request.GET.get("id_instance", None) added = request.GET.get("to_be_added", None) to_be_added = json.loads(added) if added is not None else False # Get query from id if id_query is not None: query = api_query.get_by_id(id_query, request.user) # Get instance from id if id_instance is not None: instance = instance_api.get_by_id(id_instance) # Generate url from instance information url = _get_url_with_federated_rest_extension(instance) if to_be_added: # Instance have to be added in the query as a datasource authentication = Authentication( type="oauth2", params={"access_token": instance.access_token}) data_source = DataSource( name=instance.name, url_query=url, authentication=authentication, order_by_field=",".join(settings.DATA_SORTING_FIELDS), ) data_source.query_options = {"instance_name": instance.name} if "core_linked_records_app" in settings.INSTALLED_APPS: data_source.capabilities = { "url_pid": urljoin( instance.endpoint, reverse("core_linked_records_app_query_local"), ) } api_query.add_data_source(query, data_source, request.user) else: # Data source have to be remove from the query data_source = api_query.get_data_source_by_name_and_url_query( query, instance.name, url, request.user) api_query.remove_data_source(query, data_source, request.user) return HttpResponse() else: return HttpResponseBadRequest( "Error during data source selection.") except AccessControlError: return HttpResponseForbidden("Access Forbidden") except Exception: return HttpResponseBadRequest("Error during data source selection.")