Commit 7b9eef6b authored by Jorge Samuel Mendes de Jesus's avatar Jorge Samuel Mendes de Jesus
Browse files

Sync with server content

Former-commit-id: 81f1e6be
Former-commit-id: 912ae61f2ba18c7be53fd609fe169bd3fa889004 [formerly 4d51ff113f0529b8f20d0e25f71784ebbc0e9e60]
Former-commit-id: 1b34c343a57a358544b61f39f0914c80be3cf7c6
parent 0845e2cc
import sys,os
#sys.path.append('../')
sys.path.insert(0,os.path.abspath(os.path.dirname(__file__)))
from appWSM import app as application
#https://github.com/humiaozuzu/awesome-flask
from flask import Flask
from flask_restful import Api
from resources.meta import Meta
import logging
app = Flask(__name__)
api = Api(app)
file_handler = logging.FileHandler('app.log')
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
api.add_resource(Meta, '/')
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
from flask import Flask,make_response,render_template
from flask.ext.restplus import Api, Resource, fields
app = Flask(__name__)
api = Api(app, version='0.1', title='WSM API',
description='Simple API for the WSM',
)
ns = api.namespace('monoliths', description='World Soil Museum Monoliths')
# TODOS = {
# 'todo1': {'task': 'build an API'},
# 'todo2': {'task': '?????'},
# 'todo3': {'task': 'profit!'},
# }
#
# todo = api.model('Todo', {
# 'task': fields.String(required=True, description='The task details')
# })
#
# listed_todo = api.model('ListedTodo', {
# 'id': fields.String(required=True, description='The todo ID'),
# 'todo': fields.Nested(todo, description='The Todo')
# })
#
#
# def abort_if_todo_doesnt_exist(todo_id):
# if todo_id not in TODOS:
# api.abort(404, "Todo {} doesn't exist".format(todo_id))
#
# parser = api.parser()
# parser.add_argument('task', type=str, required=True, help='The task details', location='form')
#
# @ns.route('/<string:todo_id>')
# @api.doc(responses={404: 'Todo not found'}, params={'todo_id': 'The Todo ID'})
# class Todo(Resource):
# '''Show a single todo item and lets you delete them'''
# @api.doc(description='todo_id should be in {0}'.format(', '.join(TODOS.keys())))
# @api.marshal_with(todo)
# def get(self, todo_id):
# '''Fetch a given resource'''
# abort_if_todo_doesnt_exist(todo_id)
# return TODOS[todo_id]
#
#
#
# @ns.route('/')
# class TodoList(Resource):
# '''Shows a list of all todos, and lets you POST to add new tasks'''
# @api.marshal_list_with(listed_todo)
# def get(self):
# '''List all todos'''
# return [{'id': id, 'todo': todo} for id, todo in TODOS.items()]
#
# @api.doc(parser=parser)
# @api.marshal_with(todo, code=201)
# def post(self):
# '''Create a todo'''
# args = parser.parse_args()
# todo_id = 'todo%d' % (len(TODOS) + 1)
# TODOS[todo_id] = {'task': args['task']}
# return TODOS[todo_id], 201
hello_fields = api.model('Hello', {'entry': fields.Url})
model = api.model('Model', {
'name': fields.String,
})
@ns.route('/')
@api.doc(responses={200: 'index.html'}, params={},headers = {'Content-Type': 'text/html'})
class Monolith(Resource):
"""Class that return the monoliths available"""
#@api.doc(responses={200: 'index.html'})
#@api.doc(description="Returns and html page with content")
@api.doc(model=hello_fields, params={'entry': 'The entry to wrap'})
@api.response(200, 'Success', model)
def get(self):
#return 'You want path: %s' % path
headers = {'Content-Type': 'text/html'}
#return render_template('index.html')
return make_response(render_template('index.html'),200,headers)
#return "Yes I have monoliths"
if __name__ == '__main__':
app.run(debug=True)
from flask import request
from collections import defaultdict
import re
from secret import DB
import psycopg2,psycopg2.extras
def get_db_connection():
"""Getting a DB connection based on a loaded config that contains the ip and login. This file is not part of repositor
"""
conn = psycopg2.connect(host=DB["host"],database=DB["database"], user=DB["user"], password=DB["password"])
dictCur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
return dictCur
def get_url_params(testing=False,req=None):
"""Parses the URL send by user and returns a dictionary, cleaning of string goes here
>>> get_url_params(testing=True,req=[('country', [u'BR,Es'])])
defaultdict(<type 'list'>, {'country': [u'BR', u'Es']})
>>> get_url_params(testing=True,req=[('country', [u'BR#$'])])
defaultdict(<type 'list'>, {'country': [u'BR']})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u''])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES']})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u''])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES']})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u'1'])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES'], 'limit': 1})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u'15.3'])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES']})
"""
if testing:
l=req
else:
l=request.args.lists()
d=defaultdict(list)
for k,v in l:
#Checkcing and cleaning the country structure
if (k.lower()=='country'):
countryStr=v[0]
countryList=countryStr.split(",")
countryList=[re.sub('\W+','',country) for country in countryList] #only leters and numbers allowed
d[k]=countryList
#cleaning and making an int
elif (k.lower()=='limit'):
try:
d[k]=int(v[0])
except:
pass
else:
d[k]=v
return d
if __name__ == "__main__":
#print get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u'15.3'])])
import doctest
doctest.testmod(verbose=1)
......@@ -14,12 +14,13 @@ def filter_countries(countryList,fileList):
>>> len(filter_limit(5,["/tmp/JM-006-def.1per.jpg", "/tmp/ES-011-def.1per.jpg", "/tmp/IT-016-def.1per.jpg", "/tmp/CO-002-def.1per.jpg", "/tmp/CN-003-def.1per.jpg", "/tmp/TW-020-def.1per.jpg", "/tmp/BR-012-def.1per.jpg", "/tmp/SU-003-def.1per.jpg", "/tmp/NL-002-def.1per.jpg"]))
5
"""
searchString="^("+"|".join(countryList).lower()+")"
res=[f for f in fileList if re.search(searchString,os.path.basename(f).lower())]
return res
def filter_limit(limit,fileList,randomize=True):
def filter_limit(limit,fileList,randomize=False):
"""Randomizes the file list"""
if randomize:
shuffle(fileList)
......@@ -40,8 +41,9 @@ def filter_extra(fileList):
#conn = psycopg2.connect(host="85.214.35.184",database="isric", user="jjesus", password="isric126")
#dictCur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
dictCur=get_db_connection()
dictCur.execute("select * from rest.vw_isis_classification_profile_geom")
#dictCur.execute("select * from rest.vw_isis_classification_profile_geom")
#dictCur.execute("select * from web_isis.vw_isis_profiles_with_classification_geo_img_rest")
dictCur.execute("select * from web_isis.vw_isis_profiles_with_classification_geo_img_rest")
records = dictCur.fetchall()
res=[]
for file in fileList:
......@@ -51,15 +53,15 @@ def filter_extra(fileList):
#search for fileCode in database record
for record in records:
if record["profile_code"]==fileCode:
res.append({"filename":fileName,"classification":record["classification"],"profile_id":record["profile_id"]})
#res.append({"filename":fileName,"classification":record["classification"],"profile_id":record["profile_id"],"publication_year":record["publication_year"]})
#res.append({fileName:{"classification":record["classification"],"profile_id":record["profile_id"],"publication_year":record["publication_year"]}})
res.append({"filename":fileName,"classification":record["classification"],"profile_id":record["profile_id"],"publication_year":record["publication_year"]})
found=True
break
if not found:
pass
#res.append({"filename":None,"classification":None,"profile_id":None,"publication_year":None})
#res.append({fileName:None})
pass
#res.append({"filename":None,"classification":None,"profile_id":None,"publication_year":None})
# res.append({fileName:None})
return res
#['classification', 'profile_id', 'profile_code', 'publication_year', 'geom', 'dataset_id']
......@@ -83,7 +85,7 @@ def filter_classification(classification,fileList):
#dictCur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)\
dictCur=get_db_connection()
#select * from rest.vw_isis_classification_profile_geom where classification ilike 'andosols';
dictCur.execute("select * from rest.vw_isis_classification_profile_geom where classification ilike %s",(classification[0],))
dictCur.execute("select * from web_isis.vw_isis_profiles_with_classification_geo_img_rest where classification ilike %s",(classification[0],))
records = dictCur.fetchall()
res=[]
for file in fileList:
......@@ -100,8 +102,8 @@ def filter_classification(classification,fileList):
if __name__ == "__main__":
#import doctest
#doctest.testmod(verbose=1)
import doctest
doctest.testmod(verbose=1)
#countryList=["es"]
#print filter_classification("lixisols",["BR-002-def.1per.tif","BR-015-def.1per.tif"])
#print filter_classification("foooo",["BR-002-def.1per.tif","BR-015-def.1per.tif"])
......@@ -111,7 +113,7 @@ if __name__ == "__main__":
#{"profile_id": 69753, "publication_year": 2006, "classification": "Ferralsols"}},
#fileList=[u'JM-006-def.1per.jpg', u'FR-011-def.1per.jpg', u'IT-016-def.1per.jpg']
print filter_extra([u'JM-006-def.1per.jpg', u'FR-011-def.1per.jpg', u'IT-016-def.1per.jpg'])
#print filter_extra([u'JM-006-def.1per.jpg', u'FR-011-def.1per.jpg', u'IT-016-def.1per.jpg'])
#print filter_countries(countryList,fileList)
\ No newline at end of file
"""Password of database"""
DB={"host":"85.214.35.184","database":"isric", "user":"jjesus", "password":"isric126"}
\ No newline at end of file
from flask import request
from collections import defaultdict
import re
from secret import DB
import psycopg2,psycopg2.extras
def get_db_connection():
"""Getting a DB connection based on a loaded config that contains the ip and login. This file is not part of repositor
"""
conn = psycopg2.connect(host=DB["host"],database=DB["database"], user=DB["user"], password=DB["password"])
dictCur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
return dictCur
def get_url_params(testing=False,req=None):
"""Parses the URL send by user and returns a dictionary, cleaning of string goes here
>>> get_url_params(testing=True,req=[('country', [u'BR,Es'])])
defaultdict(<type 'list'>, {'country': [u'BR', u'Es']})
>>> get_url_params(testing=True,req=[('country', [u'BR#$'])])
defaultdict(<type 'list'>, {'country': [u'BR']})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u''])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES']})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u''])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES']})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u'1'])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES'], 'limit': 1})
>>> get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u'15.3'])])
defaultdict(<type 'list'>, {'country': [u'BR', u'ES']})
"""
if testing:
l=req
else:
l=request.args.lists()
d=defaultdict(list)
for k,v in l:
#Checkcing and cleaning the country structure
if (k.lower()=='country'):
countryStr=v[0]
countryList=countryStr.split(",")
countryList=[re.sub('\W+','',country) for country in countryList] #only leters and numbers allowed
d[k]=countryList
#cleaning and making an int
elif (k.lower()=='limit'):
try:
d[k]=int(v[0])
except:
pass
else:
d[k]=v
return d
if __name__ == "__main__":
#print get_url_params(testing=True,req=[('country', [u'BR,ES']), ('limit', [u'15.3'])])
import doctest
doctest.testmod(verbose=1)
\ No newline at end of file
......@@ -4,6 +4,7 @@ from flask import render_template,make_response,send_file
from flask.ext.restplus import Resource
import json
import os.path,os
from natsort import natsorted
from common.util import get_url_params
from common.filters import filter_countries,filter_limit,filter_extra,filter_classification
......@@ -67,23 +68,24 @@ class Monoliths(Resource):
if os.path.isdir(monolithPath):
#send JSON with content
#http://127.0.0.1:5000/monoliths/10per/jpg
listFile=os.listdir(monolithPath)
listFile=natsorted(os.listdir(monolithPath))
req=get_url_params()
if req["country"]:
listFile=filter_countries(req["country"],listFile)
listFile=filter_countries(req["country"],listFile)
if req["classification"]:
listFile=filter_classification(req["classification"],listFile)
if req["limit"]:
listFile=filter_limit(req["limit"] ,listFile)
#PASOP THIS HAS TO BE THE LAST FILTER
if req["extra"]:
if req["extra"]:
listFile=filter_extra(listFile)
headers = {'Content-Type': 'application/json'}
response = json.dumps(listFile)
return make_response(response,200,headers)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment