Skip to content

Commit

Permalink
Merge pull request #1071 from TOMToolkit/1070-facility-method-for-cho…
Browse files Browse the repository at this point in the history
…osing-what-facility-forms-to-display

add function to retrieve facility form classes for display
  • Loading branch information
jchate6 authored Oct 14, 2024
2 parents 5b26c54 + b1ef668 commit 18b460b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tom_observations/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def create(self, request, *args, **kwargs):
observation_ids = []
try:
facility = get_service_class(self.request.data['facility'])()
observation_form_class = facility.observation_forms[self.request.data['observation_type']]
observation_form_class = facility.get_form_classes_for_display()[self.request.data['observation_type']]
target = Target.objects.get(pk=self.request.data['target_id'])
observing_parameters = self.request.data['observing_parameters']
except KeyError as ke:
Expand Down
11 changes: 11 additions & 0 deletions tom_observations/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class BaseObservationFacility(ABC):
the other BaseObservationFacilities.
"""
name = 'BaseObservation'
observation_forms = {}

def __init__(self):
self.user = None
Expand Down Expand Up @@ -227,6 +228,16 @@ def get_form(self, observation_type):
This method takes in an observation type and returns the form type that matches it.
"""

def get_form_classes_for_display(self, **kwargs):
"""
This method returns a dictionary of the format:
{'OBSERVATION_TYPE': FacilityFormClass}
Typically this will be all or a subset of the forms in `self.observation_forms`
"""
return self.observation_forms

def get_facility_context_data(self, **kwargs):
"""
This method provides an opportunity for the Facility subclass to add additional
Expand Down
3 changes: 2 additions & 1 deletion tom_observations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def get_context_data(self, **kwargs):
# reloaded due to form errors, only repopulate the form that was submitted.
observation_type_choices = []
initial = self.get_initial()
for observation_type, observation_form_class in self.get_facility_class().observation_forms.items():
observation_form_classes = self.get_facility_class()().get_form_classes_for_display(**kwargs)
for observation_type, observation_form_class in observation_form_classes.items():
form_data = {**initial, **{'observation_type': observation_type}}
# Repopulate the appropriate form with form data if the original submission was invalid
if observation_type == self.request.POST.get('observation_type'):
Expand Down

0 comments on commit 18b460b

Please sign in to comment.