donation.py 785 B

123456789101112131415161718192021222324252627282930
  1. import re
  2. from rest_framework import serializers
  3. from desecapi import models
  4. class DonationSerializer(serializers.ModelSerializer):
  5. class Meta:
  6. model = models.Donation
  7. fields = ('name', 'iban', 'bic', 'amount', 'message', 'email', 'mref', 'interval')
  8. read_only_fields = ('mref',)
  9. extra_kwargs = { # do not return sensitive information
  10. 'iban': {'write_only': True},
  11. 'bic': {'write_only': True},
  12. 'message': {'write_only': True},
  13. }
  14. @staticmethod
  15. def validate_bic(value):
  16. return re.sub(r'[\s]', '', value)
  17. @staticmethod
  18. def validate_iban(value):
  19. return re.sub(r'[\s]', '', value)
  20. def create(self, validated_data):
  21. return self.Meta.model(**validated_data)