def validate(self, data): """Validate this payment schedule. As for Payment, payment method, recipient type and various other things need to fit together. """ if not self.Meta.model.is_payment_and_recipient_allowed( data["payment_method"], data["recipient_type"] ): raise serializers.ValidationError( _("ugyldig betalingsmetode for betalingsmodtager") ) if ( data["recipient_type"] == PaymentSchedule.COMPANY and "recipient_id" in data and data["recipient_id"] and not validate_cvr(data["recipient_id"]) ): raise serializers.ValidationError( _("Ugyldigt CVR nummer for firma") ) # Validate one_time and individual payment. one_time_payment = ( data["payment_type"] == PaymentSchedule.ONE_TIME_PAYMENT ) individual_payment = ( data["payment_type"] == PaymentSchedule.INDIVIDUAL_PAYMENT ) payment_frequency = data.get("payment_frequency", None) if ( not one_time_payment and not individual_payment ) and not payment_frequency: raise serializers.ValidationError( _( "En betalingstype der ikke er en engangsbetaling eller" " individuel betaling skal have en betalingsfrekvens" ) ) elif (one_time_payment or individual_payment) and payment_frequency: raise serializers.ValidationError( _( "En engangsbetaling eller individuel betaling må" " ikke have en betalingsfrekvens" ) ) payment_cost_type = data.get("payment_cost_type", None) payment_amount = data.get("payment_amount", None) payment_day_of_month = data.get("payment_day_of_month", None) payment_units = data.get("payment_units", None) if individual_payment: if payment_cost_type: raise serializers.ValidationError( _( "En individuel betaling må ikke have" " en betalingspristype" ) ) if payment_amount: raise serializers.ValidationError( _("En individuel betaling må ikke have et beløb") ) if payment_day_of_month: raise serializers.ValidationError( _( "En individuel betaling må ikke have" " en månedlig betalingsdato" ) ) if payment_units: raise serializers.ValidationError( _("en individuel betaling må ikke have betalingsenheder") ) # Validate payment/rate/unit info instance = self.instance if not instance and self.parent: # XXX: Get instance from parent form data, we need it. instance_id = self.parent.initial_data["payment_plan"].get("id") if instance_id: instance = PaymentSchedule.objects.get(id=instance_id) if payment_cost_type == PaymentSchedule.FIXED_PRICE: # Payment amount needs to be given, apart from that s'all # good. if data.get("payment_amount", None) is None: raise serializers.ValidationError( _("Beløb skal udfyldes ved fast beløb") ) # Rate can't be given. if data.get("payment_rate", None): raise serializers.ValidationError( _("Takst skal ikke angives ved fast beløb") ) # Price data can't be given. if data.get("price_per_unit") and data["price_per_unit"].get( "amount", None ): raise serializers.ValidationError( _("Beløb pr. enhed skal ikke angives ved fast takst") ) # Units can't be given. if data.get("payment_units", None): raise serializers.ValidationError( _("Enheder skal ikke angives ved fast beløb") ) elif payment_cost_type == PaymentSchedule.PER_UNIT_PRICE: # Units need to be given. if not data.get("payment_units", None): if not instance or instance.payment_units is None: raise serializers.ValidationError( _("Enheder skal angives ved pris pr. enhed") ) # Price data needs to be given. # If not given, start and end date default to None. if ( not data.get("price_per_unit", None) or data["price_per_unit"].get("amount", None) is None ): if not instance or not instance.price_per_unit: raise serializers.ValidationError( _("Beløb pr. enhed skal angives") ) # Rate can't be given. if data.get("payment_rate", None): raise serializers.ValidationError( _("Takst skal ikke angives ved pris pr. enhed") ) # Payment amount can't be given. if data.get("payment_amount", None): raise serializers.ValidationError( _("Beløbsfeltet skal ikke udfyldes ved pris pr. enhed") ) elif ( payment_cost_type == PaymentSchedule.GLOBAL_RATE_PRICE ): # pragma: no cover # Units need to be given. if not data.get("payment_units", None): raise serializers.ValidationError( _("Enheder skal angives ved fast takst") ) # Rate needs to be given. if not data.get("payment_rate", None): raise serializers.ValidationError(_("Takst skal angives")) # Payment amount can't be given. if data.get("payment_amount", None): raise serializers.ValidationError( _("Beløbsfeltet skal ikke udfyldes ved fast takst") ) # Price data can't be given. if data.get("price_per_unit") and data["price_per_unit"].get( "amount", None ): raise serializers.ValidationError( _("Beløb pr. enhed skal ikke angives ved fast takst") ) return data
def test_validate_cvr_with_spaces_success(self): self.assertTrue(validate_cvr(" 29244049 "))
def test_validate_cvr_failure_9_digits(self): self.assertFalse(validate_cvr("292440494"))
def test_validate_cvr_success(self): self.assertTrue(validate_cvr("26570514"))