Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(file_upload): added new fields in file model and fixed file update permission #1109

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions care/facility/api/serializers/file_upload.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.timezone import localtime, now
from jsonschema import ValidationError
from rest_framework import serializers

Expand All @@ -8,6 +9,7 @@
from care.facility.models.patient_consultation import PatientConsultation
from care.facility.models.patient_sample import PatientSample
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.users.models import User
from config.serializers import ChoiceField


Expand Down Expand Up @@ -108,6 +110,7 @@ class FileUploadListSerializer(serializers.ModelSerializer):

id = serializers.UUIDField(source="external_id", read_only=True)
uploaded_by = UserBaseMinimumSerializer(read_only=True)
archived_by = UserBaseMinimumSerializer(read_only=True)
extension = serializers.CharField(source="get_extension", read_only=True)

class Meta:
Expand All @@ -116,6 +119,8 @@ class Meta:
"id",
"name",
"uploaded_by",
"archived_by",
"archived_datetime",
"upload_completed",
"is_archived",
"archive_reason",
Expand All @@ -129,6 +134,7 @@ class Meta:
class FileUploadUpdateSerializer(serializers.ModelSerializer):

id = serializers.UUIDField(source="external_id", read_only=True)
archived_by = UserBaseMinimumSerializer(read_only=True)

class Meta:
model = FileUpload
Expand All @@ -138,12 +144,29 @@ class Meta:
"upload_completed",
"is_archived",
"archive_reason",
"archived_by",
"archived_datetime",
)

def update(self, instance, validated_data):
user = self.context["request"].user
if instance.is_archived:
raise ValidationError("Operation not permitted when archived.")
return super().update(instance, validated_data)
raise serializers.ValidationError(
{"file": "Operation not permitted when archived."}
)
if user.user_type <= User.TYPE_VALUE_MAP["LocalBodyAdmin"]:
if instance.uploaded_by == user:
pass
else:
raise serializers.ValidationError(
{"permission": "Don't have permission to archive"}
)
file = super().update(instance, validated_data)
if file.is_archived:
file.archived_by = user
file.archived_datetime = localtime(now())
file.save()
return file

def validate(self, attrs):
validated = super().validate(attrs)
Expand Down
26 changes: 26 additions & 0 deletions care/facility/migrations/0327_fileupload_archived_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.2.11 on 2022-11-27 14:46

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('facility', '0326_auto_20221018_1526'),
]

operations = [
migrations.AddField(
model_name='fileupload',
name='archived_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='fileupload',
name='archived_datetime',
field=models.DateTimeField(blank=True, null=True),
),
]
4 changes: 4 additions & 0 deletions care/facility/models/file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class FileCategory(enum.Enum):
uploaded_by = models.ForeignKey(
User, on_delete=models.PROTECT, null=True, blank=True
)
archived_by = models.ForeignKey(
User, on_delete=models.PROTECT, null=True, blank=True
)
archived_datetime = models.DateTimeField(blank=True, null=True)
file_type = models.IntegerField(
choices=FileTypeChoices, default=FileType.PATIENT.value
)
Expand Down