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

Handle wetland site data from mobile #3479

Merged
merged 1 commit into from
Sep 16, 2023
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
2 changes: 0 additions & 2 deletions bims/static/js/wetland_site.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ wetlandSiteCodeButton.click(function () {
wetlandName = '-' + wetlandName.substring(0, 4);
}

console.log('wetlandName', wetlandName)

siteCode += wetlandName;

let url = '/api/get-site-code/?user_site_code=' + siteCode;
Expand Down
2 changes: 1 addition & 1 deletion bims/views/location_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def handle_location_site_post_data(
date = post_data.get('date', datetime.now())
site_geometry = post_data.get('site-geometry', None)
wetland_id = post_data.get('wetland_id', None)
ecosystem_type = post_data.get('ecosystem_type', '')
ecosystem_type = post_data.get('ecosystem_type', '').capitalize()

wetland_name = post_data.get('wetland_name', '')
user_wetland_name = post_data.get('user_wetland_name', '')
Expand Down
54 changes: 43 additions & 11 deletions mobile/api_views/add_location_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
from rest_framework.views import APIView

from bims.views.location_site import handle_location_site_post_data
from bims.enums.ecosystem_type import (
ECOSYSTEM_RIVER, ECOSYSTEM_WETLAND
)
from bims.serializers.location_site_serializer import (
LocationSiteSerializer
)


class AddLocationSiteView(APIView):
Expand All @@ -30,13 +36,21 @@ def send_location_site_email(self, location_site: LocationSite):
current_site = Site.objects.get_current()

csv_data = [
["ID", "Site Code", "User Site Code", "River Name",
"User River Name", "Description", "Owner", "URL"],
["ID", "Site Code", "Ecosystem Type",
"User Site Code",
"River Name",
"User River Name",
"Wetland Name",
"User Wetland Name",
"Description", "Owner", "URL"],
[location_site.id,
location_site.site_code,
location_site.ecosystem_type.capitalize(),
location_site.legacy_site_code,
location_site.river.name if location_site.river else '-',
location_site.legacy_river_name,
location_site.wetland_name,
location_site.user_wetland_name,
location_site.site_description,
location_site.owner.username,
'http://{url}/location-site-form/update/?id={id}'.format(
Expand Down Expand Up @@ -83,18 +97,39 @@ def send_location_site_email(self, location_site: LocationSite):
def post(self, request, *args, **kwargs):
post_data = copy.deepcopy(request.data)

# Fetch river name
ecosystem_type = post_data.get(
'ecosystem_type', ECOSYSTEM_RIVER).capitalize()
river_name = post_data.get('river_name', '')
if not river_name:
river_name = fetch_river_name(
post_data.get('latitude'), post_data.get('longitude'))
site_code = ''
wetland_name = post_data.get('wetland_name', '')
user_wetland_name = post_data.get('user_wetland_name', '')

if ecosystem_type == ECOSYSTEM_WETLAND:
wetland_data = post_data.get('wetland_data', None)
if wetland_data:
site_code = wetland_data['quaternary'][:4]
post_data['hydrogeomorphic_type'] = wetland_data['hgm_type']
post_data['wetland_id'] = wetland_data['wetlid']
else:
pass
if wetland_name:
site_code += '-' + wetland_name.replace(' ', '')[:4]
else:
site_code += '-' + user_wetland_name.replace(' ', '')[:4]
else:
# Fetch river name
if not river_name:
river_name = fetch_river_name(
post_data.get('latitude'),
post_data.get('longitude'))

# Generate site code
site_code, catchments_data = generate_site_code(
location_site=None,
lat=post_data.get('latitude'),
lon=post_data.get('longitude'),
river_name=river_name
river_name=river_name,
user_site_code=site_code
)
post_data['legacy_site_code'] = post_data.get('site_code')
post_data['site_code'] = site_code
Expand All @@ -115,8 +150,5 @@ def post(self, request, *args, **kwargs):
self.send_location_site_email(location_site)

return Response(
{
'id': location_site.id,
'site_code': site_code
}
LocationSiteSerializer(location_site).data
)
73 changes: 71 additions & 2 deletions mobile/tests/test_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ def setUp(self) -> None:
)
self.api_url = reverse('mobile-add-location-site')
self.post_data = {
'owner': self.user.id,
'owner': str(self.user.id),
'date': int(datetime.now().timestamp()),
'latitude': 1,
'longitude': 1,
'ecosystem_type': 'river',
'river_name': 'RIVER',
'site_code': 'SITE_CODE',
'description': 'desc',
Expand Down Expand Up @@ -305,7 +306,8 @@ def test_update_location_site(self):
def test_add_location_site(self):
res = self.client.post(
self.api_url,
self.post_data
self.post_data,
format='json'
)
self.assertEqual(res.status_code, status.HTTP_200_OK)
location_site = LocationSite.objects.get(
Expand All @@ -315,6 +317,73 @@ def test_add_location_site(self):
location_site.additional_data.get('source_collection'),
'mobile'
)
self.assertEqual(
location_site.ecosystem_type,
'River'
)

@factory.django.mute_signals(signals.pre_save, signals.post_save)
def test_add_wetland_location_site(self):
self.post_data['ecosystem_type'] = 'wetland'
self.post_data['wetland_name'] = 'na me'
self.post_data['wetland_data'] = {
'wetlid': 'TEST',
'hgm_type': 'hydrogeomorphic',
'quaternary': 'quaternary'
}
res = self.client.post(
self.api_url,
data=self.post_data,
format='json'
)
self.assertEqual(res.status_code, status.HTTP_200_OK)
location_site = LocationSite.objects.get(
id=res.data['id']
)
self.assertEqual(
location_site.ecosystem_type,
'Wetland'
)
self.assertEqual(
location_site.hydrogeomorphic_type,
'hydrogeomorphic'
)
self.assertEqual(
location_site.wetland_id,
'TEST'
)
self.assertTrue(
location_site.site_code.startswith('quat-name')
)

@factory.django.mute_signals(signals.pre_save, signals.post_save)
def test_add_wetland_location_site_without_wetland_data(self):
self.post_data['ecosystem_type'] = 'wetland'
self.post_data['wetland_name'] = 'NAME'
res = self.client.post(
self.api_url,
data=self.post_data,
format='json'
)
self.assertEqual(res.status_code, status.HTTP_200_OK)
location_site = LocationSite.objects.get(
id=res.data['id']
)
self.assertEqual(
location_site.ecosystem_type,
'Wetland'
)
self.assertEqual(
location_site.hydrogeomorphic_type,
''
)
self.assertEqual(
location_site.wetland_id,
''
)
self.assertTrue(
location_site.site_code.startswith('-NAME-')
)


class TestRiverName(TestCase):
Expand Down
Loading