Skip to content

Commit 4359b8b

Browse files
committed
Add a view to enable testing of model in MVS from the DB
1 parent 98fb6c0 commit 4359b8b

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

app/projects/dtos.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def __init__(
257257

258258

259259
# Function to serialize scenario topology models to JSON
260-
def convert_to_dto(scenario: Scenario):
260+
def convert_to_dto(scenario: Scenario, testing: bool = False):
261261
# Retrieve models
262262
project = Project.objects.get(scenario=scenario)
263263
economic_data = EconomicData.objects.get(project=project)
@@ -298,12 +298,17 @@ def convert_to_dto(scenario: Scenario):
298298
# to_value_type(economic_data, 'crf'),
299299
)
300300

301+
evaluated_period = to_value_type(scenario, "evaluated_period")
302+
# For testing purposes the number of simulated days is restricted to 3 or less
303+
if testing is True and evaluated_period.value > 3:
304+
evaluated_period.value = 3
305+
301306
simulation_settings = SimulationSettingsDto(
302307
scenario.start_date.strftime(
303308
"%Y-%m-%d %H:%M"
304309
), # datetime.combine(scenario.start_date, time()).timestamp(),
305310
scenario.time_step,
306-
to_value_type(scenario, "evaluated_period"),
311+
evaluated_period,
307312
)
308313

309314
# map_to_dto(economic_data, economic_data_dto)

app/projects/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def empty(x):
5151

5252

5353
# Helper to convert Scenario data to MVS importable json
54-
def format_scenario_for_mvs(scenario_to_convert):
55-
mvs_request_dto = convert_to_dto(scenario_to_convert)
54+
def format_scenario_for_mvs(scenario_to_convert, testing=False):
55+
mvs_request_dto = convert_to_dto(scenario_to_convert, testing=testing)
5656
dumped_data = json.loads(
5757
json.dumps(mvs_request_dto.__dict__, default=lambda o: o.__dict__)
5858
)

app/projects/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@
157157
view_mvs_data_input,
158158
name="view_mvs_data_input",
159159
),
160+
path(
161+
"test_mvs_data_input/<int:scen_id>",
162+
test_mvs_data_input,
163+
name="test_mvs_data_input",
164+
),
160165
path(
161166
"topology/mvs_simulation/<int:scen_id>",
162167
request_mvs_simulation,

app/projects/views.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ def asset_cops_create_or_update(
13711371
@json_view
13721372
@login_required
13731373
@require_http_methods(["GET"])
1374-
def view_mvs_data_input(request, scen_id=0):
1374+
def view_mvs_data_input(request, scen_id=0, testing=False):
13751375
if scen_id == 0:
13761376
return JsonResponse(
13771377
{"status": "error", "error": "No scenario id provided"},
@@ -1382,13 +1382,14 @@ def view_mvs_data_input(request, scen_id=0):
13821382
scenario = Scenario.objects.get(id=scen_id)
13831383

13841384
if scenario.project.user != request.user:
1385+
13851386
logger.warning(
13861387
f"Unauthorized user tried to access scenario with db id = {scen_id}."
13871388
)
13881389
raise PermissionDenied
13891390

13901391
try:
1391-
data_clean = format_scenario_for_mvs(scenario)
1392+
data_clean = format_scenario_for_mvs(scenario, testing)
13921393
except Exception as e:
13931394

13941395
logger.error(
@@ -1403,6 +1404,13 @@ def view_mvs_data_input(request, scen_id=0):
14031404
return JsonResponse(data_clean, status=200, content_type="application/json")
14041405

14051406

1407+
@json_view
1408+
@login_required
1409+
@require_http_methods(["GET"])
1410+
def test_mvs_data_input(request, scen_id=0):
1411+
return view_mvs_data_input(request, scen_id=scen_id, testing=True)
1412+
1413+
14061414
# End-point to send MVS simulation request
14071415
# @json_view
14081416
@login_required

0 commit comments

Comments
 (0)