Skip to content
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
18 changes: 18 additions & 0 deletions django_project/cplus_api/migrations/0022_inputlayer_action.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
12 changes: 12 additions & 0 deletions django_project/cplus_api/models/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions django_project/cplus_api/serializers/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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'
]


Expand Down Expand Up @@ -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
Expand All @@ -502,5 +512,5 @@ class Meta:
model = InputLayer
fields = [
'name', 'layer_type', 'component_type', 'privacy_type',
'description', 'license', 'version'
'description', 'license', 'version', 'action'
]
11 changes: 7 additions & 4 deletions django_project/cplus_api/tests/test_sync_default_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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"
}
]
}
Expand Down
12 changes: 11 additions & 1 deletion django_project/cplus_api/utils/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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/"
Expand All @@ -320,6 +329,7 @@ def sync_nature_base():
"url": url
}
file.update(result)
file["action"] = action
ProcessFile(
storage,
owner,
Expand Down
Loading