Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit 7ee9840

Browse files
committed
Initial API services for login, occurrence, layer; unfinished; untested
1 parent b5b61ec commit 7ee9840

File tree

5 files changed

+412
-172
lines changed

5 files changed

+412
-172
lines changed

LmWebServer/flask_app/base.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""The module provides a base Lifemapper service class
22
"""
3-
from flask import Flask, session, request, json
4-
from werkzeug.exceptions import HTTPException
3+
from flask import Flask, session
54

65
from LmCommon.common.lmconstants import DEFAULT_POST_USER
76
from LmServer.common.localconstants import PUBLIC_USER
@@ -36,6 +35,24 @@ class in case we decide that we need to use a different mechanism (such
3635
# self.log = cherrypy.session.log
3736
self.log = log
3837

38+
# ..........................
39+
@staticmethod
40+
def get_user(self, user_id=None):
41+
"""Gets the user id for the service call.
42+
43+
Gets the user id for the service call. If urlUser is provided, try
44+
that first. Then try the session and finally fall back to the
45+
PUBLIC_USER
46+
47+
TODO: Save the username in the session
48+
"""
49+
svc = LmService()
50+
if user_id is None:
51+
svc.get_user_id()
52+
# Check to see if we should use url user
53+
usr = svc.scribe.find_user(user_id)
54+
return usr
55+
3956
# ..........................
4057
@staticmethod
4158
def get_user_id(url_user=None):
@@ -49,9 +66,9 @@ def get_user_id(url_user=None):
4966
"""
5067
# Check to see if we should use url user
5168
if url_user is not None:
52-
if url_user.lower() == 'public'.lower():
69+
if url_user.lower() == 'public':
5370
return PUBLIC_USER
54-
if url_user.lower() == DEFAULT_POST_USER.lower():
71+
if url_user.lower() == DEFAULT_POST_USER:
5572
return DEFAULT_POST_USER
5673
# Try to get the user from the session
5774
try:

LmWebServer/flask_app/layer.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""This module provides REST services for Layers"""
2+
import werkzeug.exceptions as WEXC
3+
4+
from LmCommon.common.lmconstants import HTTPStatus
5+
from LmWebServer.common.lmconstants import HTTPMethod
6+
from LmWebServer.services.api.v2.base import LmService
7+
from LmWebServer.services.common.access_control import check_user_permission
8+
from LmWebServer.services.cp_tools.lm_format import lm_formatter
9+
10+
11+
# .............................................................................
12+
class LayerService(LmService):
13+
"""Class for layers web service."""
14+
15+
16+
# ................................
17+
@lm_formatter
18+
def count_env_layers(
19+
self, user_id, after_time=None, before_time=None, alt_pred_code=None, date_code=None,
20+
env_code=None, env_type_id=None, epsg_code=None, gcm_code=None, scenario_code=None):
21+
"""Count environmental layer objects matching the specified criteria
22+
23+
Args:
24+
user_id (str): The user authorized for this operation. Note that this may not be
25+
the same user as is logged into the system
26+
after_time (float): Time in MJD of the earliest modtime for filtering
27+
before_time (float): Time in MJD of the latest modtime for filtering
28+
alt_pred_code (str): Code of the GCM scenario for filtering predicted environmental layera
29+
date_code (str): Code of the date for filtering predicted environmental layers (for past, present, future)
30+
epsg_code (str): EPSG code for the SRS for filtering layers
31+
env_code (str): Environmental type code for filtering environmental layers
32+
env_type_id (int): Database key of the environmental type for filtering environmental layers
33+
gcm_code (str) = GCM code for filtering environmental layers
34+
scenario_code (str): Database key for filtering to environmental layers belonging to one scenario
35+
"""
36+
layer_count = self.scribe.count_env_layers(
37+
user_id=user_id, after_time=after_time, before_time=before_time, env_code=env_code, gcm_code=gcm_code,
38+
alt_pred_code=alt_pred_code, date_code=date_code, epsg=epsg_code, env_type_id=env_type_id,
39+
scenario_code=scenario_code)
40+
41+
return {'count': layer_count}
42+
43+
# ................................
44+
@lm_formatter
45+
def count_layers(self, user_id, after_time=None, before_time=None, epsg_code=None, squid=None):
46+
"""Return a count of layers matching the specified criteria
47+
48+
Args:
49+
user_id (str): The user authorized for this operation. Note that this may not be
50+
the same user as is logged into the system
51+
after_time (float): Time in MJD of the earliest modtime for filtering
52+
before_time (float): Time in MJD of the latest modtime for filtering
53+
alt_pred_code (str): Code of the GCM scenario for filtering predicted environmental layera
54+
date_code (str): Code of the date for filtering predicted environmental layers (for past, present, future)
55+
epsg_code (str): EPSG code for the SRS for filtering layers
56+
squid (str): Unique taxon identifier for filtering layers
57+
"""
58+
layer_count = self.scribe.count_layers(
59+
user_id=user_id, squid=squid, after_time=after_time, before_time=before_time, epsg=epsg_code)
60+
61+
return {'count': layer_count}
62+
63+
# ................................
64+
@lm_formatter
65+
def get_layer(self, user_id, layer_id, env_layer=False):
66+
"""Return a layer
67+
68+
Args:
69+
user_id (str): The user authorized for this operation. Note that this may not be
70+
the same user as is logged into the system
71+
layer_id (int): A database identifier for a requested layer.
72+
env_layer (bool): flag - True restricts the search to environmental layers; False returns non-specific layers
73+
"""
74+
if env_layer:
75+
lyr = self.scribe.get_env_layer(lyr_id=layer_id)
76+
else:
77+
lyr = self.scribe.get_layer(lyr_id=layer_id)
78+
79+
if lyr is None:
80+
if env_layer:
81+
return WEXC.NotFound('Environmental layer {} was not found'.format(layer_id))
82+
else:
83+
return WEXC.NotFound('Layer {} was not found'.format(layer_id))
84+
85+
if check_user_permission(user_id, lyr, HTTPMethod.GET):
86+
return lyr
87+
else:
88+
return WEXC.Forbidden('User {} does not have permission to access layer {}'.format(
89+
user_id, layer_id))
90+
91+
# ................................
92+
@lm_formatter
93+
def list_env_layers(
94+
self, user_id, after_time=None, before_time=None, alt_pred_code=None, date_code=None,
95+
env_code=None, env_type_id=None, epsg_code=None, gcm_code=None, scenario_code=None,
96+
limit=100, offset=0):
97+
"""Return a list of environmental layers matching the specified criteria
98+
99+
Args:
100+
user_id (str): The user authorized for this operation. Note that this may not be
101+
the same user as is logged into the system
102+
after_time (float): Time in MJD of the earliest modtime for filtering
103+
before_time (float): Time in MJD of the latest modtime for filtering
104+
alt_pred_code (str): Code of the GCM scenario for filtering predicted environmental layera
105+
date_code (str): Code of the date for filtering predicted environmental layers (for past, present, future)
106+
epsg_code (str): EPSG code for the SRS for filtering layers
107+
env_code (str): Environmental type code for filtering environmental layers
108+
env_type_id (int): Database key of the environmental type for filtering environmental layers
109+
gcm_code (str) = GCM code for filtering environmental layers
110+
layer_type (int): Code for filtering on environmental or other layer type.
111+
0/None = all; 1 = environmental layer; 2 = Not yet implemented
112+
scenario_code (str): Code for filtering to environmental layers belonging to one scenario
113+
limit (int): Number of records to return
114+
offset (int): Offset for starting record of records to return
115+
"""
116+
lyr_atoms = self.scribe.list_env_layers(
117+
offset, limit, user_id=user_id, after_time=after_time, before_time=before_time,
118+
env_code=env_code, gcm_code=gcm_code, alt_pred_code=alt_pred_code,
119+
date_code=date_code, epsg=epsg_code, env_type_id=env_type_id, scen_code=scenario_code)
120+
121+
return lyr_atoms
122+
123+
# ................................
124+
def list_layers(
125+
self, user_id, after_time=None, before_time=None, epsg_code=None, squid=None,
126+
limit=100, offset=0):
127+
"""Return a list of layers matching the specified criteria
128+
129+
Args:
130+
user_id (str): The user authorized for this operation. Note that this may not be
131+
the same user as is logged into the system
132+
after_time (float): Time in MJD of the earliest modtime for filtering
133+
before_time (float): Time in MJD of the latest modtime for filtering
134+
epsg_code (str): EPSG code for the SRS for filtering layers
135+
squid (str): Unique identifier or filtering to a taxon associated with the layer
136+
limit (int): Number of records to return
137+
offset (int): Offset for starting record of records to return
138+
"""
139+
layer_atoms = self.scribe.list_layers(
140+
offset, limit, user_id=user_id, after_time=after_time, before_time=before_time, epsg=epsg_code, squid=squid)
141+
142+
return layer_atoms

0 commit comments

Comments
 (0)