Skip to content

Commit

Permalink
fix(camera_preset): fixed migrations
Browse files Browse the repository at this point in the history
Signed-off-by: Abhiuday <[email protected]>
  • Loading branch information
aeswibon committed Apr 18, 2024
1 parent e69ce96 commit f4d725b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 34 deletions.
37 changes: 25 additions & 12 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def validate(self, attrs):
return super().validate(attrs)


class CameraPresetSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)

class Meta:
model = CameraPreset
exclude = ("deleted", "external_id")
read_only_fields = TIMESTAMP_FIELDS


class AssetBedSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)

Expand All @@ -97,6 +106,8 @@ class AssetBedSerializer(ModelSerializer):
asset = UUIDField(write_only=True, required=True)
bed = UUIDField(write_only=True, required=True)

camera_presets = CameraPresetSerializer(many=True, read_only=True)

class Meta:
model = AssetBed
exclude = ("deleted", "external_id")
Expand Down Expand Up @@ -142,18 +153,20 @@ def validate(self, attrs):
)
return super().validate(attrs)


class CameraPresetSerializer(ModelSerializer):
id = UUIDField(source="external_id", read_only=True)
asset_beds = ListField(child=UUIDField(), required=True, write_only=True)
asset_beds_objects = AssetBedSerializer(
source="asset_beds", many=True, read_only=True
)

class Meta:
model = CameraPreset
exclude = ("deleted", "external_id")
read_only_fields = TIMESTAMP_FIELDS
def create(self, validated_data):
asset = validated_data["asset"]
bed = validated_data["bed"]
asset_bed = AssetBed.objects.filter(asset=asset, bed=bed).first()
if not asset_bed:
asset_bed = super().create(validated_data)

if asset.asset_class == AssetClasses.ONVIF.name:
CameraPreset.objects.create(
asset_bed=asset_bed,
created_by=self.context["request"].user,
updated_by=self.context["request"].user,
)
return asset_bed


class PatientAssetBedSerializer(ModelSerializer):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.10 on 2024-04-08 13:13
# Generated by Django 4.2.10 on 2024-04-18 14:09

import uuid

Expand All @@ -7,10 +7,42 @@
from django.db import migrations, models


def add_camerapreset(apps, schema_editor):
CameraPreset = apps.get_model("facility", "CameraPreset")
AssetBed = apps.get_model("facility", "AssetBed")
User = apps.get_model("users", "User")

presets = []
for asset_bed in AssetBed.objects.all():
unique_asset_bed = AssetBed.objects.filter(
asset=asset_bed.asset, bed=asset_bed.bed
).first()
presets.append(
CameraPreset(
asset_bed=unique_asset_bed,
x=asset_bed.meta.get("x", 0),
y=asset_bed.meta.get("y", 0),
zoom=asset_bed.meta.get("zoom", 1),
preset_name=asset_bed.meta.get("preset_name", "Default"),
created_by=User.objects.first(),
updated_by=User.objects.first(),
created_date=asset_bed.created_date,
modified_date=asset_bed.modified_date,
)
)

CameraPreset.objects.bulk_create(presets)
AssetBed.objects.exclude(
id__in=AssetBed.objects.values("asset", "bed")
.annotate(min_id=models.Min("id"))
.values("min_id")
).delete()


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("facility", "0425_merge_20240403_2055"),
("facility", "0427_dailyround_is_parsed_by_ocr"),
]

operations = [
Expand Down Expand Up @@ -39,12 +71,16 @@ class Migration(migrations.Migration):
models.DateTimeField(auto_now=True, db_index=True, null=True),
),
("deleted", models.BooleanField(db_index=True, default=False)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updateded_at", models.DateTimeField(auto_now=True)),
("x", models.FloatField()),
("y", models.FloatField()),
("zoom", models.FloatField()),
("preset_name", models.CharField(max_length=255)),
(
"asset_beds",
models.ManyToManyField(
related_name="camera_presets", to="facility.assetbed"
"asset_bed",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="camera_presets",
to="facility.assetbed",
),
),
(
Expand Down Expand Up @@ -72,4 +108,9 @@ class Migration(migrations.Migration):
"abstract": False,
},
),
migrations.RunPython(add_camerapreset, reverse_code=migrations.RunPython.noop),
migrations.RemoveField(
model_name="assetbed",
name="meta",
),
]
28 changes: 24 additions & 4 deletions care/facility/models/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,36 @@ def delete(self, *args, **kwargs) -> None:
class AssetBed(BaseModel):
asset = models.ForeignKey(Asset, on_delete=models.PROTECT, null=False, blank=False)
bed = models.ForeignKey(Bed, on_delete=models.PROTECT, null=False, blank=False)
meta = JSONField(default=dict, blank=True)

def __str__(self):
return f"{self.asset.name} - {self.bed.name}"


class CameraPreset(BaseModel):
asset_beds = models.ManyToManyField(AssetBed, related_name="camera_presets")
asset_bed = models.ForeignKey(
AssetBed,
on_delete=models.PROTECT,
null=False,
blank=False,
related_name="camera_presets",
)
x = models.FloatField(
null=False,
blank=False,
)
y = models.FloatField(
null=False,
blank=False,
)
zoom = models.FloatField(
null=False,
blank=False,
)
preset_name = models.CharField(
max_length=255,
null=False,
blank=False,
)
created_by = models.ForeignKey(
User,
on_delete=models.PROTECT,
Expand All @@ -88,8 +110,6 @@ class CameraPreset(BaseModel):
blank=True,
related_name="camera_presets_updated_by",
)
created_at = models.DateTimeField(auto_now_add=True)
updateded_at = models.DateTimeField(auto_now=True)


class ConsultationBed(BaseModel):
Expand Down
6 changes: 2 additions & 4 deletions docker-compose.local.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.4'

services:
backend:
image: care_local
Expand All @@ -10,7 +8,7 @@ services:
- ./docker/.local.env
volumes:
- .:/app
entrypoint: [ "bash", "scripts/start-dev.sh" ]
entrypoint: ["bash", "scripts/start-dev.sh"]
ports:
- "9000:9000"
- "9876:9876" #debugpy
Expand All @@ -27,7 +25,7 @@ services:
image: care_local
env_file:
- ./docker/.local.env
entrypoint: [ "bash", "scripts/celery-dev.sh" ]
entrypoint: ["bash", "scripts/celery-dev.sh"]
restart: unless-stopped
depends_on:
- db
Expand Down
8 changes: 3 additions & 5 deletions docker-compose.pre-built.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
version: '3.4'

services:
backend:
image: "ghcr.io/coronasafe/care:latest"
env_file:
- ./docker/.prebuilt.env
entrypoint: [ "bash", "start-ecs.sh" ]
entrypoint: ["bash", "start-ecs.sh"]
restart: unless-stopped
depends_on:
db:
Expand All @@ -21,7 +19,7 @@ services:
image: "ghcr.io/coronasafe/care:latest"
env_file:
- ./docker/.prebuilt.env
entrypoint: [ "bash", "celery_worker-ecs.sh" ]
entrypoint: ["bash", "celery_worker-ecs.sh"]
restart: unless-stopped
depends_on:
db:
Expand All @@ -35,7 +33,7 @@ services:
image: "ghcr.io/coronasafe/care:latest"
env_file:
- ./docker/.prebuilt.env
entrypoint: [ "bash", "celery_beat-ecs.sh" ]
entrypoint: ["bash", "celery_beat-ecs.sh"]
restart: unless-stopped
depends_on:
- db
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.4'

networks:
default:
name: care
Expand Down

0 comments on commit f4d725b

Please sign in to comment.