diff options
Diffstat (limited to 'archaeological_files/forms.py')
-rw-r--r-- | archaeological_files/forms.py | 79 |
1 files changed, 62 insertions, 17 deletions
diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py index 4918fdaaa..77d19ce2f 100644 --- a/archaeological_files/forms.py +++ b/archaeological_files/forms.py @@ -624,10 +624,19 @@ INLINE_JOB_FIELDS = [ "days_worked", ] JOB_WIDGETS = { - "man_by_day_planned": forms.NumberInput(attrs={"class": "w-50 form-planned"}), - "days_planned": forms.NumberInput(attrs={"class": "w-50 form-planned"}), - "man_by_day_worked": forms.NumberInput(attrs={"class": "w-50 form-worked"}), - "days_worked": forms.NumberInput(attrs={"class": "w-50 form-worked"}), + "job": forms.Select(attrs={"bs_col_width": "col-lg-6 col-12"}), + "man_by_day_planned": forms.NumberInput( + attrs={"class": "w-50 form-planned", "bs_col_width": "col-2"} + ), + "days_planned": forms.NumberInput( + attrs={"class": "w-50 form-planned", "bs_col_width": "col-2"} + ), + "man_by_day_worked": forms.NumberInput( + attrs={"class": "w-50 form-worked", "bs_col_width": "col-2"} + ), + "days_worked": forms.NumberInput( + attrs={"class": "w-50 form-worked", "bs_col_width": "col-2"} + ), } JOB_LABELS = { @@ -653,7 +662,30 @@ class PreventiveFileForm(forms.ModelForm): return super().save(commit=commit) -class PreventiveFileJobForm(PreventiveFileForm): +class PreventiveFileGenJobForm(PreventiveFileForm): + def __init__(self, *args, **kwargs): + super(PreventiveFileGenJobForm, self).__init__(*args, **kwargs) + current_value = None + if hasattr(self.instance, "job") and self.instance.job: + current_value = self.instance.job + self.fields["job"].choices = models.Job.get_choices(current_value) + + def save(self, commit=True): + item = super().save(commit=True) + child = item.job.child + if not item or not child: + return + if not self._meta.model.objects.filter(file_id=item.file_id, job=child).count(): + self._meta.model.objects.create( + file_id=item.file_id, + job=child, + man_by_day_planned=item.man_by_day_planned, + days_planned=item.days_planned, + ) + return item + + +class PreventiveFileJobForm(PreventiveFileGenJobForm): class Meta: model = models.PreventiveFileJob fields = ["job"] + INLINE_JOB_FIELDS @@ -675,7 +707,7 @@ PreventiveFileJobFormSet.form_slug = "preventive-030-post-excavation" PreventiveFileJobFormSet.dynamic_add = True -class PreventiveFileGroundJobForm(PreventiveFileForm): +class PreventiveFileGroundJobForm(PreventiveFileGenJobForm): class Meta: model = models.PreventiveFileGroundJob fields = ["job"] + INLINE_JOB_FIELDS @@ -715,12 +747,20 @@ INLINE_COST_FIELDS = [ ] COST_WIDGETS = { - "quantity_by_day_planned": forms.NumberInput(attrs={"class": "w-50 form-planned"}), - "days_planned": forms.NumberInput(attrs={"class": "w-50 form-planned unit-form"}), - "quantity_by_day_worked": forms.NumberInput(attrs={"class": "w-50 form-worked"}), - "days_worked": forms.NumberInput(attrs={"class": "w-50 form-worked unit-form"}), + "quantity_by_day_planned": forms.NumberInput( + attrs={"class": "w-50 form-planned", "bs_col_width": "col-2"} + ), + "days_planned": forms.NumberInput( + attrs={"class": "w-50 form-planned unit-form", "bs_col_width": "col-2"} + ), + "quantity_by_day_worked": forms.NumberInput( + attrs={"class": "w-50 form-worked", "bs_col_width": "col-2"} + ), + "days_worked": forms.NumberInput( + attrs={"class": "w-50 form-worked unit-form", "bs_col_width": "col-2"} + ), "equipment_service_cost": forms.Select( - attrs={"class": "form-cost", "bs_col_width": "col-12"} + attrs={"class": "form-cost", "bs_col_width": "col-lg-6 col-12"} ), } @@ -744,10 +784,13 @@ class PreventiveFileEquipmentServiceForm(PreventiveFileForm): self.unities = {} unit_dict = dict(models.ES_UNITS) - choices = [("", "--")] + choices = [("", "-" * 9)] costs = list(q.all()) - if self.instance and self.instance.equipment_service_cost_id \ - and self.instance.equipment_service_cost not in costs: + if ( + self.instance + and self.instance.equipment_service_cost_id + and self.instance.equipment_service_cost not in costs + ): costs.append(self.instance.equipment_service_cost) self.flat_rates = [] for cost in costs: @@ -767,10 +810,12 @@ class PreventiveFileEquipmentServiceForm(PreventiveFileForm): return for child in item.equipment_service_cost.equipment_service_type.children.all(): if not self._meta.model.objects.filter( - file_id=item.file_id, equipment_service_cost=child).count(): + file_id=item.file_id, equipment_service_cost=child + ).count(): self._meta.model.objects.create( - file_id=item.file_id, equipment_service_cost=child, - quantity_by_day_planned=0 + file_id=item.file_id, + equipment_service_cost=child, + quantity_by_day_planned=0, ) return item |