Source code for ferrosoft.apps.emiflow.forms.treatment

#  Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
from django import forms
from django.forms.models import ModelForm
from django.utils.translation import gettext_lazy as _

from ferrosoft.apps.emiflow.forms.mixins import CopyEnergyCarrierFromEmitter
from ferrosoft.apps.emiflow.models import (
    Treatment,
    TreatmentOperation,
    Emitter,
    EmitterEpitype,
    IntensityCapableEntity,
    TreatmentOperationInputLine,
    TreatmentLine,
    TreatmentOperationOutputLine,
    LifecycleCategory,
)
from ferrosoft.apps.emiflow.models.treatment import (
    TreatmentInputLine,
    TreatmentOperationStatus,
    TreatmentOutputLine,
    TreatmentLCCValue,
)
from ferrosoft.apps.ferrobase.forms import UnitChoiceField
from ferrosoft.apps.ferrobase.models import (
    MeasurementUnit,
    UnitCollection,
)


[docs] class TreatmentForm(ModelForm):
[docs] class Meta: model = Treatment fields = [ "reference", "booking_date", "operation", ] widgets = { "booking_date": forms.DateInput( attrs={"type": "date"}, format="%Y-%m-%d", ), }
operation = forms.ModelChoiceField( label=_("Treatment Operation"), queryset=TreatmentOperation.objects.filter( status=TreatmentOperationStatus.CERTIFIED ), )
[docs] def save(self, commit=True): instance = super().save(commit) if commit: instance.copy_lines_from_operation() return instance
[docs] class TreatmentOperationForm(CopyEnergyCarrierFromEmitter, ModelForm):
[docs] class Meta: model = TreatmentOperation fields = [ "reference", "name", "emitter", "energy_carrier", ]
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["emitter"].queryset = Emitter.objects.filter( epitype__in=EmitterEpitype.hub_equipment(), ) # Override energy_carrier required attribute, to be able # to copy energy carrier from selected emitter. Require # energy carrier selectively in clean method (see mixin). self.fields["energy_carrier"].required = False if isinstance(self.instance, TreatmentOperation) and self.instance.is_status( TreatmentOperationStatus.CERTIFIED ): for field in self.fields.values(): # Certified treatment operations cannot be edited unless # reset to OPEN status. field.disabled = True
[docs] def save(self, commit=True): instance = super().save(commit=False) if commit: # Copy emission intensity if one exists in the emitter. emitter_intensity = instance.emitter.emission_intensity if emitter_intensity is not None: instance.emission_intensity = emitter_intensity.copy_with_owner( IntensityCapableEntity.TREATMENT_OPERATION, instance.pk, ) instance.save() return instance
[docs] class TreatmentLineForm(ModelForm):
[docs] class Meta: model = TreatmentLine fields = [ "material", "weight_value", "weight_unit", ]
weight_unit = UnitChoiceField(units=UnitCollection.MASS)
[docs] class TreatmentInputLineForm(TreatmentLineForm):
[docs] class Meta: model = TreatmentInputLine fields = TreatmentLineForm.Meta.fields + ["treatment"] widgets = { "treatment": forms.HiddenInput(), }
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["material"].disabled = True
[docs] class TreatmentOutputLineForm(TreatmentLineForm):
[docs] class Meta: model = TreatmentOutputLine fields = TreatmentLineForm.Meta.fields + ["treatment"] widgets = { "treatment": forms.HiddenInput(), }
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["material"].disabled = True
[docs] class TreatmentOperationInputLineForm(TreatmentLineForm):
[docs] class Meta: model = TreatmentOperationInputLine fields = TreatmentLineForm.Meta.fields + ["operation"] widgets = { "operation": forms.HiddenInput(), }
[docs] class TreatmentOperationOutputLineForm(TreatmentLineForm):
[docs] class Meta: model = TreatmentOperationOutputLine fields = TreatmentLineForm.Meta.fields + ["operation"] widgets = { "operation": forms.HiddenInput(), }