37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
|
|
from rest_framework import serializers
|
||
|
|
|
||
|
|
from .models import FarmAlertNotification
|
||
|
|
|
||
|
|
|
||
|
|
class FarmAlertsRequestSerializer(serializers.Serializer):
|
||
|
|
farm_uuid = serializers.CharField(required=False, help_text="شناسه مزرعه")
|
||
|
|
sensor_uuid = serializers.CharField(required=False, help_text="نام قدیمی برای farm_uuid")
|
||
|
|
query = serializers.CharField(required=False, allow_blank=True, help_text="سوال اختیاری")
|
||
|
|
|
||
|
|
def validate(self, attrs):
|
||
|
|
farm_uuid = attrs.get("farm_uuid") or attrs.get("sensor_uuid")
|
||
|
|
if not farm_uuid:
|
||
|
|
raise serializers.ValidationError({"farm_uuid": "farm_uuid الزامی است."})
|
||
|
|
attrs["farm_uuid"] = farm_uuid
|
||
|
|
return attrs
|
||
|
|
|
||
|
|
|
||
|
|
class FarmAlertNotificationSerializer(serializers.ModelSerializer):
|
||
|
|
class Meta:
|
||
|
|
model = FarmAlertNotification
|
||
|
|
fields = [
|
||
|
|
"id",
|
||
|
|
"farm_uuid",
|
||
|
|
"endpoint",
|
||
|
|
"level",
|
||
|
|
"title",
|
||
|
|
"message",
|
||
|
|
"suggested_action",
|
||
|
|
"source_alert_id",
|
||
|
|
"source_metric_type",
|
||
|
|
"payload",
|
||
|
|
"created_at",
|
||
|
|
"updated_at",
|
||
|
|
]
|
||
|
|
read_only_fields = fields
|