diff --git a/django_project/cplus_api/migrations/0022_inputlayer_action.py b/django_project/cplus_api/migrations/0022_inputlayer_action.py new file mode 100644 index 0000000..f8d2deb --- /dev/null +++ b/django_project/cplus_api/migrations/0022_inputlayer_action.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2025-12-17 17:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cplus_api', '0021_alter_inputlayer_component_type'), + ] + + operations = [ + migrations.AddField( + model_name='inputlayer', + name='action', + field=models.IntegerField(choices=[(0, 'Protect'), (1, 'Restore'), (2, 'Manage'), (-1, 'Undefined')], default=-1), + ), + ] diff --git a/django_project/cplus_api/models/layer.py b/django_project/cplus_api/models/layer.py index 5a1515a..eded913 100644 --- a/django_project/cplus_api/models/layer.py +++ b/django_project/cplus_api/models/layer.py @@ -117,6 +117,13 @@ class LayerSources(models.TextChoices): CPLUS = 'cplus', _('CPLUS') NATURE_BASE = 'naturebase', _('Naturebase') + class PathwayTypes(models.IntegerChoices): + PROTECT = 0, _('Protect') + RESTORE = 1, _('Restore') + MANAGE = 2, _('Manage') + UNDEFINED = -1, _('Undefined') + + file = models.FileField( upload_to=input_layer_dir_path, storage=select_input_layer_storage @@ -157,6 +164,11 @@ class LayerSources(models.TextChoices): default='' ) + action = models.IntegerField( + choices=PathwayTypes.choices, + default=PathwayTypes.UNDEFINED + ) + source = models.CharField( max_length=50, choices=LayerSources.choices, diff --git a/django_project/cplus_api/serializers/layer.py b/django_project/cplus_api/serializers/layer.py index ba30b18..73873cf 100644 --- a/django_project/cplus_api/serializers/layer.py +++ b/django_project/cplus_api/serializers/layer.py @@ -86,6 +86,14 @@ InputLayer.LayerSources.NATURE_BASE ] ), + 'action': openapi.Schema( + title='''Action either Protect (0), Restore (1), Manage (2), + Undefined (-1)''', + type=openapi.TYPE_INTEGER, + enum=[ + InputLayer.PathwayTypes.PROTECT + ] + ), }, 'required': [ 'filename', 'size', 'uuid', 'layer_type', @@ -105,7 +113,8 @@ 'client_id': '', 'license': 'CC BY 4.0', 'version': '1.0.0', - 'source': InputLayer.LayerSources.NATURE_BASE + 'source': InputLayer.LayerSources.NATURE_BASE, + 'action': InputLayer.PathwayTypes.PROTECT } } @@ -213,7 +222,7 @@ class Meta: 'created_by', 'layer_type', 'size', 'url', 'component_type', 'privacy_type', 'client_id', 'metadata', 'description', - 'license', 'version', 'source' + 'license', 'version', 'source', 'action' ] @@ -475,7 +484,8 @@ class Meta: 'privacy_type': 'common', 'client_id': '', 'license': 'CC BY 4.0', - 'version': '1.0.0' + 'version': '1.0.0', + 'action': 1 } }) # Remove filename from the schema and rename it to name @@ -502,5 +512,5 @@ class Meta: model = InputLayer fields = [ 'name', 'layer_type', 'component_type', 'privacy_type', - 'description', 'license', 'version' + 'description', 'license', 'version', 'action' ] diff --git a/django_project/cplus_api/tests/test_sync_default_layers.py b/django_project/cplus_api/tests/test_sync_default_layers.py index 802d86e..16760f8 100644 --- a/django_project/cplus_api/tests/test_sync_default_layers.py +++ b/django_project/cplus_api/tests/test_sync_default_layers.py @@ -286,7 +286,7 @@ def test_nature_base_new_layer(self, mock_sync_cplus_layers): """ nature_base_url = ( "https://content.ncsmap.org/items/spatial_metadata?limit=-1&sort=title&filter[status][_in]=published&" # noqa - "fields=id,title,short_summary,download_links,cog_url,date_updated" + "fields=id,title,short_summary,download_links,cog_url,date_updated,action" # noqa ) with requests_mock.Mocker() as rm: rm.get( @@ -304,7 +304,8 @@ def test_nature_base_new_layer(self, mock_sync_cplus_layers): } ], "cog_url": "https://kartoza.com/total_ncs.tif", - "date_updated": "2024-08-28T18:59:51.419Z" + "date_updated": "2024-08-28T18:59:51.419Z", + "action": "undefined" }, { "id": 15, @@ -317,7 +318,8 @@ def test_nature_base_new_layer(self, mock_sync_cplus_layers): } ], "cog_url": "https://kartoza.com/test_pathway_naturebase.tif", # noqa - "date_updated": "2024-08-28T18:55:55.936Z" + "date_updated": "2024-08-28T18:55:55.936Z", + "action": "protect" }, { "id": 5, @@ -330,7 +332,8 @@ def test_nature_base_new_layer(self, mock_sync_cplus_layers): } ], "cog_url": None, - "date_updated": "2024-08-28T18:57:00.867Z" + "date_updated": "2024-08-28T18:57:00.867Z", + "action": "protect" } ] } diff --git a/django_project/cplus_api/utils/layers.py b/django_project/cplus_api/utils/layers.py index 40202b1..be822c1 100644 --- a/django_project/cplus_api/utils/layers.py +++ b/django_project/cplus_api/utils/layers.py @@ -177,6 +177,7 @@ def read_metadata( layer ) self.input_layer.source = self.source + self.input_layer.action = self.file.get('action', -1) self.input_layer.save() def handle_nature_base(self, file_path): @@ -298,7 +299,7 @@ def sync_nature_base(): url = ( "https://content.ncsmap.org/items/spatial_metadata?limit=-1&sort=" "title&filter[status][_in]=published&fields=id,title,short_summary," - "download_links,cog_url,date_updated" + "download_links,cog_url,date_updated,action" ) response = requests.get(url) @@ -310,6 +311,14 @@ def sync_nature_base(): result['date_updated'] ) url = result['cog_url'] or result['download_links'][0]['url'] + action = -1 + if result.get('action') == "protect": + action = 0 + elif result.get('action') == "restore": + action = 1 + elif result.get('action') == "manage": + action = 2 + file = { "Key": ( f"common_layers/ncs_pathway/" @@ -320,6 +329,7 @@ def sync_nature_base(): "url": url } file.update(result) + file["action"] = action ProcessFile( storage, owner,