meta.py 3.76 KB
Newer Older
1
#from flask_restful import Resource
2
from flask import current_app
3
from flask import render_template,make_response,send_file
4
from flask_restplus import Resource
5
6
import json
import os.path,os
7
from natsort import natsorted
8

9
10
from common.util import get_url_params
from common.filters import filter_countries,filter_limit,filter_extra,filter_classification
11
12
13
14
15
16
17
18

class Logger(object):
    """We cant get the logging object in the resources"""
    def info(self):
         app = current_app._get_current_object()
         return app.logger


19
20
21
     
class Monolith(Resource):
    """Class that return the monoliths available"""
22
23
24
    def __init__(self):
        self.logger = Logger().info()
    def get(self):
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
       #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"

class MonolithRoot(Resource):
    """Class that return the monoliths available"""
    def __init__(self):
        self.logger = Logger().info()
    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)
       headers = {'Content-Type': 'application/json'}
       monolithsPath=os.path.join( os.path.dirname( __file__ ), '..',"./monoliths/" )
       #listDir=[o for o in os.listdir("./monoliths/") if os.path.isdir("./monoliths/"+o)]
       listDir=[o for o in os.listdir(monolithsPath) if os.path.isdir(os.path.join(monolithsPath,o))]
       
       
       response = json.dumps(listDir)
       return make_response(response,200,headers)

class CloseBy(Resource):
    def __init__(self):
        self.logger = Logger().info()
    def get(self):
        """Returns the closest profiles, from a certain point
        SELECT dataset_id,publication_year,classification,profile_id,profile_code,ST_Distance(geom::geography,st_setsrid(st_makepoint(-3.7,40),4326)::geography,false)/1000 as distance_km FROM rest.vw_isis_classification_profile_geom ORDER BY geom <-> st_setsrid(st_makepoint(-3.7,40),4326) LIMIT 10; --x,y
        """
        pass
    
class Monoliths(Resource):
    def __init__(self):
       self.logger = Logger().info()
    def get(self,path):
       monolithPath=os.path.join( os.path.dirname( __file__ ), '..',"./monoliths/",path )
       #monolithPath=os.path.join("./monoliths/"+path)
       #self.logger.info("./monoliths/"+path)
       #if os.path.isdir(relativePath)
      
       #http://127.0.0.1:5000/monoliths/1per/jpg?country=BR,ES
       if os.path.isdir(monolithPath):
           #send JSON with content
           #http://127.0.0.1:5000/monoliths/10per/jpg
71
72
73
           listFile=natsorted(os.listdir(monolithPath))

   
74
           req=get_url_params()
75
            
76
           if req["country"]:
77
78
               listFile=filter_countries(req["country"],listFile)
                        
79
80
81
82
83
84
           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
85
           if req["extra"]:
86
               listFile=filter_extra(listFile)
87
          
88
89
90
91
92
93
94
95
96
97
98
               
           headers = {'Content-Type': 'application/json'}
           response = json.dumps(listFile)
           return make_response(response,200,headers)
          #return json.dumps(os.listdir(relativePath)) #testing URL http://127.0.0.1:5000/monoliths/10per/jpg
       else:
           #send file
           return send_file(monolithPath)
           #send_file 
           #return 'You want path: %s' % str(os.path.isdir(relativePath))
       #return 1
99