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

Moved some custom validation from django to rest framework

parent 1ff60054
...@@ -21,23 +21,20 @@ class Scholarship(BasicModule): ...@@ -21,23 +21,20 @@ class Scholarship(BasicModule):
validators=[MinValueValidator(0)] 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):
super(Scholarship, self).save(*args, **kwargs)
class Meta: class Meta:
abstract = True abstract = True
class ScholarshipSerializer(BasicModuleSerializer): class ScholarshipSerializer(BasicModuleSerializer):
def validate(self, 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: class Meta:
model = Scholarship model = Scholarship
fields = '__all__' fields = '__all__'
...@@ -27,31 +27,25 @@ class UniversitySemestersDates(BasicModule): ...@@ -27,31 +27,25 @@ class UniversitySemestersDates(BasicModule):
@classmethod @classmethod
def get_serializer(cls): def get_serializer(cls):
return UniversitySemestersDatesSerializer return UniversitySemestersDatesSerializer
# Need custom validation
# TODO add tests
def clean(self, *args, **kwargs): class UniversitySemestersDatesSerializer(BasicModuleSerializer):
spring_ok = check_nones(self.spring_begin, self.spring_end)
autumn_ok = check_nones(self.autumn_begin, self.autumn_end) 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: if not spring_ok:
spring_ok = self.spring_begin < self.spring_end spring_ok = attrs['spring_begin'] < attrs['spring_end']
if not autumn_ok: 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 # no check if spring_end < autumn_begin in case info is not available
if not (spring_ok and autumn_ok): if not (spring_ok and autumn_ok):
print("ici") raise serializers.ValidationError("Check your dates !") # TODO better error
raise serializers.ValidationError("Check your dates !")
super(UniversitySemestersDates, self).clean(*args, **kwargs)
def save(self, *args, **kwargs): return attrs
super(UniversitySemestersDates, self).save(*args, **kwargs)
class UniversitySemestersDatesSerializer(BasicModuleSerializer):
class Meta: class Meta:
model = UniversitySemestersDates model = UniversitySemestersDates
Supports Markdown
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