Commit 2133c82f authored by Florent Chehab's avatar Florent Chehab

Moved some custom validation from django to rest framework

parent 1ff60054
Pipeline #26776 failed with stages
in 2 minutes and 4 seconds
......@@ -21,23 +21,20 @@ class Scholarship(BasicModule):
validators=[MinValueValidator(0)]
)
# Need custom validation
def clean(self, *args, **kwargs):
if self.amount_max < self.amount_min:
raise serializers.ValidationError(
"Amount_max should be greater or equal than amount_min")
super(Scholarship, self).clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.full_clean()
super(Scholarship, self).save(*args, **kwargs)
class Meta:
abstract = True
class ScholarshipSerializer(BasicModuleSerializer):
def validate(self, attrs):
print(attrs)
if attrs['amount_max'] < attrs['amount_min']:
raise serializers.ValidationError(
"Amount_max should be greater or equal than amount_min")
return attrs
class Meta:
model = Scholarship
fields = '__all__'
......
......@@ -27,31 +27,25 @@ class UniversitySemestersDates(BasicModule):
@classmethod
def get_serializer(cls):
return UniversitySemestersDatesSerializer
# Need custom validation
# TODO add tests
def clean(self, *args, **kwargs):
spring_ok = check_nones(self.spring_begin, self.spring_end)
autumn_ok = check_nones(self.autumn_begin, self.autumn_end)
class UniversitySemestersDatesSerializer(BasicModuleSerializer):
def validate(self, attrs):
spring_ok = check_nones(attrs['spring_begin'], attrs['spring_end'])
autumn_ok = check_nones(attrs['autumn_begin'], attrs['autumn_end'])
if not spring_ok:
spring_ok = self.spring_begin < self.spring_end
spring_ok = attrs['spring_begin'] < attrs['spring_end']
if not autumn_ok:
autumn_ok = self.autumn_begin < self.autumn_end
autumn_ok = attrs['autumn_begin'] < attrs['autumn_end']
# no check if spring_end < autumn_begin in case info is not available
if not (spring_ok and autumn_ok):
print("ici")
raise serializers.ValidationError("Check your dates !")
super(UniversitySemestersDates, self).clean(*args, **kwargs)
raise serializers.ValidationError("Check your dates !") # TODO better error
def save(self, *args, **kwargs):
self.full_clean()
super(UniversitySemestersDates, self).save(*args, **kwargs)
class UniversitySemestersDatesSerializer(BasicModuleSerializer):
return attrs
class Meta:
model = UniversitySemestersDates
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment