-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
106 lines (86 loc) · 3.17 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
sys.path.append('/home/anddyc1/py_libs/')
import web, ofdb, pages
import simplejson as json
# set urls for the application #
urls = ( '/', 'index',
'/about', 'about',
'/contact', 'contact',
'/api-info', 'api_info',
'/api/landings', 'landings',
'/api/landings/countries', 'countries',
'/api/landings/countries/(.+)', 'single_country',
'/api/landings/species', 'species',
'/api/landings/species/(.+)', 'single_species',
'/api/landings/areas', 'areas',
'/api/landings/countries/(.+)/species/(.+)', 'country_species',
'/download', 'download' )
# start the application #
app = web.application(urls, globals())
# templating
render = web.template.render('templates/')
# common functions #
def result_to_json(query):
result = ofdb.db.query(query)
return json.dumps( [ row for row in result ], use_decimal=True )
# the url classes #
class download:
def GET(self):
return render.layout("title here", "something here")
class single_species:
def GET(self, species):
q = "SELECT year, Sum(catch) as catch FROM capture WHERE a3_code = '" + species
q += "' GROUP BY year ORDER BY year"
web.header('Content-Type', 'application/json')
return result_to_json(q)
class species:
def GET(self):
q = "SELECT scientific_name, english_name, a3_code, taxocode, isscaap FROM asfis"
web.header('Content-Type', 'application/json')
return result_to_json(q)
class areas:
def GET(self):
return "hello areas"
class countries:
def GET(self):
q = "SELECT country, iso3c FROM countries ORDER BY country"
web.header('Content-Type', 'application/json')
return result_to_json(q)
class single_country:
def GET(self, country):
q = "SELECT year, Sum(catch) as catch from capture WHERE "
q += "iso3c = '" + country + "' GROUP BY year ORDER BY year"
web.header('Content-Type', 'application/json')
return result_to_json(q)
class country_species:
def GET(self, country, species):
q = "SELECT year, Sum(catch) as catch FROM capture WHERE "
q += "iso3c = '" + country + "' AND a3_code = '" + species + "'"
web.header('Content-Type', 'application/json')
return result_to_json(q)
class landings:
def GET(self):
q = "SELECT year, Sum(catch) as catch from capture GROUP BY year ORDER BY year"
web.header('Content-Type', 'application/json')
return result_to_json(q)
class index:
def GET(self):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
return pages.home
class about:
def GET(self):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
return pages.about
class contact:
def GET(self):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
return pages.contact
class api_info:
def GET(self):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
return pages.api_info
if __name__ == "__main__":
app.run()
else:
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func,
addr)