def test_get_get_oauth_hs_one_hydroshare(self, mock_logger, mock_refresh_user_token, mock_hs_r): mock_social_auth_obj = mock.MagicMock() mock_backend_instance = mock.MagicMock() mock_backend_instance.name = 'hydroshare' mock_backend_instance.auth_server_hostname = 'foo' mock_data1 = { 'id': 'id', 'access_token': 'my_access_token', 'token_type': 'my_token_type', 'expires_in': 'my_expires_in', 'expires_at': 'my_expires_at', 'refresh_token': 'my_refresh_token', 'scope': 'my_scope' } mock_request = mock.MagicMock() mock_request.user.social_auth.all.return_value = [mock_social_auth_obj] mock_social_auth_obj.get_backend_instance.return_value = mock_backend_instance mock_social_auth_obj.extra_data.return_value = mock_data1 mock_refresh_user_token.return_value = True hs_client_init_exception.get_oauth_hs(mock_request) mock_logger.debug.assert_any_call('Found oauth backend: hydroshare') mock_refresh_user_token.assert_called_once_with(mock_social_auth_obj) mock_hs_r.HydroShareAuthOAuth2.assert_called_once_with('', '', token=mock_social_auth_obj.extra_data) mock_hs_r.HydroShare.assert_called_once_with(auth=mock_hs_r.HydroShareAuthOAuth2(), hostname=mock_backend_instance.auth_server_hostname) mock_logger.debug.assert_called_with('hs object initialized: {0} @ {1}'. format(mock_social_auth_obj.extra_data['id'], mock_backend_instance.auth_server_hostname))
def save_file(request, res_id, file_name, src, save_type): script = request.POST.get('script') file_path = utilities.get_workspace() + '/id' root_dir = file_path + '/' + res_id data_dir = root_dir + '/' + res_id + '/data/contents/' + file_name print data_dir try: if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) if save_type == 'save': # os.remove(data_dir) with open(data_dir, 'wb') as f: f.write(script) hs.deleteResourceFile(res_id, file_name) # raw_input('PAUSED') hs.addResourceFile(res_id, data_dir) else: with open(data_dir, 'wb') as f: f.write(script) hs.addResourceFile(res_id, data_dir) # raw_input('PAUSED') shutil.rmtree(root_dir) file = {"File Uploaded": file_name} except: file = {"File not saved": file_name} return JsonResponse(file)
def get_o_auth_hs(request): """ Gets HydroShare Open Authorization. Arguments: [request] Returns: [hs] Referenced By: [controllers_ajax.chart_data, controllers_ajax.create_layer] References: [] Libraries: [HydroShareAuthOAuth2, HydroShare] """ if use_hs_client_helper: hs = get_oauth_hs(request) else: hs_instance_name = "www" client_id = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_KEY", None) client_secret = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_SECRET", None) # this line will throw out from django.core.exceptions.ObjectDoesNotExist\ # if current user is not signed in via HydroShare OAuth token = request.user.social_auth.get(provider='hydroshare').extra_data['token_dict'] hs_hostname = "{0}.hydroshare.org".format(hs_instance_name) auth = HydroShareAuthOAuth2(client_id, client_secret, token=token) hs = HydroShare(auth=auth, hostname=hs_hostname) return hs
def get_epanet_model_metadata(request): return_obj = { 'success': False, 'message': None, 'results': "", } if request.is_ajax() and request.method == 'GET': if not request.GET.get('model_id'): return_obj['message'] = message_template_param_unfilled.format( param='model_id') else: model_id = request.GET['model_id'] try: hs = get_oauth_hs(request) except: hs = HydroShare() metadata_json = hs.getScienceMetadata(model_id) return_obj['results'] = metadata_json return_obj['success'] = True else: return_obj['message'] = message_template_wrong_req_method.format( method="GET") return JsonResponse(return_obj)
def download_epanet_model(request): return_obj = {'success': False, 'message': None, 'results': "", 'name': ""} if request.is_ajax() and request.method == 'GET': if not request.GET.get('model_id'): return_obj['message'] = message_template_param_unfilled.format( param='model_id') else: model_id = request.GET['model_id'] try: hs = get_oauth_hs(request) except: hs = HydroShare() for model_file in hs.getResourceFileList(model_id): model_url = model_file['url'] model_name = model_url[model_url.find('contents/') + 9:] model = "" for line in hs.getResourceFile(model_id, model_name): model += line.decode("utf-8") return_obj['name'] = model_name return_obj['results'] = model return_obj['success'] = True else: return_obj['message'] = message_template_wrong_req_method.format( method="POST") return JsonResponse(return_obj)
def upload_epanet_model(request): return_obj = { 'success': False, 'message': None, 'results': "", } if request.is_ajax() and request.method == 'POST': try: hs = get_oauth_hs(request) except: hs = HydroShare() model_title = request.POST['model_title'] resource_filename = model_title + ".inp" abstract = request.POST[ 'model_description'] + '\n{%EPANET Model Repository%}' title = model_title user_keywords = ["EPANET_2.0"] for keyword in request.POST['model_keywords'].split(","): user_keywords.append(keyword) keywords = (tuple(user_keywords)) rtype = 'ModelInstanceResource' extra_metadata = '{"modelProgram": "EPANET_2.0"}' fd, path = tempfile.mkstemp() with os.fdopen(fd, 'w') as tmp: tmp.write(request.POST['model_file']) fpath = path metadata = '[{"creator":{"name":"' + hs.getUserInfo( )['first_name'] + ' ' + hs.getUserInfo()['last_name'] + '"}}]' resource_id = hs.createResource(rtype, title, resource_file=fpath, resource_filename=resource_filename, keywords=keywords, abstract=abstract, metadata=metadata, extra_metadata=extra_metadata) hs.setAccessRules(resource_id, public=True) return_obj['results'] = resource_id return_obj['success'] = True else: return_obj['message'] = message_template_wrong_req_method.format( method="GET") return JsonResponse(return_obj)
def get_epanet_model_list(request): return_obj = { 'success': False, 'message': None, 'model_list': None, } if request.is_ajax() and request.method == 'GET': try: hs = get_oauth_hs(request) except: return_obj[ 'message'] = 'You must be logged in through HydroShare to view resources.' return JsonResponse(return_obj) model_list = [] try: for model in hs.resources(resourceType="ModelInstanceResource", subject="epanet"): science_metadata_json = hs.getScienceMetadata( model['resource_id']) subjects = [] if not science_metadata_json['subjects'] is None: for subject in science_metadata_json['subjects']: if subject['value'] == 'EPANET_2.0': continue subjects.append(subject['value']) model_list.append({ 'title': model['resource_title'], 'id': model['resource_id'], 'owner': model['creator'], 'public': model['public'], 'shareable': model['shareable'], 'discoverable': model['discoverable'], 'subjects': subjects }) return_obj['model_list'] = model_list return_obj['success'] = True except: return_obj['message'] = 'The HydroShare server appears to be down.' else: return_obj['error'] = message_template_wrong_req_method.format( method="GET") return JsonResponse(return_obj)
def proj_search(request): hs = get_oauth_hs(request) get_data = request.GET print(get_data) print("2" * 50) # resources = hs.resources(full_text_search=get_data['search-keyword'][0]) temp = "subject_exact:" + get_data['search-keyword'][0] print(temp) resources = hs.resources(selected_facets=temp) print("3" * 50) for resource in resources: print(resource) print(resources) context = {} return render(request, 'hydrometa/proj_search.html', context)
def delete_file(request, res_id, file_name, src): try: if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) file_path = utilities.get_workspace() + '/id' root_dir = file_path + '/' + res_id data_dir = root_dir + '/' + res_id + '/data/contents/' + file_name hs.deleteResourceFile(res_id, file_name) shutil.rmtree(root_dir) file = {"File Deleted": file_name} except: file = {'File not Deleted':file_name} return JsonResponse(file)
def get_epanet_model(request): return_obj = { 'success': False, 'message': None, 'results': "", 'metadata': "" } if request.is_ajax() and request.method == 'GET': if not request.GET.get('model_id'): return_obj['message'] = message_template_param_unfilled.format( param='model_id') else: model_id = request.GET['model_id'] try: hs = get_oauth_hs(request) except: return_obj['message'] = 'You must be logged in through HydroShare to view resources.' return JsonResponse(return_obj) metadata_json = hs.getScienceMetadata(model_id) return_obj['metadata'] = metadata_json for model_file in hs.getResourceFileList(model_id): model_url = model_file['url'] model_name = model_url[model_url.find('contents/') + 9:] model = "" for line in hs.getResourceFile(model_id, model_name): model += line.decode('utf-8') return_obj['results'] = model return_obj['success'] = True else: return_obj['message'] = message_template_wrong_req_method.format( method="POST") return JsonResponse(return_obj)
def hydroshare(request): if request.method == 'POST': get_data = request.POST r_title = str(get_data['resource-title']) r_type = str(get_data['resource-type']) r_fileRoute = str(get_data['resource-fileroute']) r_abstract = str(get_data['resource-abstract']) r_keywords_raw = str(get_data['resource-keywords']) r_keywords = r_keywords_raw.split(',') hs = get_oauth_hs(request) print(r_title, "4444444") # filepath = '/Users/yueshen/data.txt' resource_id = hs.createResource(r_type, r_title, resource_file=r_fileRoute, keywords=r_keywords, abstract=r_abstract) print(resource_id) return redirect("https://www.hydroshare.org/resource/{resource_id}".format( resource_id=resource_id))
def chart_data(request, res_id): data_for_chart = {} error = '' data1 = None # parse xml data from 'data' from data_for_js and prepare for the table if res_id == 'None': data = utilities.parse_JSON() print(type(data)) print("ddddddddddddddddddddddddddddddddd") print(data) try: data1 = data['timeSeriesLayerResource'] data1 = json.loads(data1) except: data1 = '' print(data1) if data1 == '': try: data1 = data['timeSeriesLayerResource'] except: data1 = '' # data_n = urllib.unquote(data1).decode(encoding ="UTF-8") # print (data_n) if data1 == '': error = "No data in file" else: error = '' else: temp_dir = utilities.get_workspace() root_dir = temp_dir + '/id/' + res_id try: shutil.rmtree(root_dir) except: nothing = None if not os.path.exists(temp_dir + "/id"): os.makedirs(temp_dir + "/id") else: if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) file_path = temp_dir + '/id' hs.getResource(res_id, destination=file_path, unzip=True) root_dir = file_path + '/' + res_id data_dir = root_dir + '/' + res_id + '/data/contents/' for subdir, dirs, files in os.walk(data_dir): for file in files: if 'wml_1_' in file: data_file = data_dir + file with open(data_file, 'r') as f: # print (f.read()) file_data = f.read() f.close() file_temp_name = temp_dir + '/id/' + res_id + '.xml' file_temp = open(file_temp_name, 'wb') file_temp.write(file_data) file_temp.close() if '.json.refts' in file: data_file = data_dir + file with open(data_file, 'r') as f: file_data = f.read() print(file_data) data = file_data.encode(encoding='UTF-8') print(data) data1 = json.loads(data) data1 = data1['timeSeriesLayerResource'] # data_for_chart = {"data": '{"fileVersion":1,"title":"HydroClient-2017-01-09T17:46:47.810Z","abstract":"Retrieved timeseries...","symbol":"http://data.cuahsi.org/content/images/cuahsi_logo_small.png","keyWords":["Time Series","CUAHSI"],"REFTS":[{"refType":"WOF","serviceType":"SOAP","url":"http://hydro1.sci.gsfc.nasa.gov/daac-bin/his/1.0/GLDAS_NOAH_001.cgi?WSDL","site":"X282-Y404 of Global Land Data Assimilation System (GLDAS) NASA","siteCode":"GLDAS_NOAH:X282-Y404","variable":"Surface runoff","variableCode":"GLDAS:GLDAS_NOAH025_3H.001:Qs","networkName":"GLDAS_NOAH","beginDate":"2016-01-09T00:00:00","endDate":"2016-09-30T21:00:00+00:00","returnType":"WaterML 1.0","location":{"latitude":41.125,"longitude":-109.375}}]}','error':error} data_for_chart = {"data": data1, 'error': error} return JsonResponse(data_for_chart)
def home(request): """ Controller for the app home page. :param request: the request object sent by the browser """ point_size_options = range(1, 31) stroke_width_options = range(1, 16) point_shape_options = [ 'circle', 'square', 'triangle', 'star', 'cross', 'X' ] font_size_options = range(8, 37, 2) num_gradient_colors_options = range(2, 9) context = { 'point_size_options': point_size_options, 'stroke_width_options': stroke_width_options, 'point_shape_options': point_shape_options, 'font_size_options': font_size_options, 'num_gradient_colors_options': num_gradient_colors_options } if 'add-to-project' in request.path_info: existing_projects = [] resources_to_add = [] if request.GET.get('res_ids'): res_ids = request.GET['res_ids'].split(',') hs = get_oauth_hs(request) for res_id in res_ids: try: md = hs.getSystemMetadata(res_id) resources_to_add.append({ 'title': md['resource_title'], 'id': md['resource_id'], 'type': md['resource_type'] }) except Exception as e: print(str(e)) continue userInfo = hs.getUserInfo() username = userInfo['username'] for res in hs.getResourceList(creator=username, types=['GenericResource']): res_id = res['resource_id'] try: for res_file in hs.getResourceFileList(res_id): if res_file['content_type'] == 'application/json': existing_projects.append({ 'title': res['resource_title'], 'id': res_id }) except Exception as e: print(str(e)) continue context['existing_projects'] = existing_projects context['resources_to_add'] = resources_to_add return render(request, 'hydroshare_gis/home.html', context)
def create_layer(request,fun_type,res_id): resource_id=None data_stor=[] int_resource=[] counter=0 title = str(request.POST.get('resTitle'))# causing errors because not strints? abstract = str(request.POST.get('resAbstract')) keywords = str(request.POST.get('resKeywords')) res_access = str(request.POST.get('resAccess')) keywords = keywords.split(',') str_resource = request.POST.get('checked_ids') print "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa" print str_resource str_resource = trim(str_resource) for res in str_resource: int_resource.append(int(res)) print int_resource metadata = [] if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) temp_dir = utilities.get_workspace() file_name = title.replace(" ", "") file_path = temp_dir + '/id/timeseriesLayerResource.json.refts' fpath = temp_dir + '/id/'+file_name+'.json.refts' print fpath with open(file_path, 'r') as outfile: file_data = outfile.read() data = file_data.encode(encoding ='UTF-8') data = json.loads(data) print data data = data['timeSeriesLayerResource'] try: data_symbol = data['symbol'] data_file =data['fileVersion'] except: data = json.loads(data) data_symbol = data['symbol'] data_file =data['fileVersion'] for i in data['REFTS']: if counter in int_resource: data_stor.append(i) counter = counter+1 data_dic = {"REFTS":data_stor,"fileVersion": data_file, "title": title, "symbol":data_symbol,"abstract": abstract,'keyWords':keywords} data.update(data_dic) final_dic = {"timeSeriesLayerResource":data} with open(fpath, 'w') as outfile: json.dump(final_dic, outfile) r_type = 'GenericResource' r_title = title r_keywords = (keywords) r_abstract = abstract print res_id if fun_type =='create': try: print "creating resource" resource_id = hs.createResource(r_type, r_title, resource_file=fpath, keywords=r_keywords, abstract=r_abstract, metadata=metadata) except: resource_id ="error" elif fun_type =='update': try: print "Updating resource" try: resource_id = hs.deleteResourceFile(res_id, fpath+'.json.refts') except: print 'file doesnt exist' resource_id = hs.addResourceFile(res_id, fpath) except: resource_id ="error" # if res_access == 'public': # hs.setAccessRules(resource_id, public=True) # else: # hs.setAccessRules(resource_id, public=False) #upload to hydroshare stuff return JsonResponse({'Request':resource_id})
def create_layer(request, fun_type, res_id): resource_id = None data_stor = [] int_resource = [] counter = 0 title = str( request.POST.get('resTitle')) # causing errors because not strints? abstract = str(request.POST.get('resAbstract')) keywords = str(request.POST.get('resKeywords')) res_access = str(request.POST.get('resAccess')) keywords = keywords.split(',') str_resource = request.POST.get('checked_ids') print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa") print(str_resource) str_resource = trim(str_resource) for res in str_resource: int_resource.append(int(res)) print(int_resource) metadata = [] if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) temp_dir = utilities.get_workspace() file_name = title.replace(" ", "") file_path = temp_dir + '/id/timeseriesLayerResource.json.refts' fpath = temp_dir + '/id/' + file_name + '.json.refts' print(fpath) with open(file_path, 'r') as outfile: file_data = outfile.read() data = file_data.encode(encoding='UTF-8') data = json.loads(data) print(data) data = data['timeSeriesLayerResource'] try: data_symbol = data['symbol'] data_file = data['fileVersion'] except: data = json.loads(data) data_symbol = data['symbol'] data_file = data['fileVersion'] for i in data['REFTS']: if counter in int_resource: data_stor.append(i) counter = counter + 1 data_dic = { "REFTS": data_stor, "fileVersion": data_file, "title": title, "symbol": data_symbol, "abstract": abstract, 'keyWords': keywords } data.update(data_dic) final_dic = {"timeSeriesLayerResource": data} with open(fpath, 'w') as outfile: json.dump(final_dic, outfile) r_type = 'GenericResource' r_title = title r_keywords = (keywords) r_abstract = abstract print(res_id) if fun_type == 'create': try: print("creating resource") resource_id = hs.createResource(r_type, r_title, resource_file=fpath, keywords=r_keywords, abstract=r_abstract, metadata=metadata) except: resource_id = "error" elif fun_type == 'update': try: print("Updating resource") try: resource_id = hs.deleteResourceFile(res_id, fpath + '.json.refts') except: print('file doesnt exist') resource_id = hs.addResourceFile(res_id, fpath) except: resource_id = "error" # if res_access == 'public': # hs.setAccessRules(resource_id, public=True) # else: # hs.setAccessRules(resource_id, public=False) # upload to hydroshare stuff return JsonResponse({'Request': resource_id})
def chart_data(request,res_id): data_for_chart={} error='' data1=None #parse xml data from 'data' from data_for_js and prepare for the table if res_id =='None': data = utilities.parse_JSON() print type(data) print "ddddddddddddddddddddddddddddddddd" print data try: data1 = data['timeSeriesLayerResource'] data1 = json.loads(data1) except: data1='' print data1 if data1=='': try: data1 = data['timeSeriesLayerResource'] except: data1='' # data_n = urllib.unquote(data1).decode(encoding ="UTF-8") # print data_n if data1 =='': error = "No data in file" else: error='' else: temp_dir = utilities.get_workspace() root_dir = temp_dir + '/id/' + res_id try: shutil.rmtree(root_dir) except: nothing =None if not os.path.exists(temp_dir+"/id"): os.makedirs(temp_dir+"/id") else: if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) file_path = temp_dir + '/id' hs.getResource(res_id, destination=file_path, unzip=True) root_dir = file_path + '/' + res_id data_dir = root_dir + '/' + res_id + '/data/contents/' for subdir, dirs, files in os.walk(data_dir): for file in files: if 'wml_1_' in file: data_file = data_dir + file with open(data_file, 'r') as f: # print f.read() file_data = f.read() f.close() file_temp_name = temp_dir + '/id/' + res_id + '.xml' file_temp = open(file_temp_name, 'wb') file_temp.write(file_data) file_temp.close() if '.json.refts' in file: data_file = data_dir +file with open(data_file, 'r') as f: file_data = f.read() print file_data data = file_data.encode(encoding ='UTF-8') print data data1 = json.loads(data) data1 = data1['timeSeriesLayerResource'] # data_for_chart = {"data": '{"fileVersion":1,"title":"HydroClient-2017-01-09T17:46:47.810Z","abstract":"Retrieved timeseries...","symbol":"http://data.cuahsi.org/content/images/cuahsi_logo_small.png","keyWords":["Time Series","CUAHSI"],"REFTS":[{"refType":"WOF","serviceType":"SOAP","url":"http://hydro1.sci.gsfc.nasa.gov/daac-bin/his/1.0/GLDAS_NOAH_001.cgi?WSDL","site":"X282-Y404 of Global Land Data Assimilation System (GLDAS) NASA","siteCode":"GLDAS_NOAH:X282-Y404","variable":"Surface runoff","variableCode":"GLDAS:GLDAS_NOAH025_3H.001:Qs","networkName":"GLDAS_NOAH","beginDate":"2016-01-09T00:00:00","endDate":"2016-09-30T21:00:00+00:00","returnType":"WaterML 1.0","location":{"latitude":41.125,"longitude":-109.375}}]}','error':error} data_for_chart = {"data": data1,'error':error} return JsonResponse(data_for_chart)
def upload_to_hydroshare(request): temp_dir = None try: return_json = {} if request.method == 'POST': get_data = request.POST basin_kml_filetext = str(get_data['basin_kml_filetext']) streams_kml_filetext = str(get_data['streams_kml_filetext']) upstream_kml_filetext = str(get_data['upstream_kml_filetext']) downstream_kml_filetext = str(get_data['downstream_kml_filetext']) r_title = str(get_data['r_title']) r_type = str(get_data['r_type']) r_abstract = str(get_data['r_abstract']) r_keywords_raw = str(get_data['r_keywords']) r_keywords = r_keywords_raw.split(',') # startup a Hydroshare instance with user's credentials # auth = HydroShareAuthBasic(username=hs_username, password=hs_password) # hs = HydroShare(auth=auth, hostname="www.hydroshare.org", use_https=True) #hs = getOAuthHS(request) hs = get_oauth_hs(request) #download the kml file to a temp directory temp_dir = tempfile.mkdtemp() basin_kml_file_path = os.path.join(temp_dir, "basin.kml") streams_kml_file_path = os.path.join(temp_dir, "streams.kml") upstream_kml_file_path = os.path.join(temp_dir, "upstream.kml") downstream_kml_file_path = os.path.join(temp_dir, "downstream.kml") with open(basin_kml_file_path, 'w') as fd: fd.write(basin_kml_filetext) with open(streams_kml_file_path, 'w') as fd: fd.write(streams_kml_filetext) with open(upstream_kml_file_path, 'w') as fd: fd.write(upstream_kml_filetext) with open(downstream_kml_file_path, 'w') as fd: fd.write(downstream_kml_filetext) #upload the temp file to HydroShare if os.path.exists(basin_kml_file_path): basin_resource_id = hs.createResource( r_type, r_title, resource_file=basin_kml_file_path, keywords=r_keywords, abstract=r_abstract) resource_id = hs.addResourceFile(basin_resource_id, streams_kml_file_path) resource_id = hs.addResourceFile(basin_resource_id, upstream_kml_file_path) resource_id = hs.addResourceFile(basin_resource_id, downstream_kml_file_path) return_json['success'] = 'File uploaded successfully!' return_json['newResource'] = resource_id return_json['hs_domain'] = hs.hostname else: raise except ObjectDoesNotExist as e: logger.exception(e.message) return_json[ 'error'] = 'Login timed out! Please re-sign in with your HydroShare account.' except TokenExpiredError as e: logger.exception(e.message) return_json[ 'error'] = 'Login timed out! Please re-sign in with your HydroShare account.' except Exception, err: logger.exception(err.message) if "401 Unauthorized" in str(err): return_json['error'] = 'Username or password invalid.' elif "400 Bad Request" in str(err): return_json[ 'error'] = 'File uploaded successfully despite 400 Bad Request Error.' else: traceback.print_exc() return_json[ 'error'] = 'HydroShare rejected the upload for some reason.'
def home(request): """ Controller for the app home page. :param request: the request object sent by the browser """ point_size_options = range(1, 31) stroke_width_options = range(1,16) point_shape_options = ['circle', 'square', 'triangle', 'star', 'cross', 'X'] font_size_options = range(8, 37, 2) num_gradient_colors_options = range(2, 9) context = { 'point_size_options': point_size_options, 'stroke_width_options': stroke_width_options, 'point_shape_options': point_shape_options, 'font_size_options': font_size_options, 'num_gradient_colors_options': num_gradient_colors_options } if 'add-to-project' in request.path_info: existing_projects = [] resources_to_add = [] if request.GET.get('res_ids'): res_ids = request.GET['res_ids'].split(',') hs = get_oauth_hs(request) for res_id in res_ids: try: md = hs.getSystemMetadata(res_id) resources_to_add.append({ 'title': md['resource_title'], 'id': md['resource_id'], 'type': md['resource_type'] }) except Exception as e: print str(e) continue userInfo = hs.getUserInfo() username = userInfo['username'] for res in hs.getResourceList(creator=username, types=['GenericResource']): res_id = res['resource_id'] try: for res_file in hs.getResourceFileList(res_id): if res_file['content_type'] == 'application/json': existing_projects.append({ 'title': res['resource_title'], 'id': res_id }) except Exception as e: print str(e) continue context['existing_projects'] = existing_projects context['resources_to_add'] = resources_to_add return render(request, 'hydroshare_gis/home.html', context)
def home(request): # import sys # sys.path.append("/home/drew/pycharm-debug") # import pydevd # pydevd.settrace('172.17.42.1', port=21000, suspend=False) popup_title = popup_title_WELCOME popup_content = popup_content_NOT_LAUNCHED_FROM_HYDROSHARE success_flag = "true" resource_title = None if request.GET: res_id = request.GET.get("res_id", None) src = request.GET.get('src', None) usr = request.GET.get('usr', None) if res_id is None or src is None or src != "hs" or usr is None: success_flag = "welcome" elif usr.lower() == "anonymous": popup_title = popup_title_ERROR popup_content = popup_content_ANONYMOUS_USER success_flag = "false" else: request.session['res_id'] = res_id request.session['src'] = src request.session['usr'] = usr try: # res_id = "b7822782896143ca8712395f6814c44b" # res_id = "877bf9ed9e66468cadddb229838a9ced" # res_id = "e660640a7b084794aa2d70dc77cfa67b" # private res # res_id = "a4a4bca8369e4c1e88a1b35b9487e731" # request.session['res_id'] = res_id #hs = getOAuthHS(request) hs = get_oauth_hs(request) global hs_hostname hs_hostname = hs.hostname resource_md = hs.getSystemMetadata(res_id) resource_type = resource_md.get("resource_type", "") resource_title = resource_md.get("resource_title", "") if resource_type.lower() != "rasterresource": popup_title = popup_title_ERROR popup_content = popup_content_NOT_RASTER_RESOURCE success_flag = "false" #raise Http404("Not RasterResource") except ObjectDoesNotExist as e: logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_NOT_OAUTH_LOGIN success_flag = "false" except TokenExpiredError as e: logger.exception(e) popup_title = popup_title_WARNING popup_content = popup_content_TOKEN_EXPIRED success_flag = "false" # raise Http404("Token Expired") except HydroShareNotAuthorized as e: logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_NO_PERMISSION success_flag = "false" # raise Http404("Your have no permission on this resource") except HydroShareNotFound as e: logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_NOT_FOUND success_flag = "false" except Exception as e: logger.error("unknown error") logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_UNKNOWN_ERROR success_flag = "false" # raise else: success_flag = "welcome" context = {"popup_title": popup_title, "popup_content": popup_content, "success_flag": success_flag, 'resource_title': resource_title, } return render(request, 'raster_viewer/home.html', context)
def draw_raster(request): res_id = request.session.get("res_id", None) temp_res_extracted_dir = None temp_dir = None map_dict = {} map_dict["success"] = False band_stat_info_array = [] try: if res_id is not None: map_dict = getMapParas(geosvr_url_base=geosvr_url_base, wsName=geoserver_workspace_name, store_id=res_id, \ layerName=res_id, un=geosvr_user, pw=geosvr_pw) if map_dict["success"]: # find cached raster session = SessionMaker() # res_1 = RasterStatistics(res_id="res_id_1", # min_val=1, # max_val=10) # session.add(res_1) # session.commit() band_stat_info_array = [] raster_stat_array = session.query(RasterStatistics).all() for r_stat in raster_stat_array: if r_stat.res_id == res_id: band_min_val = r_stat.min_val band_max_val = r_stat.max_val band_min_2nd_val = r_stat.min_2nd_val band_max_2nd_val = r_stat.max_2nd_val band_mean_val = r_stat.mean_val band_std_val = r_stat.std_val band_id = r_stat.band_id band_name = r_stat.band_name band_no_data_val = r_stat.no_data_val band_stat_info={} band_stat_info["min_val"] = band_min_val band_stat_info["max_val"] = band_max_val band_stat_info["min_2nd_val"] = band_min_2nd_val band_stat_info["max_2nd_val"] = band_max_2nd_val band_stat_info["mean_val"] = band_mean_val band_stat_info["std_val"] = band_std_val band_stat_info["band_id"] = band_id band_stat_info["band_name"] = band_name band_stat_info["no_data_val"] = band_no_data_val band_stat_info_array.append(band_stat_info) logger.debug("---------------------Load band_stat_info from DB-------------------------------") logger.debug(band_stat_info_array) else: # no cached raster or raster has no projection session = SessionMaker() raster_stat_array = session.query(RasterStatistics).all() for r_stat in raster_stat_array: if r_stat.res_id == res_id: session.delete(r_stat) logger.debug("---------------------Delete leftover band_stat_info from DB-------------------------------") session.commit() #hs = getOAuthHS(request) hs = get_oauth_hs(request) global hs_hostname hs_hostname = hs.hostname logger.debug ("Begin download res: {0}".format(res_id)) hs.getResource(res_id, destination=extract_base_path, unzip=True) logger.debug ("End download res") temp_res_extracted_dir = extract_base_path + '/' + res_id logger.debug (temp_res_extracted_dir) contents_folder = extract_base_path + '/' + res_id + '/' + res_id +'/data/contents/' logger.debug (contents_folder) file_list = os.listdir(contents_folder) tif_fn = file_list[0] # tif full name for fn in file_list: if fn.endswith(".tif"): tif_fn = fn break tif_fp = contents_folder + tif_fn # tif full path band_stat_info_array = extract_geotiff_stat_info(tif_fp) logger.debug(band_stat_info_array) if len(band_stat_info_array) > 0: session = SessionMaker() for band_info in band_stat_info_array: band_info_db = RasterStatistics(res_id=res_id, min_val=band_info["min_val"], max_val=band_info["max_val"], mean_val=band_info["mean_val"], std_val=band_info["std_val"], min_2nd_val=band_info["min_2nd_val"], max_2nd_val=band_info["max_2nd_val"], band_id=band_info["band_id"], band_name=str(band_info["band_id"]), hs_branch=hs_hostname, no_data_val=band_info["no_data_val"]) session.add(band_info_db) session.commit() logger.debug("--------------- Save to DB : band_stat_info ------------") tif_hdl = open(tif_fp, 'rb') tif_obj = tif_hdl.read() temp_dir = tempfile.mkdtemp() rslt_dic = zipSaveAs(res_id + ".tif", tif_obj, temp_dir, "zip_file.zip") zip_file_full_path = rslt_dic['zip_file_full_path'] zip_crc = rslt_dic['crc'] rslt = addZippedTif2Geoserver(geosvr_url_base=geosvr_url_base, uname=geosvr_user, upwd=geosvr_pw, ws_name=geoserver_workspace_name, \ store_name=res_id, zippedTif_full_path=zip_file_full_path, res_url="appsdev.hydroshare.org/apps/raster-viewer") if(rslt): map_dict = getMapParas(geosvr_url_base=geosvr_url_base, wsName=geoserver_workspace_name, store_id=res_id, \ layerName=res_id, un=geosvr_user, pw=geosvr_pw) map_dict['geosvr_url_base'] = geosvr_url_base map_dict['ws_name'] = geoserver_workspace_name map_dict['store_name'] = res_id map_dict['layer_name'] = res_id map_dict['band_stat_info_array'] = band_stat_info_array if map_dict["success"] == False: map_dict['popup_title'] = popup_title_ERROR map_dict['popup_content'] = popup_content_INVALID_GEOTIFF else: map_dict["success"] = False map_dict['popup_title'] = popup_title_ERROR map_dict['popup_content'] = popup_content_NO_RESOURCEID_IN_SESSION except ObjectDoesNotExist as e: logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_NOT_OAUTH_LOGIN map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content except TokenExpiredError as e: logger.exception(e) popup_title = popup_title_WARNING popup_content = popup_content_TOKEN_EXPIRED map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content # raise Http404("Token Expired") except HydroShareNotAuthorized as e: logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_NO_PERMISSION map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content # raise Http404("Your have no permission on this resource") except HydroShareNotFound as e: logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_NOT_FOUND map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content except Exception as e: logger.error("unknown error") logger.exception(e) popup_title = popup_title_ERROR popup_content = popup_content_UNKNOWN_ERROR map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content # raise finally: if temp_dir is not None: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) logger.debug(temp_dir + " deleted") if temp_res_extracted_dir is not None: if os.path.exists(temp_res_extracted_dir): shutil.rmtree(temp_res_extracted_dir) logger.debug(temp_res_extracted_dir + " deleted") return JsonResponse(map_dict)
def chart_data(request, res_id, src): data_for_chart = {} error = False is_owner = False print "JSON Reponse" print datetime.now() print "update" # Downloading all files types that work with app from hydroshare file_path = utilities.get_workspace() + '/id' root_dir = file_path + '/' + res_id try: shutil.rmtree(root_dir) except: nothing =None try: if use_hs_client_helper: hs = get_oauth_hs(request) else: hs = getOAuthHS(request) hs.getResource(res_id, destination=file_path, unzip=True) data_dir = root_dir + '/' + res_id + '/data/contents/' # f = open(data_dir) # print f.read() for subdir, dirs, files in os.walk(data_dir): for file in files: # if '.r' in file or '.R' in file or'.py' in file or '.m' in file or '.txt' in file or '.xml' in file: data_file = data_dir + file with open(data_file, 'r') as f: # print f.read() data = f.read() # print data f.close() print data try: data= data.decode('latin-1') except: data = data data_for_chart.update({str(file): data}) # data_for_chart = {'bjo':'hello'} user = hs.getUserInfo() user1 = user['username'] # resource = hs.getResourceList(user ='******') resource = hs.getResourceList(owner = user1) for res in resource: # print res id = res["resource_id"] # print id if(res_id ==res["resource_id"]): is_owner = True except Exception as inst: data_for_chart = 'You are not authorized to access this resource' owner = False error = True print 'start' print(type(inst)) print(inst.args) try: data_for_chart = str(inst) except: data_for_chart = "There was an error loading data for resource"+res_id print "end" return JsonResponse({"data":data_for_chart,"owner":is_owner,"error":error})
def home(request): # import sys # sys.path.append("/home/drew/pycharm-debug") # import pydevd # pydevd.settrace('172.17.42.1', port=21000, suspend=False) popup_title = popup_title_WELCOME popup_content = popup_content_NOT_LAUNCHED_FROM_HYDROSHARE success_flag = "true" resource_title = None if request.GET: res_id = request.GET.get("res_id", None) src = request.GET.get('src', None) usr = request.GET.get('usr', None) if res_id is None or src is None or src != "hs" or usr is None: success_flag = "welcome" elif usr.lower() == "anonymous": popup_title = popup_title_ERROR popup_content = popup_content_ANONYMOUS_USER success_flag = "false" else: request.session['res_id'] = res_id request.session['src'] = src request.session['usr'] = usr try: # res_id = "b7822782896143ca8712395f6814c44b" # res_id = "877bf9ed9e66468cadddb229838a9ced" # res_id = "e660640a7b084794aa2d70dc77cfa67b" # private res # res_id = "a4a4bca8369e4c1e88a1b35b9487e731" # request.session['res_id'] = res_id hs = get_oauth_hs(request) global hs_hostname hs_hostname = hs.hostname resource_md = hs.getSystemMetadata(res_id) resource_type = resource_md.get("resource_type", "") resource_title = resource_md.get("resource_title", "") if resource_type.lower() != target_res_type: popup_title = popup_title_ERROR popup_content = popup_content_NOT_GEOG_FEATURE_RESOURCE success_flag = "false" logger.debug(resource_type.lower()) except ObjectDoesNotExist as e: logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_NOT_OAUTH_LOGIN success_flag = "false" except TokenExpiredError as e: logger.exception(e.message) popup_title = popup_title_WARNING popup_content = popup_content_TOKEN_EXPIRED success_flag = "false" except HydroShareNotAuthorized as e: logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_NO_PERMISSION success_flag = "false" except HydroShareNotFound as e: logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_NOT_FOUND success_flag = "false" except Exception as e: logger.error("unknown error") logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_UNKNOWN_ERROR success_flag = "false" # raise else: success_flag = "welcome" context = { "popup_title": popup_title, "popup_content": popup_content, "success_flag": success_flag, 'resource_title': resource_title, } return render(request, 'hydroshare_shapefile_viewer/home.html', context)
def upload_to_hydroshare(request): print("running upload_to_hydroshare!") temp_dir = None try: return_json = {} if request.method == 'GET': get_data = request.GET base_url = request.build_absolute_uri() waterml_url = base_url.replace('upload-to-hydroshare', 'waterml') print(waterml_url) r_title = request.GET['title'] r_abstract = request.GET['abstract'] r_keywords_raw = request.GET['keywords'] r_type = 'CompositeResource' r_keywords = r_keywords_raw.split(',') #hs = getOAuthHS(request) hs = get_oauth_hs(request) # download the kml file to a temp directory temp_dir = tempfile.mkdtemp() waterml_file_path = os.path.join(temp_dir, "snow.wml") print(waterml_file_path) with open(waterml_file_path, 'w') as f: resp = requests.get(waterml_url, verify=False) f.write(resp.content) # upload the temp file to HydroShare if os.path.exists(waterml_file_path): resource_id = hs.createResource( r_type, r_title, resource_file=waterml_file_path, keywords=r_keywords, abstract=r_abstract) return_json['success'] = 'File uploaded successfully!' return_json['newResource'] = resource_id else: raise except ObjectDoesNotExist as e: print((str(e))) return_json[ 'error'] = 'Object doesn"t exist: Login timed out! Please re-sign in with your HydroShare account.' except TokenExpiredError as e: print((str(e))) return_json[ 'error'] = 'Login timed out! Please re-sign in with your HydroShare account.' except Exception as err: if "401 Unauthorized" in str(err): return_json['error'] = 'Username or password invalid.' elif "400 Bad Request" in str(err): return_json[ 'error'] = 'File uploaded successfully despite 400 Bad Request Error.' else: traceback.print_exc() return_json[ 'error'] = 'HydroShare rejected the upload for some reason.' finally: if temp_dir != None: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) print(return_json) return JsonResponse(return_json)
def draw_geog_feature(request): res_id = request.session.get("res_id", None) temp_res_extracted_dir = None temp_dir = None map_dict = {} map_dict["success"] = False try: if res_id is not None: map_dict = getMapParas(geosvr_url_base=geosvr_url_base, wsName=geoserver_workspace_name, store_id=res_id, \ layerName=res_id, un=geosvr_user, pw=geosvr_pw) if map_dict["success"]: # find cached raster logger.debug("find cached layer on geoserver") else: # no cached raster or raster has no projection hs = get_oauth_hs(request) global hs_hostname hs_hostname = hs.hostname logger.debug("Begin download res: {0}".format(res_id)) hs.getResource(res_id, destination=extract_base_path, unzip=True) logger.debug("End download res") temp_res_extracted_dir = extract_base_path + '/' + res_id logger.debug(temp_res_extracted_dir) contents_folder = extract_base_path + '/' + res_id + '/' + res_id + '/data/contents/' logger.debug(contents_folder) file_list = os.listdir(contents_folder) for fn in file_list: logger.debug(fn) temp_dir = tempfile.mkdtemp() zip_file_full_path = temp_dir + "/" + "zip_shapefile.zip" with zipfile.ZipFile(zip_file_full_path, 'a') as myzip: for fn in file_list: shapefile_fp = contents_folder + fn # tif full path new_file_name = res_id + os.path.splitext(fn)[1] myzip.write(shapefile_fp, arcname=new_file_name) rslt = addZippedShapefile2Geoserver(geosvr_url_base=geosvr_url_base, uname=geosvr_user, upwd=geosvr_pw, ws_name=geoserver_workspace_name, \ store_name=res_id, zippedTif_full_path=zip_file_full_path, res_url='appsdev.hydroshare.org/apps/hydroshare-shapefile-viewer') if (rslt): map_dict = getMapParas(geosvr_url_base=geosvr_url_base, wsName=geoserver_workspace_name, store_id=res_id, \ layerName=res_id, un=geosvr_user, pw=geosvr_pw) map_dict['geosvr_url_base'] = geosvr_url_base map_dict['ws_name'] = geoserver_workspace_name map_dict['store_name'] = res_id map_dict['layer_name'] = res_id if map_dict["success"] == False: map_dict['popup_title'] = popup_title_ERROR map_dict['popup_content'] = popup_content_INVALID_GEOTIFF else: map_dict["success"] = False map_dict['popup_title'] = popup_title_ERROR map_dict['popup_content'] = popup_content_NO_RESOURCEID_IN_SESSION except ObjectDoesNotExist as e: logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_NOT_OAUTH_LOGIN map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content except TokenExpiredError as e: logger.exception(e.message) popup_title = popup_title_WARNING popup_content = popup_content_TOKEN_EXPIRED map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content except HydroShareNotAuthorized as e: logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_NO_PERMISSION map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content except HydroShareNotFound as e: logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_NOT_FOUND map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content except Exception as e: logger.error("unknown error") logger.exception(e.message) popup_title = popup_title_ERROR popup_content = popup_content_UNKNOWN_ERROR map_dict["success"] = False map_dict['popup_title'] = popup_title map_dict['popup_content'] = popup_content # raise finally: if temp_dir is not None: if os.path.exists(temp_dir): shutil.rmtree(temp_dir) logger.debug(temp_dir + " deleted") if temp_res_extracted_dir is not None: if os.path.exists(temp_res_extracted_dir): shutil.rmtree(temp_res_extracted_dir) logger.debug(temp_res_extracted_dir + " deleted") return JsonResponse(map_dict)