Skip to content

Commit

Permalink
Add a view to enable testing of model in MVS from the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Bachibouzouk committed Oct 27, 2022
1 parent 98fb6c0 commit 4359b8b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
9 changes: 7 additions & 2 deletions app/projects/dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def __init__(


# Function to serialize scenario topology models to JSON
def convert_to_dto(scenario: Scenario):
def convert_to_dto(scenario: Scenario, testing: bool = False):
# Retrieve models
project = Project.objects.get(scenario=scenario)
economic_data = EconomicData.objects.get(project=project)
Expand Down Expand Up @@ -298,12 +298,17 @@ def convert_to_dto(scenario: Scenario):
# to_value_type(economic_data, 'crf'),
)

evaluated_period = to_value_type(scenario, "evaluated_period")
# For testing purposes the number of simulated days is restricted to 3 or less
if testing is True and evaluated_period.value > 3:
evaluated_period.value = 3

simulation_settings = SimulationSettingsDto(
scenario.start_date.strftime(
"%Y-%m-%d %H:%M"
), # datetime.combine(scenario.start_date, time()).timestamp(),
scenario.time_step,
to_value_type(scenario, "evaluated_period"),
evaluated_period,
)

# map_to_dto(economic_data, economic_data_dto)
Expand Down
4 changes: 2 additions & 2 deletions app/projects/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def empty(x):


# Helper to convert Scenario data to MVS importable json
def format_scenario_for_mvs(scenario_to_convert):
mvs_request_dto = convert_to_dto(scenario_to_convert)
def format_scenario_for_mvs(scenario_to_convert, testing=False):
mvs_request_dto = convert_to_dto(scenario_to_convert, testing=testing)
dumped_data = json.loads(
json.dumps(mvs_request_dto.__dict__, default=lambda o: o.__dict__)
)
Expand Down
5 changes: 5 additions & 0 deletions app/projects/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
view_mvs_data_input,
name="view_mvs_data_input",
),
path(
"test_mvs_data_input/<int:scen_id>",
test_mvs_data_input,
name="test_mvs_data_input",
),
path(
"topology/mvs_simulation/<int:scen_id>",
request_mvs_simulation,
Expand Down
12 changes: 10 additions & 2 deletions app/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ def asset_cops_create_or_update(
@json_view
@login_required
@require_http_methods(["GET"])
def view_mvs_data_input(request, scen_id=0):
def view_mvs_data_input(request, scen_id=0, testing=False):
if scen_id == 0:
return JsonResponse(
{"status": "error", "error": "No scenario id provided"},
Expand All @@ -1382,13 +1382,14 @@ def view_mvs_data_input(request, scen_id=0):
scenario = Scenario.objects.get(id=scen_id)

if scenario.project.user != request.user:

logger.warning(
f"Unauthorized user tried to access scenario with db id = {scen_id}."
)
raise PermissionDenied

try:
data_clean = format_scenario_for_mvs(scenario)
data_clean = format_scenario_for_mvs(scenario, testing)
except Exception as e:

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


@json_view
@login_required
@require_http_methods(["GET"])
def test_mvs_data_input(request, scen_id=0):
return view_mvs_data_input(request, scen_id=scen_id, testing=True)


# End-point to send MVS simulation request
# @json_view
@login_required
Expand Down

0 comments on commit 4359b8b

Please sign in to comment.