-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add simple Django based domain selection view #700
base: master
Are you sure you want to change the base?
Changes from all commits
636b756
f213397
fb33c68
48cf79f
7975aaa
309939a
fb61d8d
6e65522
919b7d6
43d8f0a
565557b
478fa5a
3cb65a3
2a8d878
b23de0a
0b11f85
860a3c3
d62ea95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,14 @@ | |
from pathlib import Path | ||
from .. import datapane | ||
from .. import configuration | ||
from dmod.modeldata.hydrofabric import GeoPackageHydrofabric | ||
|
||
import logging | ||
logger = logging.getLogger("gui_log") | ||
|
||
_resolution_regex = re.compile("(.+) \((.+)\)") | ||
|
||
|
||
def _build_fabric_path(fabric, type): | ||
""" | ||
build a qualified path from the hydrofabric name and type | ||
|
@@ -34,52 +36,100 @@ def _build_fabric_path(fabric, type): | |
resolution = resolution_match.group(2) | ||
else: | ||
name = fabric | ||
resolution='' | ||
resolution = '' | ||
|
||
hyfab_data_dir = Path(PROJECT_ROOT, 'static', 'ngen', 'hydrofabric', name, resolution) | ||
|
||
geojson_file = hyfab_data_dir.joinpath(f"{type}_data.geojson") | ||
if geojson_file.exists(): | ||
return geojson_file | ||
|
||
if hyfab_data_dir.joinpath("hydrofabric.gpkg").exists(): | ||
geopackage_file = hyfab_data_dir.joinpath("hydrofabric.gpkg") | ||
elif hyfab_data_dir.joinpath(f"{name}.gpkg").exists(): | ||
geopackage_file = hyfab_data_dir.joinpath(f"{name}.gpkg") | ||
else: | ||
logger.error(f"Can't build fabric path: can't find hydrofabric data file in directory {hyfab_data_dir!s}") | ||
return None | ||
|
||
return geopackage_file | ||
|
||
path = Path(PROJECT_ROOT, 'static', 'ngen', 'hydrofabric', name, resolution, type+'_data.geojson') | ||
return path | ||
|
||
class Fabrics(APIView): | ||
def get(self, request: HttpRequest, fabric: str = None) -> typing.Optional[JsonResponse]: | ||
if fabric is None: | ||
fabric = 'example' | ||
fabric = 'example_fabric_name' | ||
type = request.GET.get('fabric_type', 'catchment') | ||
if not type: | ||
type="catchment" | ||
type = "catchment" | ||
|
||
id_only = request.GET.get("id_only", "false") | ||
if isinstance(id_only, str): | ||
id_only = id_only.strip().lower() == "true" | ||
else: | ||
id_only = bool(id_only) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a helper function in |
||
|
||
path = _build_fabric_path(fabric, type) | ||
|
||
if path is None: | ||
return None | ||
elif path.name == f"{type}_data.geojson": | ||
with open(path) as fp: | ||
data = json.load(fp) | ||
if id_only: | ||
return JsonResponse(sorted([feature["id"] for feature in data["features"]]), safe=False) | ||
else: | ||
return JsonResponse(data) | ||
elif path.name[-5:] == ".gpkg": | ||
hf = GeoPackageHydrofabric.from_file(geopackage_file=path) | ||
if id_only: | ||
if type == "catchment": | ||
return JsonResponse(sorted(hf.get_all_catchment_ids()), safe=False) | ||
elif type == "nexus": | ||
return JsonResponse(sorted(hf.get_all_nexus_ids()), safe=False) | ||
else: | ||
logger.error(f"Unsupported fabric type '{type}' for id_only geopackage in Fabrics API view") | ||
return None | ||
else: | ||
if type == "catchment": | ||
df = hf._dataframes[hf._DIVIDES_LAYER_NAME] | ||
elif type == "nexus": | ||
df = hf._dataframes[hf._NEXUS_LAYER_NAME] | ||
else: | ||
logger.error(f"Unsupported fabric type '{type}' for geopackage in Fabrics API view") | ||
return None | ||
return JsonResponse(json.loads(df.to_json())) | ||
else: | ||
logger.error(f"Can't make API request for hydrofabric '{fabric!s}'") | ||
return None | ||
|
||
with open(path) as fp: | ||
data = json.load(fp) | ||
return JsonResponse(data) | ||
|
||
class FabricNames(APIView): | ||
_fabric_dir = Path(PROJECT_ROOT, 'static', 'ngen', 'hydrofabric') | ||
_fabrics_root_dir = Path(PROJECT_ROOT, 'static', 'ngen', 'hydrofabric') | ||
|
||
def get(self, request: HttpRequest) -> JsonResponse: | ||
names = [] | ||
for f_name in self._fabric_dir.iterdir(): | ||
if f_name.is_dir(): | ||
for fabric_subdir in self._fabrics_root_dir.iterdir(): | ||
if fabric_subdir.is_dir(): | ||
#Check for sub dirs/resolution | ||
sub = False | ||
for r_name in f_name.iterdir(): | ||
for r_name in fabric_subdir.iterdir(): | ||
if r_name.is_dir(): | ||
names.append( '{} ({})'.format(f_name.name, r_name.name)) | ||
names.append(f'{fabric_subdir.name} ({r_name.name})') | ||
sub = True | ||
if not sub: | ||
names.append( '{}'.format(f_name.name) ) | ||
names.append(f'{fabric_subdir.name}') | ||
return JsonResponse(data={ | ||
"fabric_names": names | ||
}) | ||
|
||
|
||
class FabricTypes(APIView): | ||
def get(self, rquest: HttpRequest) -> JsonResponse: | ||
return JsonResponse( data={ | ||
"fabric_types": ['catchment', 'flowpath', 'nexus'] | ||
}) | ||
}) | ||
|
||
|
||
class ConnectedFeatures(APIView): | ||
def get(self, request: HttpRequest) -> JsonResponse: | ||
|
@@ -142,3 +192,56 @@ def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: | |
|
||
# Return the rendered page | ||
return render(request, 'maas/map.html', payload) | ||
|
||
|
||
class DomainView(View): | ||
|
||
""" | ||
A view used to render the map | ||
""" | ||
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: | ||
""" | ||
The handler for 'get' requests. This will render the 'map.html' template | ||
|
||
:param HttpRequest request: The request asking to render this page | ||
:param args: An ordered list of arguments | ||
:param kwargs: A dictionary of named arguments | ||
:return: A rendered page | ||
""" | ||
# If a list of error messages wasn't passed, create one | ||
if 'errors' not in kwargs: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A list of errors and stuff wouldn't get passed in here - it would be path variables. If the path to the domain view was something along the lines of |
||
errors = list() | ||
else: | ||
# Otherwise continue to use the passed in list | ||
errors = kwargs['errors'] # type: list | ||
|
||
# If a list of warning messages wasn't passed create one | ||
if 'warnings' not in kwargs: | ||
warnings = list() | ||
else: | ||
# Otherwise continue to use the passed in list | ||
warnings = kwargs['warnings'] # type: list | ||
|
||
# If a list of basic messages wasn't passed, create one | ||
if 'info' not in kwargs: | ||
info = list() | ||
else: | ||
# Otherwise continue to us the passed in list | ||
info = kwargs['info'] # type: list | ||
|
||
framework_selector = datapane.Input("framework", "select", "The framework within which to run models") | ||
for editor in configuration.get_editors(): | ||
framework_selector.add_choice(editor['name'], editor['description'], editor['friendly_name']) | ||
|
||
pprint(framework_selector.__dict__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean for this debug statement to stay in here? |
||
|
||
# Package everything up to be rendered for the client | ||
payload = { | ||
'errors': errors, | ||
'info': info, | ||
'warnings': warnings, | ||
'pane_inputs': [framework_selector] | ||
} | ||
|
||
# Return the rendered page | ||
return render(request, 'maas/domain.html', payload) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,9 @@ class Migration(migrations.Migration): | |
def create_superuser(apps, schema_editor): | ||
from django.contrib.auth.models import User | ||
|
||
SU_NAME = os.environ.get('DMOD_SU_NAME') | ||
SU_EMAIL = os.environ.get('DMOD_SU_EMAIL') | ||
SU_PASSWORD = os.environ.get('DMOD_SU_PASSWORD') | ||
SU_NAME = os.environ.get('DMOD_SU_NAME', "dmod_admin") | ||
SU_EMAIL = os.environ.get('DMOD_SU_EMAIL', "[email protected]") | ||
SU_PASSWORD = os.environ.get('DMOD_SU_PASSWORD', f"{SU_NAME}{os.environ.get('SQL_PASSWORD')}") | ||
|
||
superuser = User.objects.create_superuser( | ||
username=SU_NAME, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be wise to abstract out this logic into another function that would be somewhat "easy" to swap in and out. Won't the fabrics come via the data store in the future?