UPDATE
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
# Generated by Django 5.2.5 on 2025-02-24 00:00
|
||||
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("farm_hub", "0009_farmhub_irrigation_method_fields"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="FarmerTodoZone",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("uuid", models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, unique=True)),
|
||||
("label", models.CharField(max_length=255)),
|
||||
("value", models.CharField(max_length=255)),
|
||||
("is_active", models.BooleanField(default=True)),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
(
|
||||
"farm",
|
||||
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="todo_zones", to="farm_hub.farmhub"),
|
||||
),
|
||||
],
|
||||
options={"db_table": "farmer_todo_zones", "ordering": ["label"]},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="FarmerTodoTag",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("uuid", models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, unique=True)),
|
||||
("label", models.CharField(max_length=100)),
|
||||
("value", models.CharField(max_length=100)),
|
||||
("is_active", models.BooleanField(default=True)),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
(
|
||||
"farm",
|
||||
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="todo_tags", to="farm_hub.farmhub"),
|
||||
),
|
||||
],
|
||||
options={"db_table": "farmer_todo_tags", "ordering": ["label"]},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="FarmerTodoTask",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("title", models.CharField(max_length=255)),
|
||||
("scheduled_date", models.DateField()),
|
||||
("time", models.TimeField()),
|
||||
("priority", models.CharField(choices=[("زیاد", "High"), ("متوسط", "Medium"), ("کم", "Low")], max_length=16)),
|
||||
("note", models.TextField(blank=True, default="")),
|
||||
("status", models.CharField(choices=[("open", "Open"), ("done", "Done")], default="open", max_length=16)),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
(
|
||||
"farm",
|
||||
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="todo_tasks", to="farm_hub.farmhub"),
|
||||
),
|
||||
(
|
||||
"tags",
|
||||
models.ManyToManyField(blank=True, related_name="tasks", to="farmer_todos.farmertodotag"),
|
||||
),
|
||||
(
|
||||
"zone",
|
||||
models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="tasks", to="farmer_todos.farmertodozone"),
|
||||
),
|
||||
],
|
||||
options={"db_table": "farmer_todo_tasks", "ordering": ["scheduled_date", "time", "created_at"]},
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="farmertodozone",
|
||||
constraint=models.UniqueConstraint(fields=("farm", "value"), name="uniq_farmer_todo_zone_per_farm"),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="farmertodotag",
|
||||
constraint=models.UniqueConstraint(fields=("farm", "value"), name="uniq_farmer_todo_tag_per_farm"),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,104 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
TodoZone = apps.get_model("farmer_todos", "FarmerTodoZone")
|
||||
TodoTag = apps.get_model("farmer_todos", "FarmerTodoTag")
|
||||
TodoTask = apps.get_model("farmer_todos", "FarmerTodoTask")
|
||||
CalendarZone = apps.get_model("farmer_calendar", "FarmerCalendarZone")
|
||||
CalendarTag = apps.get_model("farmer_calendar", "FarmerCalendarTag")
|
||||
CalendarEvent = apps.get_model("farmer_calendar", "FarmerCalendarEvent")
|
||||
|
||||
zone_map = {}
|
||||
for todo_zone in TodoZone.objects.all().iterator():
|
||||
calendar_zone, _ = CalendarZone.objects.get_or_create(
|
||||
farm_id=todo_zone.farm_id,
|
||||
value=todo_zone.value,
|
||||
defaults={
|
||||
"uuid": todo_zone.uuid,
|
||||
"label": todo_zone.label,
|
||||
"is_active": todo_zone.is_active,
|
||||
"created_at": todo_zone.created_at,
|
||||
"updated_at": todo_zone.updated_at,
|
||||
},
|
||||
)
|
||||
updated = False
|
||||
if calendar_zone.label != todo_zone.label:
|
||||
calendar_zone.label = todo_zone.label
|
||||
updated = True
|
||||
if calendar_zone.is_active != todo_zone.is_active:
|
||||
calendar_zone.is_active = todo_zone.is_active
|
||||
updated = True
|
||||
if updated:
|
||||
calendar_zone.save(update_fields=["label", "is_active", "updated_at"])
|
||||
zone_map[todo_zone.id] = calendar_zone
|
||||
|
||||
tag_map = {}
|
||||
for todo_tag in TodoTag.objects.all().iterator():
|
||||
calendar_tag, _ = CalendarTag.objects.get_or_create(
|
||||
farm_id=todo_tag.farm_id,
|
||||
value=todo_tag.value,
|
||||
defaults={
|
||||
"uuid": todo_tag.uuid,
|
||||
"label": todo_tag.label,
|
||||
"is_active": todo_tag.is_active,
|
||||
"created_at": todo_tag.created_at,
|
||||
"updated_at": todo_tag.updated_at,
|
||||
},
|
||||
)
|
||||
updated = False
|
||||
if calendar_tag.label != todo_tag.label:
|
||||
calendar_tag.label = todo_tag.label
|
||||
updated = True
|
||||
if calendar_tag.is_active != todo_tag.is_active:
|
||||
calendar_tag.is_active = todo_tag.is_active
|
||||
updated = True
|
||||
if updated:
|
||||
calendar_tag.save(update_fields=["label", "is_active", "updated_at"])
|
||||
tag_map[todo_tag.id] = calendar_tag
|
||||
|
||||
through_model = TodoTask.tags.through
|
||||
task_tags = {}
|
||||
for relation in through_model.objects.all().iterator():
|
||||
task_tags.setdefault(relation.farmertodotask_id, []).append(relation.farmertodotag_id)
|
||||
|
||||
for todo_task in TodoTask.objects.all().iterator():
|
||||
calendar_event, created = CalendarEvent.objects.get_or_create(
|
||||
farm_id=todo_task.farm_id,
|
||||
title=todo_task.title,
|
||||
scheduled_date=todo_task.scheduled_date,
|
||||
time=todo_task.time,
|
||||
defaults={
|
||||
"zone": zone_map.get(todo_task.zone_id),
|
||||
"description": todo_task.note,
|
||||
"priority": todo_task.priority,
|
||||
"status": todo_task.status,
|
||||
"created_at": todo_task.created_at,
|
||||
"updated_at": todo_task.updated_at,
|
||||
},
|
||||
)
|
||||
if not created:
|
||||
calendar_event.zone = zone_map.get(todo_task.zone_id)
|
||||
calendar_event.description = todo_task.note
|
||||
calendar_event.priority = todo_task.priority
|
||||
calendar_event.status = todo_task.status
|
||||
calendar_event.save(update_fields=["zone", "description", "priority", "status", "updated_at"])
|
||||
|
||||
calendar_tags = [tag_map[tag_id] for tag_id in task_tags.get(todo_task.id, []) if tag_id in tag_map]
|
||||
if calendar_tags:
|
||||
calendar_event.tags.set(calendar_tags)
|
||||
|
||||
|
||||
def backwards(apps, schema_editor):
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("farmer_calendar", "0001_initial"),
|
||||
("farmer_todos", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forwards, backwards),
|
||||
]
|
||||
Reference in New Issue
Block a user