Skip to content

Commit

Permalink
Merge pull request #741 from coronasafe/master
Browse files Browse the repository at this point in the history
Production Release; May Week 2
  • Loading branch information
gigincg authored May 13, 2022
2 parents 346baa1 + 653e894 commit bba8471
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 84 deletions.
22 changes: 9 additions & 13 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class ConsultationBedSerializer(ModelSerializer):

bed_object = BedSerializer(source="bed", read_only=True)

consultation = ExternalIdSerializerField(queryset=PatientConsultation.objects.all(), write_only=True, required=True)
consultation = ExternalIdSerializerField(
queryset=PatientConsultation.objects.all(), write_only=True, required=True
)
bed = ExternalIdSerializerField(queryset=Bed.objects.all(), write_only=True, required=True)

class Meta:
Expand All @@ -107,28 +109,22 @@ def validate(self, attrs):
end_date = attrs.get("end_date", None)
existing_qs = ConsultationBed.objects.filter(consultation=consultation, bed=bed)
# Conflict checking logic
if existing_qs.objects.filter(start_date__gt=start_date, end_date__lt=start_date).exists():
if existing_qs.filter(start_date__gt=start_date, end_date__lt=start_date).exists():
raise ValidationError({"start_date": "Cannot create conflicting entry"})
if end_date:
if existing_qs.objects.filter(start_date__gt=end_date, end_date__lt=end_date).exists():
if existing_qs.filter(start_date__gt=end_date, end_date__lt=end_date).exists():
raise ValidationError({"end_date": "Cannot create conflicting entry"})

else:
raise ValidationError(
{
"consultation": "Field is Required",
"bed": "Field is Required",
"start_date": "Field is Required",
}
{"consultation": "Field is Required", "bed": "Field is Required", "start_date": "Field is Required",}
)
return super().validate(attrs)

def create(self, validated_data):
consultation = validated_data["consultation"]
bed = validated_data["bed"]
existing_qs = ConsultationBed.objects.filter(consultation=consultation, bed=bed)
if existing_qs.exists():
existing_qs.end_date = validated_data["start_date"]
existing_qs.save()
existing_beds = ConsultationBed.objects.filter(consultation=consultation, bed=bed, end_date__isnull=True)
existing_beds.update(end_date=validated_data["start_date"])
obj = super().create(validated_data)
consultation.current_bed = obj # This needs better logic, when an update occurs and the latest bed is no longer the last bed consultation relation added.
consultation.save(update_fields=["current_bed"])
Expand Down
2 changes: 2 additions & 0 deletions care/facility/api/serializers/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Meta:
"district_object",
"state_object",
"facility_type",
"cover_image_url",
)


Expand Down Expand Up @@ -96,6 +97,7 @@ class Meta:
"expected_type_b_cylinders",
"expected_type_c_cylinders",
"expected_type_d_cylinders",
"cover_image_url",
]
read_only_fields = ("modified_date", "created_date")

Expand Down
2 changes: 2 additions & 0 deletions care/facility/api/viewsets/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class AssetBedViewSet(
serializer_class = AssetBedSerializer
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AssetBedFilter
lookup_field = "external_id"

def get_queryset(self):
user = self.request.user
Expand All @@ -87,6 +88,7 @@ class ConsultationBedViewSet(ListModelMixin, RetrieveModelMixin, CreateModelMixi
serializer_class = ConsultationBedSerializer
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = ConsultationBedFilter
lookup_field = "external_id"

def get_queryset(self):
user = self.request.user
Expand Down
6 changes: 3 additions & 3 deletions care/facility/api/viewsets/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class DummyNotificationSerializer(Serializer): # Dummy for Spec
@action(detail=False, methods=["POST"])
def notify(self, request, *args, **kwargs):
user = request.user
if "facility" not in request.data:
if "facility" not in request.data or request.data["facility"] == "":
raise ValidationError({"facility": "is required"})
if "message" not in request.data:
raise ValidationError({"mesasge": "is required"})
if "message" not in request.data or request.data["message"] == "":
raise ValidationError({"message": "is required"})
facilities = get_facility_queryset(user)
facility = get_object_or_404(facilities.filter(external_id=request.data["facility"]))
NotificationGenerator(
Expand Down
22 changes: 22 additions & 0 deletions care/facility/migrations/0290_auto_20220426_2231.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.2.11 on 2022-04-26 17:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('facility', '0289_merge_20220422_2220'),
]

operations = [
migrations.AlterField(
model_name='asset',
name='qr_code_id',
field=models.CharField(blank=True, default=None, max_length=1024, null=True),
),
migrations.AddConstraint(
model_name='asset',
constraint=models.UniqueConstraint(condition=models.Q(qr_code_id__isnull=False), fields=('qr_code_id',), name='qr_code_unique_when_not_null'),
),
]
18 changes: 18 additions & 0 deletions care/facility/migrations/0291_facility_cover_image_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.11 on 2022-04-29 08:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('facility', '0290_auto_20220426_2231'),
]

operations = [
migrations.AddField(
model_name='facility',
name='cover_image_url',
field=models.URLField(blank=True, default=None, null=True),
),
]
18 changes: 18 additions & 0 deletions care/facility/migrations/0292_auto_20220430_1748.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.11 on 2022-04-30 12:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('facility', '0291_facility_cover_image_url'),
]

operations = [
migrations.AlterField(
model_name='bed',
name='description',
field=models.TextField(blank=True, default=''),
),
]
64 changes: 23 additions & 41 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

from django.contrib.postgres.fields.jsonb import JSONField
from django.db import models
from django.db.models import Q

from care.facility.models.facility import Facility
from care.users.models import User, phone_number_regex
from care.utils.assetintegration.asset_classes import AssetClasses
from care.utils.models.base import BaseModel


def get_random_asset_id():
return str(uuid.uuid4())


class AssetLocation(BaseModel):
"""
This model is also used to store rooms that the assets are in, Since these rooms are mapped to
Expand All @@ -26,12 +29,8 @@ class RoomType(enum.Enum):

name = models.CharField(max_length=1024, blank=False, null=False)
description = models.TextField(default="", null=True, blank=True)
location_type = models.IntegerField(
choices=RoomTypeChoices, default=RoomType.OTHER.value
)
facility = models.ForeignKey(
Facility, on_delete=models.PROTECT, null=False, blank=False
)
location_type = models.IntegerField(choices=RoomTypeChoices, default=RoomType.OTHER.value)
facility = models.ForeignKey(Facility, on_delete=models.PROTECT, null=False, blank=False)


class Asset(BaseModel):
Expand All @@ -51,16 +50,10 @@ class Status(enum.Enum):

name = models.CharField(max_length=1024, blank=False, null=False)
description = models.TextField(default="", null=True, blank=True)
asset_type = models.IntegerField(
choices=AssetTypeChoices, default=AssetType.INTERNAL.value
)
asset_class = models.IntegerField(
choices=AssetClassChoices, default=None, null=True, blank=True
)
asset_type = models.IntegerField(choices=AssetTypeChoices, default=AssetType.INTERNAL.value)
asset_class = models.IntegerField(choices=AssetClassChoices, default=None, null=True, blank=True)
status = models.IntegerField(choices=StatusChoices, default=Status.ACTIVE.value)
current_location = models.ForeignKey(
AssetLocation, on_delete=models.PROTECT, null=False, blank=False
)
current_location = models.ForeignKey(AssetLocation, on_delete=models.PROTECT, null=False, blank=False)
is_working = models.BooleanField(default=None, null=True, blank=True)
not_working_reason = models.CharField(max_length=1024, blank=True, null=True)
serial_number = models.CharField(max_length=1024, blank=True, null=True)
Expand All @@ -69,45 +62,34 @@ class Status(enum.Enum):
# Vendor Details
vendor_name = models.CharField(max_length=1024, blank=True, null=True)
support_name = models.CharField(max_length=1024, blank=True, null=True)
support_phone = models.CharField(
max_length=14, validators=[phone_number_regex], default=""
)
support_phone = models.CharField(max_length=14, validators=[phone_number_regex], default="")
support_email = models.EmailField(blank=True, null=True)
qr_code_id = models.CharField(max_length=1024, unique=True, blank=True, default=get_random_asset_id)
qr_code_id = models.CharField(max_length=1024, blank=True, default=None, null=True)

class Meta:
constraints = [
models.UniqueConstraint(
fields=["qr_code_id"], name="qr_code_unique_when_not_null", condition=Q(qr_code_id__isnull=False)
),
]


class UserDefaultAssetLocation(BaseModel):
user = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
location = models.ForeignKey(
AssetLocation, on_delete=models.PROTECT, null=False, blank=False
)
location = models.ForeignKey(AssetLocation, on_delete=models.PROTECT, null=False, blank=False)


class FacilityDefaultAssetLocation(BaseModel):
facility = models.ForeignKey(
Facility, on_delete=models.PROTECT, null=False, blank=False
)
location = models.ForeignKey(
AssetLocation, on_delete=models.PROTECT, null=False, blank=False
)
facility = models.ForeignKey(Facility, on_delete=models.PROTECT, null=False, blank=False)
location = models.ForeignKey(AssetLocation, on_delete=models.PROTECT, null=False, blank=False)


class AssetTransaction(BaseModel):
asset = models.ForeignKey(Asset, on_delete=models.PROTECT, null=False, blank=False)
from_location = models.ForeignKey(
AssetLocation,
on_delete=models.PROTECT,
related_name="from_location",
null=False,
blank=False,
AssetLocation, on_delete=models.PROTECT, related_name="from_location", null=False, blank=False,
)
to_location = models.ForeignKey(
AssetLocation,
on_delete=models.PROTECT,
related_name="to_location",
null=False,
blank=False,
)
performed_by = models.ForeignKey(
User, on_delete=models.PROTECT, null=False, blank=False
AssetLocation, on_delete=models.PROTECT, related_name="to_location", null=False, blank=False,
)
performed_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
2 changes: 1 addition & 1 deletion care/facility/models/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BedType(enum.Enum):
BedTypeChoices = [(e.value, e.name) for e in BedType]

name = models.CharField(max_length=1024)
description = models.TextField(default="")
description = models.TextField(default="", blank=True)
bed_type = models.IntegerField(choices=BedTypeChoices, default=BedType.REGULAR.value)
facility = models.ForeignKey(Facility, on_delete=models.PROTECT, null=False, blank=False) # Deprecated
meta = JSONField(default=dict)
Expand Down
2 changes: 2 additions & 0 deletions care/facility/models/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class Facility(FacilityBaseModel, FacilityPermissionMixin):
User, through="FacilityUser", related_name="facilities", through_fields=("facility", "user"),
)

cover_image_url = models.URLField(blank=True, null=True, default=None)

class Meta:
verbose_name_plural = "Facilities"

Expand Down
Loading

0 comments on commit bba8471

Please sign in to comment.