Skip to content

Commit cd154dc

Browse files
Merge pull request #34 from kartoza/feat-output-selection-detail
update scenario detail with selection outputs field and default values
2 parents 890c96a + 3baf7ea commit cd154dc

File tree

4 files changed

+145
-35
lines changed

4 files changed

+145
-35
lines changed

django_project/cplus_api/serializers/scenario.py

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from core.models.task_log import TaskLog
88
from cplus_api.models.layer import BaseLayer, InputLayer
99
from cplus_api.models.scenario import ScenarioTask
10+
from cplus_api.utils.default import DEFAULT_VALUES
1011

1112

1213
def validate_layer_uuid(value):
@@ -220,17 +221,24 @@ class Meta:
220221
class ScenarioInputSerializer(serializers.Serializer):
221222
scenario_name = serializers.CharField(required=True)
222223
scenario_desc = serializers.CharField(required=True)
223-
snapping_enabled = serializers.BooleanField(required=False)
224+
snapping_enabled = serializers.BooleanField(
225+
required=False, default=DEFAULT_VALUES.snapping_enabled)
224226
snap_layer = serializers.CharField(required=False, allow_blank=True)
225227
snap_layer_uuid = serializers.CharField(
226228
required=False, validators=[validate_layer_uuid], allow_blank=True
227229
)
228-
pathway_suitability_index = serializers.IntegerField(required=False)
229-
carbon_coefficient = serializers.FloatField(required=False)
230-
snap_rescale = serializers.BooleanField(required=False)
231-
snap_method = serializers.IntegerField(required=False)
232-
sieve_enabled = serializers.BooleanField(required=False)
233-
sieve_threshold = serializers.FloatField(required=False)
230+
pathway_suitability_index = serializers.IntegerField(
231+
required=False, default=DEFAULT_VALUES.pathway_suitability_index)
232+
carbon_coefficient = serializers.FloatField(
233+
required=False, default=DEFAULT_VALUES.carbon_coefficient)
234+
snap_rescale = serializers.BooleanField(
235+
required=False, default=DEFAULT_VALUES.snap_rescale)
236+
snap_method = serializers.IntegerField(
237+
required=False, default=DEFAULT_VALUES.snap_method)
238+
sieve_enabled = serializers.BooleanField(
239+
required=False, default=DEFAULT_VALUES.sieve_enabled)
240+
sieve_threshold = serializers.FloatField(
241+
required=False, default=DEFAULT_VALUES.sieve_threshold)
234242
sieve_mask_path = serializers.CharField(required=False, allow_blank=True)
235243
sieve_mask_uuid = serializers.CharField(
236244
required=False, validators=[validate_layer_uuid], allow_blank=True
@@ -251,6 +259,16 @@ class ScenarioInputSerializer(serializers.Serializer):
251259
priority_layers = PriorityLayerSerializer(many=True)
252260
priority_layer_groups = PriorityGroupSerializer(many=True)
253261
activities = ActivitySerializer(many=True)
262+
ncs_with_carbon = serializers.BooleanField(
263+
required=False, default=DEFAULT_VALUES.ncs_with_carbon)
264+
landuse_project = serializers.BooleanField(
265+
required=False, default=DEFAULT_VALUES.landuse_project)
266+
landuse_normalized = serializers.BooleanField(
267+
required=False, default=DEFAULT_VALUES.landuse_normalized)
268+
landuse_weighted = serializers.BooleanField(
269+
required=False, default=DEFAULT_VALUES.landuse_weighted)
270+
highest_position = serializers.BooleanField(
271+
required=False, default=DEFAULT_VALUES.highest_position)
254272

255273
class Meta:
256274
swagger_schema_fields = {
@@ -267,7 +285,8 @@ class Meta:
267285
),
268286
'snapping_enabled': openapi.Schema(
269287
title='Is snapping enabled',
270-
type=openapi.TYPE_BOOLEAN
288+
type=openapi.TYPE_BOOLEAN,
289+
default=DEFAULT_VALUES.snapping_enabled
271290
),
272291
'snap_layer': openapi.Schema(
273292
title='Snap layer Path',
@@ -279,27 +298,33 @@ class Meta:
279298
),
280299
'pathway_suitability_index': openapi.Schema(
281300
title='Pathway suitability index',
282-
type=openapi.TYPE_INTEGER
301+
type=openapi.TYPE_INTEGER,
302+
default=DEFAULT_VALUES.pathway_suitability_index
283303
),
284304
'carbon_coefficient': openapi.Schema(
285305
title='Carbon coefficient',
286-
type=openapi.TYPE_NUMBER
306+
type=openapi.TYPE_NUMBER,
307+
default=DEFAULT_VALUES.carbon_coefficient
287308
),
288309
'snap_rescale': openapi.Schema(
289310
title='Is snap rescale',
290-
type=openapi.TYPE_BOOLEAN
311+
type=openapi.TYPE_BOOLEAN,
312+
default=DEFAULT_VALUES.snap_rescale
291313
),
292314
'snap_method': openapi.Schema(
293315
title='Snap method',
294-
type=openapi.TYPE_INTEGER
316+
type=openapi.TYPE_INTEGER,
317+
default=DEFAULT_VALUES.snap_method
295318
),
296319
'sieve_enabled': openapi.Schema(
297320
title='Is sieve function enabled',
298-
type=openapi.TYPE_BOOLEAN
321+
type=openapi.TYPE_BOOLEAN,
322+
default=DEFAULT_VALUES.sieve_enabled
299323
),
300324
'sieve_threshold': openapi.Schema(
301325
title='Sieve function threshold',
302-
type=openapi.TYPE_NUMBER
326+
type=openapi.TYPE_NUMBER,
327+
default=DEFAULT_VALUES.sieve_threshold
303328
),
304329
'sieve_mask_path': openapi.Schema(
305330
title='Sieve mask layer path',
@@ -309,6 +334,31 @@ class Meta:
309334
title='Sieve mask layer UUID',
310335
type=openapi.TYPE_STRING
311336
),
337+
'ncs_with_carbon': openapi.Schema(
338+
title='Enable output NCS with carbon',
339+
type=openapi.TYPE_BOOLEAN,
340+
default=DEFAULT_VALUES.ncs_with_carbon
341+
),
342+
'landuse_project': openapi.Schema(
343+
title='Enable output Landuse Activity',
344+
type=openapi.TYPE_BOOLEAN,
345+
default=DEFAULT_VALUES.landuse_project
346+
),
347+
'landuse_normalized': openapi.Schema(
348+
title='Enable output Landuse Activity Normalized',
349+
type=openapi.TYPE_BOOLEAN,
350+
default=DEFAULT_VALUES.landuse_normalized
351+
),
352+
'landuse_weighted': openapi.Schema(
353+
title='Enable output Landuse Weighted with PWL',
354+
type=openapi.TYPE_BOOLEAN,
355+
default=DEFAULT_VALUES.landuse_weighted
356+
),
357+
'highest_position': openapi.Schema(
358+
title='Enable output Scenario Highest Position analysis',
359+
type=openapi.TYPE_BOOLEAN,
360+
default=DEFAULT_VALUES.highest_position
361+
),
312362
'mask_path': openapi.Schema(
313363
title='Mask layer path',
314364
type=openapi.TYPE_STRING

django_project/cplus_api/tasks/remove_layers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def remove_layers():
3737

3838
# Remove private data that is more 2 weeks
3939
output_group_to_keep = SitePreferences.preferences().output_group_to_keep
40-
output_layers = OutputLayer.objects.exclude(
40+
output_layers = OutputLayer.objects.filter(
41+
created_on__lt=last_14_days_datetime
42+
).exclude(
4143
Q(is_final_output=True) | Q(group__in=output_group_to_keep)
4244
)
4345
results[OutputLayer] = output_layers.count()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
class DEFAULT_VALUES(object):
3+
snapping_enabled = False
4+
pathway_suitability_index = 0
5+
carbon_coefficient = 0.0
6+
snap_rescale = False
7+
snap_method = 0
8+
sieve_enabled = False
9+
sieve_threshold = 10.0
10+
ncs_with_carbon = False
11+
landuse_project = True
12+
landuse_normalized = True
13+
landuse_weighted = True
14+
highest_position = True

django_project/cplus_api/utils/worker_analysis.py

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from cplus_api.models.scenario import ScenarioTask
2222
from cplus_api.models.layer import BaseLayer, OutputLayer, InputLayer
2323
from cplus_api.utils.api_helper import convert_size, todict, CustomJsonEncoder
24+
from cplus_api.utils.default import DEFAULT_VALUES
2425

2526
logger = logging.getLogger(__name__)
2627

@@ -34,15 +35,15 @@ class TaskConfig(object):
3435
priority_layers: typing.List = []
3536
priority_layer_groups: typing.List = []
3637
analysis_extent: SpatialExtent = None
37-
snapping_enabled: bool = False
38+
snapping_enabled: bool = DEFAULT_VALUES.snapping_enabled
3839
snap_layer = ''
3940
snap_layer_uuid = ''
40-
pathway_suitability_index = 0
41-
carbon_coefficient = 0.0
42-
snap_rescale = False
43-
snap_method = 0
44-
sieve_enabled = False
45-
sieve_threshold = 10.0
41+
pathway_suitability_index = DEFAULT_VALUES.pathway_suitability_index
42+
carbon_coefficient = DEFAULT_VALUES.carbon_coefficient
43+
snap_rescale = DEFAULT_VALUES.snap_rescale
44+
snap_method = DEFAULT_VALUES.snap_method
45+
sieve_enabled = DEFAULT_VALUES.sieve_enabled
46+
sieve_threshold = DEFAULT_VALUES.sieve_threshold
4647
sieve_mask_uuid = ''
4748
mask_path = ''
4849
mask_layers_paths = ''
@@ -52,16 +53,31 @@ class TaskConfig(object):
5253
carbon_uuid_layers = {}
5354
priority_uuid_layers = {}
5455
total_input_layers = 0
56+
# output selections
57+
ncs_with_carbon = DEFAULT_VALUES.ncs_with_carbon
58+
landuse_project = DEFAULT_VALUES.landuse_project
59+
landuse_normalized = DEFAULT_VALUES.landuse_normalized
60+
landuse_weighted = DEFAULT_VALUES.landuse_weighted
61+
highest_position = DEFAULT_VALUES.highest_position
5562

5663
def __init__(self, scenario_name, scenario_desc, extent,
5764
analysis_activities, priority_layers,
5865
priority_layer_groups,
5966
snapping_enabled=False, snap_layer_uuid='',
60-
pathway_suitability_index=0,
61-
carbon_coefficient=0.0, snap_rescale=False,
62-
snap_method=0, sieve_enabled=False,
63-
sieve_threshold=10.0, sieve_mask_uuid='',
64-
mask_layer_uuids='', scenario_uuid=None) -> None:
67+
pathway_suitability_index=
68+
DEFAULT_VALUES.pathway_suitability_index,
69+
carbon_coefficient=DEFAULT_VALUES.carbon_coefficient,
70+
snap_rescale=DEFAULT_VALUES.snap_rescale,
71+
snap_method=DEFAULT_VALUES.snap_method,
72+
sieve_enabled=DEFAULT_VALUES.sieve_enabled,
73+
sieve_threshold=DEFAULT_VALUES.sieve_threshold,
74+
sieve_mask_uuid='',
75+
mask_layer_uuids='', scenario_uuid=None,
76+
ncs_with_carbon=DEFAULT_VALUES.ncs_with_carbon,
77+
landuse_project=DEFAULT_VALUES.landuse_project,
78+
landuse_normalized=DEFAULT_VALUES.landuse_normalized,
79+
landuse_weighted=DEFAULT_VALUES.landuse_weighted,
80+
highest_position=DEFAULT_VALUES.highest_position) -> None:
6581
self.scenario_name = scenario_name
6682
self.scenario_desc = scenario_desc
6783
if scenario_uuid:
@@ -89,6 +105,12 @@ def __init__(self, scenario_name, scenario_desc, extent,
89105
weighted_activities=[],
90106
priority_layer_groups=self.priority_layer_groups
91107
)
108+
# output selections
109+
self.ncs_with_carbon = ncs_with_carbon
110+
self.landuse_project = landuse_project
111+
self.landuse_normalized = landuse_normalized
112+
self.landuse_weighted = landuse_weighted
113+
self.highest_position = highest_position
92114

93115
def get_activity(
94116
self, activity_uuid: str
@@ -137,7 +159,12 @@ def to_dict(self):
137159
'pathway_uuid_layers': self.pathway_uuid_layers,
138160
'carbon_uuid_layers': self.carbon_uuid_layers,
139161
'priority_uuid_layers': self.priority_uuid_layers,
140-
'total_input_layers': self.total_input_layers
162+
'total_input_layers': self.total_input_layers,
163+
'ncs_with_carbon': self.ncs_with_carbon,
164+
'landuse_project': self.landuse_project,
165+
'landuse_normalized': self.landuse_normalized,
166+
'landuse_weighted': self.landuse_weighted,
167+
'highest_position': self.highest_position
141168
}
142169
for activity in self.analysis_activities:
143170
activity_dict = {
@@ -171,17 +198,34 @@ def from_dict(cls, data: dict) -> typing.Self:
171198
)
172199
config.priority_layers = data.get('priority_layers', [])
173200
config.priority_layer_groups = data.get('priority_layer_groups', [])
174-
config.snapping_enabled = data.get('snapping_enabled', False)
201+
config.snapping_enabled = data.get(
202+
'snapping_enabled', DEFAULT_VALUES.snapping_enabled)
175203
config.snap_layer_uuid = data.get('snap_layer_uuid', '')
176204
config.pathway_suitability_index = data.get(
177-
'pathway_suitability_index', 0)
178-
config.carbon_coefficient = data.get('carbon_coefficient', 0.0)
179-
config.snap_rescale = data.get('snap_rescale', False)
180-
config.snap_method = data.get('snap_method', 0)
181-
config.sieve_enabled = data.get('sieve_enabled', False)
182-
config.sieve_threshold = data.get('sieve_threshold', 10.0)
205+
'pathway_suitability_index',
206+
DEFAULT_VALUES.pathway_suitability_index)
207+
config.carbon_coefficient = data.get(
208+
'carbon_coefficient', DEFAULT_VALUES.carbon_coefficient)
209+
config.snap_rescale = data.get(
210+
'snap_rescale', DEFAULT_VALUES.snap_rescale)
211+
config.snap_method = data.get(
212+
'snap_method', DEFAULT_VALUES.snap_method)
213+
config.sieve_enabled = data.get(
214+
'sieve_enabled', DEFAULT_VALUES.sieve_enabled)
215+
config.sieve_threshold = data.get(
216+
'sieve_threshold', DEFAULT_VALUES.sieve_threshold)
183217
config.sieve_mask_uuid = data.get('sieve_mask_uuid', '')
184218
config.mask_layer_uuids = data.get('mask_layer_uuids', '')
219+
config.ncs_with_carbon = data.get(
220+
'ncs_with_carbon', DEFAULT_VALUES.ncs_with_carbon)
221+
config.landuse_project = data.get(
222+
'landuse_project', DEFAULT_VALUES.landuse_project)
223+
config.landuse_normalized = data.get(
224+
'landuse_normalized', DEFAULT_VALUES.landuse_normalized)
225+
config.landuse_weighted = data.get(
226+
'landuse_weighted', DEFAULT_VALUES.landuse_weighted)
227+
config.highest_position = data.get(
228+
'highest_position', DEFAULT_VALUES.highest_position)
185229
# store dict of <layer_uuid, list of obj identifier>
186230
config.priority_uuid_layers = {}
187231
config.pathway_uuid_layers = {}

0 commit comments

Comments
 (0)