def ks_explorer(request): try: ks_url = urllib.parse.parse_qs( 'url=' + request.GET['ks_complete_url'])['url'][0] except: ks_url = request.POST['ks_complete_url'] try: # use the KsUrl class to clean it up ks_url = KsUrl(ks_url).url this_ks = KnowledgeServer.this_knowledge_server() # info on the remote ks ar_ks_info = ApiResponse() ar_ks_info.invoke_oks_api(ks_url, 'api_ks_info') + "?format=JSON" if ar_ks_info.status == ApiResponse.success: organization = ar_ks_info.content['DataSet']['ActualInstance'][ 'Organization'] for ks in organization['knowledgeserver_set']: if ks['this_ks']: explored_ks = ks # info about structures on the remote ks ar_ds_types = ApiResponse() ar_ds_types.invoke_oks_api(ks_url, 'api_dataset_types') + "?format=JSON" owned_structures = [] other_structures = [] for ei in ar_ds_types.content['DataSets']: entity = {} entity['actual_instance_name'] = ei['ActualInstance'][ 'DataSetStructure']['name'] entity['UKCL'] = urllib.parse.urlencode( {'': ei['ActualInstance']['DataSetStructure']['UKCL']})[1:] entity['oks_name'] = ei['knowledge_server']['name'] external_oks_url = KsUrl(ei['knowledge_server']['UKCL']).home() entity['oks_home'] = urllib.parse.urlencode( {'': external_oks_url})[1:] if ei['knowledge_server']['UKCL'] == explored_ks['UKCL']: owned_structures.append(entity) else: other_structures.append(entity) else: HttpResponse("Error invoking api_ks_info on " + ks_url + " - " + ar_ks_info.message) except Exception as ex: return HttpResponse(str(ex)) cont = RequestContext( request, { 'owned_structures': owned_structures, 'other_structures': other_structures, 'this_ks': this_ks, 'this_ks_encoded_url': this_ks.url(True), 'organization': organization, 'explored_ks': explored_ks, 'ks_url': urllib.parse.urlencode({'': ks_url})[1:] }) return render_to_response('knowledge_server/ks_explorer_entities.html', context_instance=cont)
def this_ks_subscribes_to(request, UKCL): ''' This ks is subscribing to a data set in another ks First I store the subscription locally Then I invoke remotely api_subscribe If it works I commit locally ''' UKCL = str(urllib.parse.unquote(UKCL).replace("%2F", "/")) other_ks_uri = KsUrl(UKCL).home() KnowledgeServer.get_remote_ks(other_ks_uri) try: with transaction.atomic(): encoded_UKCL = urllib.parse.urlencode({'': UKCL})[1:] # invoke remote API to subscribe this_ks = KnowledgeServer.this_knowledge_server() url_to_invoke = urllib.parse.urlencode( {'': this_ks.url() + reverse('api_notify')})[1:] ar = ApiResponse() ar.invoke_oks_api(other_ks_uri, 'api_subscribe') + ( "?UKCL=%s&remote_url=%s" % ((encoded_UKCL, url_to_invoke))) if ar.status == ApiResponse.success: # save locally sto = SubscriptionToOther() sto.URL = UKCL sto.first_version_UKCL = ar.content # it contains the UKCL of the first version sto.save() return render(request, 'knowledge_server/export.json', {'json': ar.response}, content_type="application/json") else: return render(request, 'knowledge_server/export.json', {'json': ar.response}, content_type="application/json") except Exception as ex: return HttpResponse(str(ex))
def datasets_of_type(request, ks_url, UKCL, response_format): ''' returns the list of datasets of a specific type/structure ''' this_ks = KnowledgeServer.this_knowledge_server() response_format = response_format.upper() ks_url = urllib.parse.unquote(ks_url) tmp_ks_url = KsUrl(ks_url) q_UKCL = UKCL UKCL = urllib.parse.unquote(UKCL) if this_ks.scheme != tmp_ks_url.scheme or this_ks.netloc != tmp_ks_url.netloc: # info on the remote ks ar_ks_info = ApiResponse() ar_ks_info.invoke_oks_api(ks_url, 'api_ks_info') + "?format=JSON" organization = ar_ks_info.content['DataSet']['ActualInstance'][ 'Organization'] for ks in organization['knowledgeserver_set']: if ks['this_ks']: external_ks_json = ks external_ks = KnowledgeServer() external_ks.name = external_ks_json['name'] external_ks.scheme = external_ks_json['scheme'] external_ks.netloc = external_ks_json['netloc'] external_ks.description = external_ks_json['description'] browsing_this = False else: external_ks = this_ks organization = this_ks.organization browsing_this = True # info on the DataSetStructure # TODO: the following call relies on api_catch_all; use dataset_info instead response = urlopen(UKCL + "/json") es_info_json = json_loads(response.read().decode("utf-8")) if response_format == 'XML': local_url = reverse('api_datasets') + ("?UKCL=%s&format=%s" % (q_UKCL, response_format)) if response_format == 'JSON' or response_format == 'BROWSE': local_url = reverse('api_datasets') + ("?UKCL=%s&format=JSON" % q_UKCL) response = urlopen(ks_url + local_url) datasets = response.read().decode("utf-8") if response_format == 'XML': return render(request, 'knowledge_server/export.xml', {'xml': datasets}, content_type="application/xhtml+xml") if response_format == 'JSON': return render(request, 'knowledge_server/export.json', {'json': datasets}, content_type="application/json") if response_format == 'BROWSE': # parse decoded = json_loads(datasets) # I prepare a list of UKCL of root so that I can check which I have subscribed to first_version_UKCLs = [] for ei in decoded['content']['DataSets']: if 'first_version' in ei: first_version_UKCLs.append(ei['first_version']['UKCL']) else: first_version_UKCLs.append(ei['UKCL']) subscribed = SubscriptionToOther.objects.filter( first_version_UKCL__in=first_version_UKCLs) subscribed_first_version_UKCLs = [] for s in subscribed: subscribed_first_version_UKCLs.append(s.first_version_UKCL) datasets = [] for ei in decoded['content']['DataSets']: dataset = {} if 'ActualInstance' in ei.keys(): actual_instance_class = list(ei['ActualInstance'].keys())[0] dataset['actual_instance_name'] = ei['ActualInstance'][ actual_instance_class]['name'] else: #is a view dataset['actual_instance_name'] = ei['description'] dataset['encodedUKCL'] = urllib.parse.urlencode({'': ei['UKCL']})[1:] dataset['UKCL'] = urllib.parse.quote(ei['UKCL']).replace( "/", "%2F") subscribed_UKCL = ei['first_version'][ 'UKCL'] if 'first_version' in ei else ei['UKCL'] dataset[ 'subscribed'] = subscribed_UKCL in subscribed_first_version_UKCLs datasets.append(dataset) cont = RequestContext( request, { 'browsing_this': browsing_this, 'datasets': datasets, 'organization': organization, 'this_ks': this_ks, 'this_ks_encoded_url': this_ks.url(True), 'external_ks': external_ks, 'es_info_json': es_info_json }) return render_to_response('knowledge_server/datasets_of_type.html', context_instance=cont)