43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
from rest_framework import serializers
|
|
from django.contrib.gis.geos import Point
|
|
from .models import ObjektSigurnosti
|
|
# class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
|
# class Meta:
|
|
# model = ObjektSigurnosti
|
|
# fields = '__all__'
|
|
|
|
# class LogSerializer(serializers.ModelSerializer):
|
|
|
|
# user_full_name = serializers.ReadOnlyField(source='user.full_name')
|
|
|
|
# class Meta:
|
|
# model = Log
|
|
# fields = ('id', 'user', 'user_full_name', 'timestamp', 'akcija', 'opis')
|
|
|
|
class PointSerializer(serializers.Serializer):
|
|
lat = serializers.FloatField()
|
|
lon = serializers.FloatField()
|
|
|
|
def to_representation(self, instance):
|
|
return {'lat': instance.y, 'lon': instance.x}
|
|
|
|
class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
|
lokacija = PointSerializer()
|
|
|
|
class ObjektSigurnostiSerializer(serializers.ModelSerializer):
|
|
lokacija = PointSerializer()
|
|
|
|
class Meta:
|
|
model = ObjektSigurnosti
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
representation = super().to_representation(instance)
|
|
representation['lokacija'] = PointSerializer(instance.lokacija).data
|
|
return representation
|
|
|
|
def create(self, validated_data):
|
|
lokacija_data = validated_data.pop('lokacija')
|
|
lokacija = Point(lokacija_data['lon'], lokacija_data['lat'], srid=3765)
|
|
objekt = ObjektSigurnosti.objects.create(lokacija=lokacija, **validated_data)
|
|
return objekt |