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

#  Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from ferrosoft.apps.emiflow.models import Emitter


[docs] class CopyEnergyCarrierFromEmitter:
[docs] def clean_energy_carrier(self): # noinspection PyUnresolvedReferences energy_carrier = self.cleaned_data["energy_carrier"] if energy_carrier is not None: return energy_carrier # Check if emitter is selected and whether it has an energy carrier. # If not, require energy carrier to be selected. # noinspection PyUnresolvedReferences emitter: Emitter | None = self.cleaned_data.get("emitter", None) if emitter is None: raise ValidationError( _( "Please select a value. If you first select an emitter that contains an energy carrier, you do not need to select a value here." ) ) if emitter.energy_carrier is None: raise ValidationError( _( "Please select a value, as the emitter does not contain an energy carrier." ) ) return emitter.energy_carrier