Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed cascade logic for dropdown options filtering #30

Merged
merged 4 commits into from
Jul 30, 2024
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
29 changes: 16 additions & 13 deletions src/evagram/website/backend/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def test_initial_load(self):
response = self.client.get("/api/initial-load/")
self.assertEqual(200, response.status_code)
self.assertTrue("owners" in response.json())
self.assertTrue("experiments" in response.json())
self.assertTrue("groups" in response.json())
self.assertTrue("observations" in response.json())
self.assertTrue("variables" in response.json())

def test_get_single_plot(self):
response = self.client.get(
Expand Down Expand Up @@ -140,16 +136,18 @@ def test_update_experiment_option_insufficient_params(self):
self.assertEqual(400, response.status_code)

def test_update_observation_option_valid_params(self):
response = self.client.get("/api/update-observation-option/?observation_id=1")
self.assertEqual("observation_id=1", response.request['QUERY_STRING'])
response = self.client.get("/api/update-observation-option/"
"?experiment_id=12&observation_id=1")
self.assertEqual("experiment_id=12&observation_id=1", response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)
self.assertTrue("groups" in response.json())
self.assertTrue("variables" in response.json())
self.assertTrue("variablesMap" in response.json())

def test_update_observation_option_object_not_found(self):
response = self.client.get("/api/update-observation-option/?observation_id=-1")
self.assertEqual("observation_id=-1", response.request['QUERY_STRING'])
response = self.client.get("/api/update-observation-option/"
"?experiment_id=1&observation_id=-1")
self.assertEqual("experiment_id=1&observation_id=-1", response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_update_observation_option_value_error(self):
Expand All @@ -164,17 +162,22 @@ def test_update_observation_option_insufficient_params(self):

def test_update_variable_option_valid_params(self):
response = self.client.get(
"/api/update-variable-option/?variable_name=brightnessTemperature&channel=4")
self.assertEqual("variable_name=brightnessTemperature"
"&channel=4", response.request['QUERY_STRING'])
"/api/update-variable-option/?experiment_id=12&observation_id=1&"
"variable_name=brightnessTemperature&channel=4")
self.assertEqual("experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=4",
response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)
self.assertTrue("groups" in response.json())
self.assertTrue("channel" in response.json())

def test_update_variable_option_object_not_found(self):
response = self.client.get(
"/api/update-variable-option/?variable_name=variable&channel=null")
self.assertEqual("variable_name=variable&channel=null", response.request['QUERY_STRING'])
"/api/update-variable-option/?experiment_id=12&observation_id=1&"
"variable_name=variable&channel=null")
self.assertEqual("experiment_id=12&observation_id=1&"
"variable_name=variable&channel=null",
response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_update_variable_option_insufficient_params(self):
Expand Down
65 changes: 20 additions & 45 deletions src/evagram/website/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,9 @@
@api_view(['GET'])
def initial_load(request):
data = {
"owners": [],
"experiments": [],
"groups": [],
"observations": [],
"variables": []
"owners": []
}
data["owners"] = get_owners()
if len(data["owners"]) > 0:
first_owner = data["owners"][0]["owner_id"]
data["experiments"] = get_experiments_by_owner(first_owner)
if len(data["experiments"]) > 0:
first_experiment = data["experiments"][0]["experiment_id"]
data["observations"] = get_observations_by_experiment(first_experiment)
if len(data["observations"]) > 0:
first_observation = data["observations"][0]["observation_id"]
data["variables"] = get_variables_by_observation(first_observation)
if len(data["variables"]) > 0:
first_variable = data["variables"][0]["variable_id"]
data["groups"] = get_groups_by_variable(first_variable)
return Response(data)


Expand Down Expand Up @@ -66,12 +50,12 @@ def get_plots_by_field(request):
assert current_experiment in experiments, error_msg
# get plots by experiment field
if observation_id == "null":
plots = Plots.objects.filter(experiment_id=experiment_id)
plots = Plots.objects.filter(experiment=experiment_id)

# get plots by observation field
elif variable_name == "null":
plots = Plots.objects.filter(experiment_id=experiment_id,
observation_id=observation_id)
plots = Plots.objects.filter(experiment=experiment_id,
observation=observation_id)

# get plots by variable field
elif group_id == "null":
Expand All @@ -81,9 +65,9 @@ def get_plots_by_field(request):
variable_id = Variables.objects.get(
variable_name=variable_name, channel=channel).variable_id
plots = Plots.objects.filter(
experiment_id=experiment_id,
observation_id=observation_id,
variable_id=variable_id)
experiment=experiment_id,
observation=observation_id,
variable=variable_id)

elif group_id != "":
if channel == "null":
Expand All @@ -92,7 +76,7 @@ def get_plots_by_field(request):
plots = Plots.objects.filter(experiment=experiment_id,
group=group_id,
observation=observation_id,
variable_id=variable_id)
variable=variable_id)

serializer = PlotSerializer(plots, many=True)
return Response(serializer.data)
Expand Down Expand Up @@ -123,14 +107,6 @@ def update_user_option(request):
"groups": []
}
data["experiments"] = get_experiments_by_owner(owner_id)
if len(data["experiments"]) > 0:
data["observations"] = get_observations_by_experiment(
data["experiments"][0]["experiment_id"])
if len(data["observations"]) > 0:
data["variables"] = get_variables_by_observation(
data["observations"][0]["observation_id"])
if len(data["variables"]) > 0:
data["groups"] = get_groups_by_variable(data["variables"][0]["variable_id"])
return Response(data)

except ValueError as e:
Expand All @@ -155,11 +131,6 @@ def update_experiment_option(request):
"groups": []
}
data["observations"] = get_observations_by_experiment(experiment_id)
if len(data["observations"]) > 0:
data["variables"] = get_variables_by_observation(
data["observations"][0]["observation_id"])
if len(data["variables"]) > 0:
data["groups"] = get_groups_by_variable(data["variables"][0]["variable_id"])
return Response(data)

except ValueError as e:
Expand All @@ -176,21 +147,20 @@ def update_experiment_option(request):
@api_view(['GET'])
def update_observation_option(request):
try:
experiment_id = request.GET["experiment_id"]
observation_id = request.GET["observation_id"]
Observations.objects.get(pk=observation_id)
data = {
"variables": [],
"groups": [],
"variablesMap": {}
}
data["variables"] = get_variables_by_observation(observation_id)
data["variables"] = get_variables_by_observation(experiment_id, observation_id)
variablesMap = {}
for variable in data["variables"]:
variablesMap[variable['variable_name']] = variablesMap.get(
variable['variable_name'], []) + [variable['channel']]
data["variablesMap"] = variablesMap
if len(data["variables"]) > 0:
data["groups"] = get_groups_by_variable(data["variables"][0]["variable_id"])
return Response(data)

except ValueError as e:
Expand All @@ -207,6 +177,8 @@ def update_observation_option(request):
@api_view(['GET'])
def update_variable_option(request):
try:
experiment_id = request.GET["experiment_id"]
observation_id = request.GET["observation_id"]
variable_name = request.GET["variable_name"]
channel = request.GET["channel"]
variable_id = None
Expand All @@ -232,7 +204,7 @@ def update_variable_option(request):
"groups": [],
"channel": channel
}
data["groups"] = get_groups_by_variable(variable_id)
data["groups"] = get_groups_by_variable(experiment_id, observation_id, variable_id)
return Response(data)

except AssertionError as e:
Expand Down Expand Up @@ -269,15 +241,18 @@ def get_observations_by_experiment(pk_experiment):
return serializer.data


def get_variables_by_observation(pk_observation):
queryset = Plots.objects.filter(observation_id=pk_observation).values_list("variable_id")
def get_variables_by_observation(pk_experiment, pk_observation):
queryset = Plots.objects.filter(experiment=pk_experiment,
observation=pk_observation).values_list("variable_id")
variables = Variables.objects.filter(variable_id__in=queryset)
serializer = VariableSerializer(variables, many=True)
return serializer.data


def get_groups_by_variable(pk_variable):
queryset = Plots.objects.filter(variable_id=pk_variable).values_list("group_id")
def get_groups_by_variable(pk_experiment, pk_observation, pk_variable):
queryset = Plots.objects.filter(experiment=pk_experiment,
observation=pk_observation,
variable=pk_variable).values_list("group_id")
groups = Groups.objects.filter(group_id__in=queryset)
serializer = GroupSerializer(groups, many=True)
return serializer.data
Loading
Loading