diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index dfedb17..9e88838 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -666,6 +666,17 @@ def assistantQueryFactory(): return Assistant.query.join(User).where(User.active == True) +def weighting_field(label: str, default: float): + return DecimalField( + label, + validators=[DataRequired(), NumberRange(0, 1)], + default=default, + description="Between 0 and 1.", + places=2, + widget=NumberInput(step=0.01), + ) + + class SemesterExperimentView(SecureAdminModelView): class ProgramFilter(ProgramRowFilter): def apply(self, query, value, alias=None): @@ -687,30 +698,9 @@ class SemesterExperimentView(SecureAdminModelView): blank_text="-", ) - oral_weighting = DecimalField( - "Oral weighting", - validators=[DataRequired(), NumberRange(0, 1)], - default=0.5, - description="Between 0 and 1.", - places=2, - widget=NumberInput(step=0.01), - ) - protocol_weighting = DecimalField( - "Protocol weighting", - validators=[DataRequired(), NumberRange(0, 1)], - default=0.5, - description="Between 0 and 1. Oral and protocol weightings have to add to 1! Both are rounded to 2 decimal digits.", - places=2, - widget=NumberInput(step=0.01), - ) - final_weighting = DecimalField( - "Final weighting", - validators=[DataRequired(), NumberRange(0, 1)], - default=1.0, - description="Between 0 and 1.", - places=2, - widget=NumberInput(step=0.01), - ) + oral_weighting = weighting_field("Oral weighting", 0.5) + protocol_weighting = weighting_field("Protocol weighting", 0.5) + final_weighting = weighting_field("Final weighting", 1.0) assistants = QuerySelectMultipleField( "Assistants", @@ -880,9 +870,6 @@ class AdminView(SecureAdminModelView): ] -assistantBlankText = "Auto assign if experiment has only one assistant" - - class ExperimentRowFilter(FilterEqual): def get_options(self, view): if not has_request_context(): @@ -913,6 +900,41 @@ def group_experiment_note_field(): ) +def appointment_fields(number=None): + if number is None: + # Used in AppointmentView + label_addition = "" + date_validator = DataRequired() + date_description = None + else: + # Used in GroupExperimentView + label_addition = f"Appointment-{number} " + date_validator = Optional() + + if number == 1: + date_description = "Set if you already want to add an appointment. Otherwise, leave it blank and you can do it later under the Appointment tab." + else: + date_description = "Add another appointment (see above)." + + date = DateField( + label_addition + "Date", + validators=[date_validator], + description=date_description, + ) + special = BooleanField( + label_addition + "Special", + default=False, + description="A special appointment should take place in the semester break.", + ) + assistant = QuerySelectField( + label_addition + "Assistant", + query_factory=assistantQueryFactory, + allow_blank=True, + blank_text="Auto assign if experiment has only one assistant", + ) + return date, special, assistant + + class GroupExperimentView(SecureAdminModelView): class ExperimentFilter(ExperimentRowFilter): def apply(self, query, value, alias=None): @@ -951,37 +973,9 @@ class GroupExperimentView(SecureAdminModelView): blank_text="-", ) - appointment1_date = DateField( - "Appointment-1 Date", - validators=[Optional()], - description="Set if you already want to add an appointment. Otherwise, leave it blank and you can do it later under the Appointment tab.", - ) - appointment1_special = BooleanField( - "Appointment-1 Special", - default=False, - ) - appointment1_assistant = QuerySelectField( - "Appointment-1 Assistant", - query_factory=assistantQueryFactory, - allow_blank=True, - blank_text=assistantBlankText, - ) + appointment1_date, appointment1_special, appointment1_assistant = appointment_fields(1) + appointment2_date, appointment2_special, appointment2_assistant = appointment_fields(2) - appointment2_date = DateField( - "Appointment-2 Date", - validators=[Optional()], - description="Add a second appointment (see above).", - ) - appointment2_special = BooleanField( - "Appointment-2 Special", - default=False, - ) - appointment2_assistant = QuerySelectField( - "Appointment-2 Assistant", - query_factory=assistantQueryFactory, - allow_blank=True, - blank_text=assistantBlankText, - ) note = group_experiment_note_field() column_display_all_relations = True @@ -1077,21 +1071,7 @@ class AppointmentView(SecureAdminModelView): allow_blank=True, blank_text="-", ) - date = DateField( - "Date", - validators=[DataRequired()], - ) - special = BooleanField( - "Special", - default=False, - description="A special appointment should take place in the semester break.", - ) - assistant = QuerySelectField( - "Assistant", - query_factory=assistantQueryFactory, - allow_blank=True, - blank_text=assistantBlankText, - ) + date, special, assistant = appointment_fields() column_descriptions = { "special": "A special appointment should take place in the semester break",