serializers.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from rest_framework import serializers
  2. class StatsV2Serializer(serializers.Serializer):
  3. category = serializers.ChoiceField(choices=("error", "error"))
  4. interval = serializers.ChoiceField(
  5. choices=(("1d", "1 day"), ("1h", "1 hour"), ("1m", "1 minute")),
  6. default="1h",
  7. required=False,
  8. )
  9. project = serializers.ListField(
  10. child=serializers.IntegerField(min_value=-1), required=False
  11. )
  12. field = serializers.ChoiceField(
  13. choices=(
  14. ("sum(quantity)", "sum(quantity)"),
  15. ("sum(times_seen", "sum(times_seen"),
  16. ),
  17. )
  18. start = serializers.DateTimeField()
  19. end = serializers.DateTimeField()
  20. def validate(self, data):
  21. start = data.get("start")
  22. end = data.get("end")
  23. interval = data.get("interval")
  24. series_quantity = (end - start).days
  25. if interval == "1h":
  26. series_quantity *= 24
  27. elif interval == "1m":
  28. series_quantity *= 1440
  29. if series_quantity > 1000:
  30. raise serializers.ValidationError({"end": "Too many intervals"})
  31. return data