refactor: handle exception on vault unlock date check for validation

This commit is contained in:
ramvignesh-b
2026-04-18 04:39:18 +05:30
parent 11780cdbd4
commit 78e2625883
2 changed files with 32 additions and 5 deletions
@@ -0,0 +1,22 @@
# Generated by Django 6.0.4 on 2026-04-17 18:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("letters", "0008_letter_notified_at"),
]
operations = [
migrations.AlterField(
model_name="letter",
name="notified_at",
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
migrations.AlterField(
model_name="letter",
name="unlock_at",
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
]
+10 -5
View File
@@ -39,11 +39,14 @@ class LetterSerializer(serializers.ModelSerializer):
def to_representation(self, instance): def to_representation(self, instance):
fields = super().to_representation(instance) fields = super().to_representation(instance)
if fields["type"] == Letter.Type.VAULT and fields["status"] == Letter.Status.SEALED: if fields["type"] == Letter.Type.VAULT and fields["status"] == Letter.Status.SEALED:
unlock_datetime = datetime.fromisoformat(fields["unlock_at"]).replace(tzinfo=UTC) try:
if unlock_datetime - datetime.now(tz=UTC) > timedelta(seconds=0): unlock_datetime = datetime.fromisoformat(fields["unlock_at"]).replace(tzinfo=UTC)
fields["encrypted_content"] = None if unlock_datetime - datetime.now(tz=UTC) > timedelta(seconds=0):
fields["images"] = None fields["encrypted_content"] = None
fields["encrypted_dek"] = None fields["images"] = None
fields["encrypted_dek"] = None
except (ValueError, TypeError):
pass
return fields return fields
def validate(self, data): def validate(self, data):
@@ -54,4 +57,6 @@ class LetterSerializer(serializers.ModelSerializer):
raise serializers.ValidationError( raise serializers.ValidationError(
"encrypted_dek is required when encrypted_content and encrypted_metadata are present" "encrypted_dek is required when encrypted_content and encrypted_metadata are present"
) )
if data.get("type") == Letter.Type.VAULT and not data.get("unlock_at"):
raise serializers.ValidationError("unlock_at is required for vault letters")
return data return data