Add sensor_data app to Django settings and URL routing
- Included sensor_data in the INSTALLED_APPS of settings.py. - Added URL path for sensor_data in urls.py to enable API access.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
from django.db import transaction
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from soil_data.models import SoilLocation
|
||||
|
||||
from .models import ParameterUpdateLog, SensorData, SensorDataHistory, SensorParameter
|
||||
from .serializers import (
|
||||
SensorDataResponseSerializer,
|
||||
SensorDataUpdateSerializer,
|
||||
SensorParameterSerializer,
|
||||
)
|
||||
|
||||
|
||||
class SensorDataUpdateView(APIView):
|
||||
"""
|
||||
آپدیت داده سنسور. هنگام آپدیت، نسخه فعلی در SensorDataHistory ذخیره میشود.
|
||||
"""
|
||||
|
||||
def put(self, request, uuid_sensor):
|
||||
return self._update(request, uuid_sensor)
|
||||
|
||||
def patch(self, request, uuid_sensor):
|
||||
return self._update(request, uuid_sensor, partial=True)
|
||||
|
||||
def _update(self, request, uuid_sensor, partial=False):
|
||||
serializer = SensorDataUpdateSerializer(
|
||||
data=request.data, partial=partial
|
||||
)
|
||||
if not serializer.is_valid():
|
||||
return Response(
|
||||
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
location_id = serializer.validated_data.pop("location_id")
|
||||
location = SoilLocation.objects.filter(pk=location_id).first()
|
||||
if not location:
|
||||
return Response(
|
||||
{"code": 404, "msg": "location_id یافت نشد.", "data": None},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
with transaction.atomic():
|
||||
sensor_data, created = SensorData.objects.get_or_create(
|
||||
uuid_sensor=uuid_sensor,
|
||||
defaults={"location": location, **serializer.validated_data},
|
||||
)
|
||||
|
||||
if not created:
|
||||
# آپدیت رکورد اصلی
|
||||
for key, value in serializer.validated_data.items():
|
||||
setattr(sensor_data, key, value)
|
||||
sensor_data.save()
|
||||
|
||||
# ذخیره نسخه جدید (همان مقادیر جدول اصلی) در تاریخچه
|
||||
SensorDataHistory.objects.create(
|
||||
uuid_sensor=sensor_data.uuid_sensor,
|
||||
location_id=sensor_data.location_id,
|
||||
soil_moisture=sensor_data.soil_moisture,
|
||||
soil_temperature=sensor_data.soil_temperature,
|
||||
soil_ph=sensor_data.soil_ph,
|
||||
electrical_conductivity=sensor_data.electrical_conductivity,
|
||||
nitrogen=sensor_data.nitrogen,
|
||||
phosphorus=sensor_data.phosphorus,
|
||||
potassium=sensor_data.potassium,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": SensorDataResponseSerializer(sensor_data).data,
|
||||
},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class SensorParameterCreateView(APIView):
|
||||
"""
|
||||
اضافه کردن پارامتر جدید و ثبت در ParameterUpdateLog.
|
||||
"""
|
||||
|
||||
def post(self, request):
|
||||
serializer = SensorParameterSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response(
|
||||
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
code = serializer.validated_data["code"]
|
||||
name_fa = serializer.validated_data["name_fa"]
|
||||
unit = serializer.validated_data.get("unit", "")
|
||||
|
||||
with transaction.atomic():
|
||||
parameter, created = SensorParameter.objects.update_or_create(
|
||||
code=code,
|
||||
defaults={"name_fa": name_fa, "unit": unit},
|
||||
)
|
||||
action = (
|
||||
ParameterUpdateLog.ACTION_ADDED
|
||||
if created
|
||||
else ParameterUpdateLog.ACTION_MODIFIED
|
||||
)
|
||||
ParameterUpdateLog.objects.create(parameter=parameter, action=action)
|
||||
|
||||
return Response(
|
||||
{
|
||||
"code": 201,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"id": parameter.id,
|
||||
"code": parameter.code,
|
||||
"name_fa": parameter.name_fa,
|
||||
"unit": parameter.unit,
|
||||
"created_at": parameter.created_at,
|
||||
"action": action,
|
||||
},
|
||||
},
|
||||
status=status.HTTP_201_CREATED,
|
||||
)
|
||||
Reference in New Issue
Block a user