From d4b75dd4fb1876c6e062772a7eb0c4a6753d08a2 Mon Sep 17 00:00:00 2001 From: Dimas Date: Sat, 16 Sep 2023 20:48:28 +0700 Subject: [PATCH] Handle wetland site from mobile --- bims/static/js/wetland_site.js | 2 - bims/views/location_site.py | 2 +- mobile/api_views/add_location_site.py | 54 ++++++++++++++++---- mobile/tests/test_api_views.py | 73 ++++++++++++++++++++++++++- 4 files changed, 115 insertions(+), 16 deletions(-) diff --git a/bims/static/js/wetland_site.js b/bims/static/js/wetland_site.js index b16b47464..a1a679fd6 100644 --- a/bims/static/js/wetland_site.js +++ b/bims/static/js/wetland_site.js @@ -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; diff --git a/bims/views/location_site.py b/bims/views/location_site.py index 38579f394..a1d65c871 100644 --- a/bims/views/location_site.py +++ b/bims/views/location_site.py @@ -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', '') diff --git a/mobile/api_views/add_location_site.py b/mobile/api_views/add_location_site.py index b43a40be7..ff7ab6e05 100644 --- a/mobile/api_views/add_location_site.py +++ b/mobile/api_views/add_location_site.py @@ -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): @@ -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( @@ -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 @@ -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 ) diff --git a/mobile/tests/test_api_views.py b/mobile/tests/test_api_views.py index 18418dacc..c5cbdaaf5 100644 --- a/mobile/tests/test_api_views.py +++ b/mobile/tests/test_api_views.py @@ -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', @@ -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( @@ -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):