Files
Ai/farm_data/management/commands/seed_farm_data.py
T
2026-04-06 23:50:24 +03:30

70 lines
2.2 KiB
Python

"""
Management command to seed a fixed demo farm-data record.
Run: python manage.py seed_farm_data
"""
from uuid import UUID
from django.core.management.base import BaseCommand
from farm_data.models import SensorData
from location_data.models import SoilLocation
from plant.models import Plant
from weather.models import WeatherForecast
DEMO_FARM_UUID = UUID("11111111-1111-1111-1111-111111111111")
DEMO_LATITUDE = "50.000000"
DEMO_LONGITUDE = "50.000000"
DEMO_SENSOR_PAYLOAD = {
"sensor-7-1": {
"soil_moisture": 42.3,
"soil_temperature": 21.4,
"soil_ph": 6.9,
"electrical_conductivity": 1.1,
"nitrogen": 28.0,
"phosphorus": 14.0,
"potassium": 19.0,
}
}
DEMO_PLANT_NAMES = [
"گوجه‌فرنگی",
"خیار",
]
class Command(BaseCommand):
help = "Seed a fixed farm-data row with farm_uuid=11111111-1111-1111-1111-111111111111."
def handle(self, *args, **options):
location, _ = SoilLocation.objects.get_or_create(
latitude=DEMO_LATITUDE,
longitude=DEMO_LONGITUDE,
)
weather_forecast = (
WeatherForecast.objects.filter(location=location)
.order_by("-forecast_date", "-id")
.first()
)
farm_data, created = SensorData.objects.update_or_create(
farm_uuid=DEMO_FARM_UUID,
defaults={
"center_location": location,
"weather_forecast": weather_forecast,
"sensor_payload": DEMO_SENSOR_PAYLOAD,
},
)
plants = list(Plant.objects.filter(name__in=DEMO_PLANT_NAMES).order_by("name"))
if plants:
farm_data.plants.set(plants)
status_text = "Created" if created else "Updated"
weather_text = weather_forecast.id if weather_forecast else "None"
plant_count = len(plants)
self.stdout.write(
self.style.SUCCESS(
f"{status_text} farm-data {farm_data.farm_uuid} for center_location_id={location.id} weather_forecast_id={weather_text} plants={plant_count}"
)
)
self.stdout.write(self.style.SUCCESS("\nDone seeding farm_data demo record."))