def samples(): email = session.get('email', None) api_key = session.get('api_key', None) api = MetpetAPI(email, api_key).api filters = ast.literal_eval(json.dumps(request.args)) offset = request.args.get('offset', 0) filters['offset'] = offset data = api.sample.get(params=filters) next, previous, last, total_count = paginate_model('samples', data, filters) samples = data.data['objects'] for sample in samples: if 'minerals' in samples: mineral_names = [mineral['name'] for mineral in sample['minerals']] sample['mineral_list'] = (', ').join(mineral_names) first_page_filters = filters del first_page_filters['offset'] if filters: first_page_url = url_for('samples') + '?' + urlencode(first_page_filters) else: first_page_url = url_for('samples') + urlencode(first_page_filters) return render_template('samples.html', samples=samples, next_url=next, prev_url=previous, total=total_count, first_page=first_page_url, last_page=last)
def chemical_analyses(): email = session.get('email', None) api_key = session.get('api_key', None) api = MetpetAPI(email, api_key).api filters = ast.literal_eval(json.dumps(request.args)) offset = request.args.get('offset', 0) filters['offset'] = offset data = api.chemical_analysis.get(params=filters) next, previous, last, total_count = paginate_model('chemical_analyses', data, filters) chemical_analyses = data.data['objects'] first_page_filters = filters del first_page_filters['offset'] if filters: first_page_url = url_for('chemical_analyses') + '?' + urlencode(first_page_filters) else: first_page_url = url_for('chemical_analyses') + urlencode(first_page_filters) return render_template('chemical_analyses.html', chemical_analyses=chemical_analyses, next_url=next, prev_url=previous, total=total_count, first_page=first_page_url, last_page=last)
def chemical_analyses(): api = drest.API(baseurl=env('API_DRF_HOST')) next = previous = last = total_count = None filters = literal_eval(json.dumps(request.args)) filters['format'] = 'json' chemicals = api.make_request('GET', '/chemical_analyses/', params=filters).data chem_results = chemicals['results'] next, previous, last, total_count = paginate_model('chemical-analyses', chemicals, filters) for c in chem_results: c['sample'] = api.make_request('GET', '/samples/' + c['subsample']['sample'], params={ 'fields': 'number', 'format': 'json' }).data if c['analysis_date']: c['analysis_date'] = c['analysis_date'][:-10] return render_template('chemical_analyses.html', chemical_analyses=chem_results, next_url=next, prev_url=previous, total=total_count, first_page=url_for('chemical-analyses') + '?' + urlencode(filters), last_page=last)
def samples(): api = drest.API(baseurl=env('API_DRF_HOST')) next = previous = last = total_count = None filters = literal_eval(json.dumps(request.args)) filters['format'] = 'json' samples = api.make_request('GET', '/samples/', params=filters).data sample_results = samples['results'] next, previous, last, total_count = paginate_model('samples', samples, filters) for s in sample_results: pos = s['location_coords'].split(" ") s['location_coords'] = [ round(float(pos[2].replace(")", "")), 5), round(float(pos[1].replace("(", "")), 5) ] s['minerals'] = (', ').join([m['name'] for m in s['minerals']]) if s['collection_date']: s['collection_date'] = s['collection_date'][:-10] return render_template('samples.html', samples=sample_results, showmap='showmap' in filters, next_url=next, prev_url=previous, total=total_count, first_page=url_for('samples') + '?' + urlencode(filters), last_page=last)
def chemical_analyses(): #similar to samples filters = dict(request.args) for key in filters.keys(): f = "" for i in range(len(filters[key])): if filters[key][i]: f += str(filters[key][i])+',' if f: filters[key] = f[:-1] else: del filters[key] filters["format"] = "json" if "minerals_and" in filters: del filters["minerals_and"] headers = None if session.get("auth_token", None): headers = {"Authorization": "Token "+session.get("auth_token")} else: filters["public_data"] = True ##MAKES Chemicals private visible to uploaders chemicals = get(env("API_HOST")+"chemical_analyses/", params = filters,headers=headers).json() chem_results = chemicals["results"] next_url, prev_url, last_page, total = paginate_model("chemical_analyses", chemicals, filters) #collect sample ids and corresponding names samples = set() for c in chem_results: samples.add(c["subsample"]["sample"]) samples = get(env("API_HOST")+"samples/", params = {"fields": "number,id", "ids": (",").join(list(samples)), "format": "json"}, headers = headers).json()["results"] numbers = {} for s in samples: numbers[s["id"]] = s for c in chem_results: c["sample"] = numbers[c["subsample"]["sample"]] if c["analysis_date"]: c["analysis_date"] = c["analysis_date"][:-10] return render_template("chemical_analyses.html", chemical_analyses = chem_results, total = total, next_url = next_url, prev_url = prev_url, first_page = url_for("chemical_analyses")+"?"+urlencode(filters), last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None) )
def chemical_analyses(): #similar to samples filters = dict(request.args) for key in filters.keys(): f = "" for i in range(len(filters[key])): if filters[key][i]: f += str(filters[key][i])+',' if f: filters[key] = f[:-1] else: del filters[key] filters["format"] = "json" if "minerals_and" in filters: del filters["minerals_and"] headers = None if session.get("auth_token", None): headers = {"Authorization": "Token "+session.get("auth_token")} else: filters["public_data"] = True chemicals = get(env("API_HOST")+"chemical_analyses/", params = filters).json() chem_results = chemicals["results"] next_url, prev_url, last_page, total = paginate_model("chemical_analyses", chemicals, filters) #collect sample ids and corresponding names samples = set() for c in chem_results: samples.add(c["subsample"]["sample"]) samples = get(env("API_HOST")+"samples/", params = {"fields": "number,id", "ids": (",").join(list(samples)), "format": "json"}, headers = headers).json()["results"] numbers = {} for s in samples: numbers[s["id"]] = s for c in chem_results: c["sample"] = numbers[c["subsample"]["sample"]] if c["analysis_date"]: c["analysis_date"] = c["analysis_date"][:-10] return render_template("chemical_analyses.html", chemical_analyses = chem_results, total = total, next_url = next_url, prev_url = prev_url, first_page = url_for("chemical_analyses")+"?"+urlencode(filters), last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None) )
def samples(): #filters sent as parameters to API calls #only return public data samples if not logged in filters = dict(request.args) for key in filters.keys(): if key == "polygon_coords" and filters[key][0]: coords = filters[key][0][1:-1].split("],[") coords.append(coords[0]) filters[key] = ["[["+("],[").join(coords)+"]]"] filters["polygon_coord"] = [] filters[key] = (',').join([e for e in filters[key] if e and e[0]]) if not filters[key]: del filters[key] filters["format"] = "json" #get sample data and use meta data to get pagination urls samples = get(env("API_HOST")+"samples/", params = filters).json() sample_results = samples["results"] next_url, prev_url, last_page, total = paginate_model("samples", samples, filters) #split location into (rounded!) latitude and longitude #make string of minerals for ... look and clean up date for s in sample_results: pos = s["location_coords"].split(" ") s["location_coords"] = [round(float(pos[2].replace(")","")),5),round(float(pos[1].replace("(","")),5)] s["minerals"] = (", ").join([m["name"] for m in s["minerals"]]) if s["collection_date"]: s["collection_date"] = s["collection_date"][:-10] return render_template("samples.html", samples = sample_results, showmap = "showmap" in filters, extends = "render" in filters, total = total, next_url = next_url, prev_url = prev_url, first_page = url_for("samples")+"?"+urlencode(filters), last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None) )
def samples(): #filters sent as parameters to API calls #only return public data samples if not logged in filters = dict(request.args) tmp_var = 0 print("Pre-processing filters") print(filters) for key in filters.keys(): # Key is a map polygon if key == "polygon_coords" and filters[key][0]: # List of coordinate point strings; remove trailing comma, starting/trailing bracket, # and split into coordinate points coords = filters[key][0].strip(',').strip('[').strip(']').split('],[') # Tack the first coordinate on to the end of the list so that the coordinates # form a closed loop coords.append(coords[0]) # Reassemble coordinates to be a list of two-element lists coords = '[[' + ('],[').join(coords) + ']]' filters[key] = coords print(filters[key]) # Unnecessary, empty, or blank key elif key == "polygon_coord" or not filters[key] or filters[key] == '' or filters[key][0] == '': del filters[key] # Any other key else: # Turn list into a comma-separated string filters[key] = (',').join([e for e in filters[key] if e and e[0]]) print("Post-processing filters:") print(filters) filters["format"] = "json" #get sample data and use meta data to get pagination urls samples = get(env("API_HOST")+"samples/", params = filters).json() # print samples try: sample_results = samples["results"] except: try: error = samples["error"] print error samples = {} except: samples = {} next_url, prev_url, last_page, total = paginate_model("samples", samples, filters) #split location into (rounded!) latitude and longitude #make string of minerals for ... look and clean up date for s in sample_results: pos = s["location_coords"].split(" ") s["location_coords"] = [round(float(pos[2].replace(")","")),5),round(float(pos[1].replace("(","")),5)] s["minerals"] = (", ").join([m["name"] for m in s["minerals"]]) if s["collection_date"]: s["collection_date"] = s["collection_date"][:-10] return render_template("samples.html", samples = sample_results, showmap = "showmap" in filters, extends = "render" in filters, total = total, next_url = next_url, prev_url = prev_url, first_page = url_for("samples")+"?"+urlencode(filters), last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None) )
def chemical_analyses(): #similar to samples filters = dict(request.args) for key in filters.keys(): f = "" for i in range(len(filters[key])): if filters[key][i]: f += str(filters[key][i])+',' if f: filters[key] = f[:-1] else: del filters[key] filters["format"] = "json" if "minerals_and" in filters: del filters["minerals_and"] # handle sorting sorting_dict = {'Point':'spot_id','Analysis Method':'analysis_method','Analysis Material':'mineral','Subsample Type':'subsample_type', \ 'Analysis Location':'where_done','Owner':'owner','Reference':'reference','Analyst':'analyst','Analysis Date':'analysis_date','Total':'total'} if 'ordering' in filters and filters['ordering'] != ['']: sorting_name = filters['ordering'] else: sorting_name = 'Point' # default if sorting_name in sorting_dict: # all but second page filters['ordering'] = sorting_dict[sorting_name] else: # second page rev_sorting_dict = dict((v, k) for k, v in sorting_dict.iteritems()) sorting_name = rev_sorting_dict[filters['ordering'].strip("'").strip('[').strip(']')] # dynamic fields fields_dict = {'Sample Number':'sample','Subsample':'subsample','Point':'spot_id','Analysis Method':'analysis_method','Analysis Material':'mineral', \ 'Stage X':'stage_x','Stage Y':'stage_y','Reference':'reference','Reference X':'reference_x','Reference Y':'reference_y','Subsample Type':'subsample_type', \ 'Analysis Location':'where_done','Elements':'elements','Oxides':'oxides','Owner':'owner', \ 'Analyst':'analyst','Analysis Date':'analysis_date','Total':'total'} fields_list, fields_dict, field_names = handle_fields(filters,False) filters['fields'] = fields_list[0] headers = None if session.get("auth_token", None): headers = {"Authorization": "Token "+session.get("auth_token")} else: filters["public_data"] = True # Make chemicals private visible to uploaders chemicals = get(env("API_HOST")+"chemical_analyses/", params = filters,headers=headers).json() chem_results = chemicals["results"] next_url, prev_url, last_page, total, page_num = paginate_model("chemical_analyses", chemicals, filters) for c in chem_results: if "analysis_date" in c and c['analysis_date']: c["analysis_date"] = c["analysis_date"][:-10] csv_url = env("API_HOST") + "chemical_analyses/?" + urlencode(filters) + "&format=csv" return render_template("chemical_analyses.html", chemical_analyses = chem_results, field_names = field_names, fields_dict = fields_dict, sorting_dict = sorted(sorting_dict), sorting_name = sorting_name, total = total, page_num = page_num, next_url = next_url, prev_url = prev_url, first_page = url_for("chemical_analyses")+"?"+urlencode(filters), extends = "render" in filters, last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None), csv_url = csv_url )
def samples(): #filters sent as parameters to API calls #only return public data samples if not logged in # put header data here headers = None #this ideally should do nothing if passed to the metpetdb api filters = dict(request.args) tmp_var = 0 print("Tokens:") print(session) print("Pre-processing filters:") print(filters) # Gather polygons to be drawn on map polygons = [] if "metamorphic_regions" in filters: names = [] # Keep names of regions because sample only stores name of its metamorphic region, not object for mr in filters["metamorphic_regions"][0].split(","): region = get(env("API_HOST")+"metamorphic_regions/" + mr, params={"fields":"shape,name", "format":"json"}).json() if "shape" in region: polygons.append({"type": "metamorphic_region", "points": extract_points_from_shape(region["shape"])}) names.append(region["name"]) filters["metamorphic_regions"] = [",".join(names)] provenance_fields = ["owners", "collectors", "numbers", "references"] for field in provenance_fields: # replace comma-space separated elements with just comma (or API will try to match names starting with spaces) if field in filters: filters[field][0] = filters[field][0].split(",") filters[field][0] = ",".join([x.strip(" ") for x in filters[field][0] if x != ""]) if filters[field][0] == "": # delete field if resulting input is an empty string del filters[field] # handle sorting sorting_dict = {'Sample Number':'number','Subsample Count':'subsamples', 'Collection Date':'collection_date','Subsamples':'subsamples', 'Country':'country', \ 'Images':'images','Metamorphic Grades':'metamorphic_grades','Owner':'owner__name', 'References':'references__name', 'Rock Type':'rock_type__name', \ 'Chemical Analyses':'chemical_analyses'} if 'ordering' in filters and filters['ordering'] != ['']: sorting_name = filters['ordering'][0] else: sorting_name = 'Sample Number' # default if sorting_name in sorting_dict: #b all but second page filters['ordering'] = [sorting_dict[sorting_name]] else: # second page rev_sorting_dict = dict((v, k) for k, v in sorting_dict.iteritems()) sorting_name = rev_sorting_dict[filters['ordering'][0]] # format fields in request and keep list of titles for interface filters['fields'], fields_dict, field_names = handle_fields(filters, True) for key in filters.keys(): # Strip unused time values for start & end date queries if (key == "start_date" or key == "end_date") and filters[key]: filters[key] = filters[key][0][0:10] # Unnecessary, empty, or blank key elif key == "polygon_coord": # or not filters[key] or filters[key] == '' or filters[key][0] == '': del filters[key] elif key == "polygon_coords": # pre-process polygon_coords filters[key] = filters[key][0].rstrip(",") # strip off trailing commas from frontend # Add first point to the end of list as it is required by the backend to "close" the polygon # use regex because the input is a string, not a list first_point = re.search("\[(\-)?[0-9]+(\.[0-9]+)?,(\-)?[0-9]+(\.[0-9]+)?\]", filters[key]) if first_point: filters[key] += "," + first_point.group(0) # Finally, enclose the string in brackets since we need to send a list of points filters[key] = "[" + filters[key] + "]" # Any other key else: # Turn list into a comma-separated string filters[key] = (',').join([e for e in filters[key] if e and e[0]]).rstrip(",") # strip trailing commas print("Post-processing filters:") print(filters) filters["format"] = "json" try: headers = {"Authorization":"Token "+session["auth_token"]} samples = get(env("API_HOST")+"samples/", params = filters,headers= headers).json() print "URL: ", get(env("API_HOST")+"samples/", params = filters,headers= headers) except KeyError: samples = get(env("API_HOST")+"samples/",params = filters).json() try: sample_results = samples["results"] except: try: error = samples["error"] print error samples = {} sample_results = {} except: samples = {} sample_results = {} # Decode utf8 before encoding as urld for k, v in filters.iteritems(): if isinstance(v, unicode): v = v.encode("utf8") elif isinstance(v, str): v.decode("utf8") filters[k] = v next_url, prev_url, last_page, total, page_num = paginate_model("samples", samples, filters) # clean up values for samples view for s in sample_results: if "metamorphic_grades" in s: s["metamorphic_grades"] = (", ").join([g for g in sorted(s["metamorphic_grades"])]) if "metamorphic_regions" in s: s["metamorphic_regions"] = (", ").join([r for r in sorted(s["metamorphic_regions"])]) if "minerals" in s: s["minerals"] = (", ").join([m for m in sorted(s["minerals"])]) if "references" in s: s["references"] = (", ").join([r for r in sorted(s["references"])]) if "regions" in s: s["regions"] = (", ").join([m for m in sorted(s["regions"])]) csv_url = env("API_HOST") + "samples/?" + urlencode(filters) + "&format=csv" return render_template("samples.html", samples = sample_results, field_names = field_names, fields_dict = fields_dict, sorting_dict = sorting_dict, sorting_name = sorting_name, showmap = "showmap" in filters, extends = "render" in filters, polygons = polygons, total = total, page_num = page_num, next_url = next_url, prev_url = prev_url, first_page = url_for("samples")+"?"+urlencode(filters), last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None), csv_url = csv_url )
def samples(): #filters sent as parameters to API calls #only return public data samples if not logged in # put header data here headers = None #this ideally should do nothing if passed to the metpetdb api filters = dict(request.args) tmp_var = 0 print("tokens") print(session) print("Pre-processing filters") print(filters) for key in filters.keys(): # Key is a map polygon if key == "polygon_coords" and filters[key][0]: # List of coordinate point strings; remove trailing comma, starting/trailing bracket, # and split into coordinate points coords = filters[key][0].strip(',').strip('[').strip(']').split('],[') # Tack the first coordinate on to the end of the list so that the coordinates # form a closed loop coords.append(coords[0]) # Reassemble coordinates to be a list of two-element lists coords = '[[' + ('],[').join(coords) + ']]' filters[key] = coords print(filters[key]) # Unnecessary, empty, or blank key elif key == "polygon_coord" or not filters[key] or filters[key] == '' or filters[key][0] == '': del filters[key] # Any other key else: # Turn list into a comma-separated string filters[key] = (',').join([e for e in filters[key] if e and e[0]]) print("Post-processing filters:") print(filters) filters["format"] = "json" samples = {} try: headers = {"Authorization":"Token "+session["auth_token"]} print "HEADER:",headers samples = get(env("API_HOST")+"samples/", params = filters,headers= headers).json() print "URL: ",get(env("API_HOST")+"samples/", params = filters,headers= headers) except KeyError: samples = get(env("API_HOST")+"samples/",params = filters).json() # print samples try: sample_results = samples["results"] except: try: error = samples["error"] print error samples = {} except: samples = {} next_url, prev_url, last_page, total = paginate_model("samples", samples, filters) #split location into (rounded!) latitude and longitude #make string of minerals for ... look and clean up date for s in sample_results: pos = s["location_coords"].split(" ") s["location_coords"] = [round(float(pos[2].replace(")","")),5),round(float(pos[1].replace("(","")),5)] s["minerals"] = (", ").join([m["name"] for m in s["minerals"]]) if s["collection_date"]: s["collection_date"] = s["collection_date"][:-10] return render_template("samples.html", samples = sample_results, showmap = "showmap" in filters, extends = "render" in filters, total = total, next_url = next_url, prev_url = prev_url, first_page = url_for("samples")+"?"+urlencode(filters), last_page = last_page, auth_token = session.get("auth_token",None), email = session.get("email",None), name = session.get("name",None) )