Skip to content
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

Revise open waterbody form #3665

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bims/models/location_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ def generate_site_code(
from bims.utils.site_code import (
fbis_catchment_generator,
rbis_catchment_generator,
wetland_catchment
wetland_catchment,
open_waterbody_catchment
)
from preferences import preferences
site_code = ''
Expand All @@ -546,6 +547,10 @@ def generate_site_code(
'wetlid' in location_site.additional_data
) else {}
catchment_site_code = wetland_catchment(lat, lon, wetland_data, wetland_name)
elif ecosystem_type.lower() == 'open waterbody':
catchment_site_code = open_waterbody_catchment(
lat, lon, river_name
)
else:
catchment_site_code, catchments_data = fbis_catchment_generator(
location_site=location_site,
Expand Down
19 changes: 15 additions & 4 deletions bims/static/js/non_requirejs/site_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ let validator = $('#site-form').validate({
return false;
}
// Check length
if (siteCode.length !== 12) {
showSiteCodeError();
return false;
if (ecosystemType == 'River') {
if (siteCode.length !== 12) {
showSiteCodeError();
return false;
}
} else {
if (siteCode.length !== 15) {
alert(siteCode.length)
showSiteCodeError();
return false;
}
}
// Check regex
let regex = /(\w{6})-(\w{5})/g;
if (ecosystemType == 'Open waterbody') {
regex = /(\w{4})-(\w{4})-(\w{5})/g;
}
let regexResult = regex.test(siteCode);
if (!regexResult) {
showSiteCodeError();
Expand Down Expand Up @@ -186,7 +197,7 @@ const updateSiteCode = (e) => {
document.getElementById('update-site-code').disabled = true;
button.html('Generating...');
siteCodeInput.prop('disabled', true);
let url = '/api/get-site-code/?user_river_name=' + userRiverName + '&lon=' + longitude + '&lat=' + latitude;
let url = '/api/get-site-code/?ecosystem_type=' + ecosystemType + '&user_river_name=' + userRiverName + '&lon=' + longitude + '&lat=' + latitude;
if (siteId) {
url += '&site_id=' + siteId
}
Expand Down
15 changes: 13 additions & 2 deletions bims/templates/location_site_form_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
const siteCodeGeneratorMethod = '{{ preferences.SiteSetting.site_code_generator }}';
const bingKey = '{{ bing_key }}';
const isFbis = {% if preferences.SiteSetting.default_data_source.lower == 'fbis' %}true{% else %}false{% endif %};
const ecosystemType = '{{ ecosystem_type }}';
</script>
<style>
.carousel {
Expand Down Expand Up @@ -156,7 +157,13 @@ <h2>
</div>
<div class="form-group row">
<label for="user_river_name"
class="col-sm-2 col-form-label col-form-label">User River Name</label>
class="col-sm-2 col-form-label col-form-label">
{% if ecosystem_type == 'Open waterbody' %}
User Open Waterbody Name
{% else %}
User River Name
{% endif %}
</label>
<div class="col-sm-10">
<input type="text" name="user_river_name" id="user_river_name"
class="form-control form-control-sm"
Expand All @@ -175,7 +182,11 @@ <h2>
<small id="siteCodeHelp" class="form-text text-muted">
The following standard has been adopted for naming site code: <br/>
{% if preferences.SiteSetting.default_data_source.lower == 'fbis' %}
Secondary catchment code, 1st four letters of river name, 1st five letters of location. E.g. X2CROC-VELOR (Crocodile River @ Veloren Vallei Nature Reserve)
{% if ecosystem_type == 'Open waterbody' %}
1st four letters of Quaternary code + 1st four letters of user open waterbody name or river name + site count.
{% else %}
Secondary catchment code, 1st four letters of river name, 1st five letters of location. E.g. X2CROC-VELOR (Crocodile River @ Veloren Vallei Nature Reserve)
{% endif %}
{% elif preferences.SiteSetting.default_data_source.lower == 'rbis' %}
Catchment + Province ID + District ID + site count. E.g. NNYU1205-00001
{% else %}
Expand Down
32 changes: 32 additions & 0 deletions bims/utils/site_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,35 @@ def wetland_catchment(lat, lon, wetland_data: Dict, user_wetland_name: str) -> s
wetland_site_code += user_wetland_name.replace(' ', '')[:4]

return wetland_site_code


def open_waterbody_catchment(lat, lon, user_open_waterbody_name: str) -> str:
"""
Generates a catchment code for a given open waterbody based on location and data.

:param user_open_waterbody_name: Open waterbody name from user
:param lat: Latitude of the location site
:param lon: Longitude of the location site
:return: The generated catchment code, e.g. L112-NAME
"""
site_code = ''
quaternary_catchment_area_key = 'quaternary_catchment_area'
quaternary_geocontext_key = 'quaternary_catchment_group'

catchments, catchments_data = _get_catchments_data(
lat=lat,
lon=lon,
catchment_key=quaternary_geocontext_key
)

try:
if quaternary_catchment_area_key in catchments:
site_code += catchments[quaternary_catchment_area_key]
site_code += '-'
except TypeError:
pass

if user_open_waterbody_name:
site_code += user_open_waterbody_name.replace(' ', '')[:4]

return site_code
2 changes: 1 addition & 1 deletion bims/views/location_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def handle_location_site_post_data(
)
river_name = post_data.get('river_name', None)
user_river_name = post_data.get('user_river_name', '')
if not river_name:
if not river_name and ecosystem_type.lower() == 'river':
river_name = user_river_name

site_code = post_data.get('site_code', None)
Expand Down
Loading