RadicalxChange / rxc-voice

An app for decentralized democratic governance.
https://voice.radicalxchange.org/
Other
42 stars 14 forks source link

Enforce correct start / end date orders for event stages #84

Open alexrandaccio opened 2 years ago

alexrandaccio commented 2 years ago

notes from discord chat Add backend validation so group admin is not able to create deliberation / delegation / election in the wrong date order via the API.

Originally posted by @whatSocks in https://github.com/RadicalxChange/rxc-voice/issues/47#issuecomment-879378186

Solution from DRF docs

Object-level validation To do any other validation that requires access to multiple fields, add a method called .validate() to your Serializer subclass. This method takes a single argument, which is a dictionary of field values. It should raise a serializers.ValidationError if necessary, or just return the validated values. For example:

from rest_framework import serializers

class EventSerializer(serializers.Serializer):
    description = serializers.CharField(max_length=100)
    start = serializers.DateTimeField()
    finish = serializers.DateTimeField()

    def validate(self, data):
        """
        Check that start is before finish.
        """
        if data['start'] > data['finish']:
            raise serializers.ValidationError("finish must occur after start")
        return data