From f4d725bfba58e918201ecf84e38c110f17472482 Mon Sep 17 00:00:00 2001 From: Abhiuday Date: Thu, 18 Apr 2024 20:12:32 +0530 Subject: [PATCH] fix(camera_preset): fixed migrations Signed-off-by: Abhiuday --- care/facility/api/serializers/bed.py | 37 +++++++++---- ...0428_remove_assetbed_meta_camerapreset.py} | 55 ++++++++++++++++--- care/facility/models/bed.py | 28 ++++++++-- docker-compose.local.yaml | 6 +- docker-compose.pre-built.yaml | 8 +-- docker-compose.yaml | 2 - 6 files changed, 102 insertions(+), 34 deletions(-) rename care/facility/migrations/{0426_camerapreset.py => 0428_remove_assetbed_meta_camerapreset.py} (53%) diff --git a/care/facility/api/serializers/bed.py b/care/facility/api/serializers/bed.py index f1b437116c..811448529a 100644 --- a/care/facility/api/serializers/bed.py +++ b/care/facility/api/serializers/bed.py @@ -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) @@ -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") @@ -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): diff --git a/care/facility/migrations/0426_camerapreset.py b/care/facility/migrations/0428_remove_assetbed_meta_camerapreset.py similarity index 53% rename from care/facility/migrations/0426_camerapreset.py rename to care/facility/migrations/0428_remove_assetbed_meta_camerapreset.py index 1591b3a2a9..7c25fdbe69 100644 --- a/care/facility/migrations/0426_camerapreset.py +++ b/care/facility/migrations/0428_remove_assetbed_meta_camerapreset.py @@ -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 @@ -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 = [ @@ -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", ), ), ( @@ -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", + ), ] diff --git a/care/facility/models/bed.py b/care/facility/models/bed.py index 7abcc505d0..e8fdf7bed5 100644 --- a/care/facility/models/bed.py +++ b/care/facility/models/bed.py @@ -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, @@ -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): diff --git a/docker-compose.local.yaml b/docker-compose.local.yaml index aeb971ccd6..855565d310 100644 --- a/docker-compose.local.yaml +++ b/docker-compose.local.yaml @@ -1,5 +1,3 @@ -version: '3.4' - services: backend: image: care_local @@ -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 @@ -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 diff --git a/docker-compose.pre-built.yaml b/docker-compose.pre-built.yaml index 19dbd14194..c6ae7c1032 100644 --- a/docker-compose.pre-built.yaml +++ b/docker-compose.pre-built.yaml @@ -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: @@ -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: @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml index 11cfcc0a9c..f29b4e4581 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,3 @@ -version: '3.4' - networks: default: name: care