Skip to content

Commit aa0a3c5

Browse files
committed
fix(file): add is_file_empty to check for file with empty content
- add typing for file content on asset serializers - update firebase submodule to develop
1 parent ace7a21 commit aa0a3c5

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

apps/common/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.db.models.fields import files
55

66
from main.config import Config
7+
from utils.common import is_file_empty
78

89

910
@typing.overload
@@ -15,7 +16,7 @@ def get_absolute_uri(file: files.FieldFile) -> str: ...
1516

1617

1718
def get_absolute_uri(file: files.FieldFile | None) -> str | None:
18-
if not file:
19+
if is_file_empty(file):
1920
return None
2021
if isinstance(file.storage, FileSystemStorage):
2122
# FIXME(tnagorra): We should change the url method in storage

apps/project/serializers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
from .models import Organization, Project, ProjectAsset, ProjectAssetInputTypeEnum, ProjectTypeEnum
2323
from .tasks import process_project_task, push_project_to_firebase
2424

25+
if typing.TYPE_CHECKING:
26+
from django.core.files.base import ContentFile
27+
2528
VALID_PROJECT_STATUS_TRANSITIONS = set(
2629
[
2730
(Project.Status.DRAFT, Project.Status.MARKED_AS_READY),
@@ -371,7 +374,7 @@ def _validate_aoi_geometry(
371374
attrs: dict[str, typing.Any],
372375
mimetype: AssetMimetypeEnum | None,
373376
) -> None:
374-
file = attrs.get("file")
377+
file: ContentFile[bytes] | None = attrs.get("file")
375378
if not file:
376379
raise ValidationError("Required field file is not provided.")
377380

@@ -421,7 +424,7 @@ def _validate_cover_image(
421424
attrs: dict[str, typing.Any],
422425
mimetype: AssetMimetypeEnum | None,
423426
) -> None:
424-
file = attrs.get("file")
427+
file: ContentFile[bytes] | None = attrs.get("file")
425428
if not file:
426429
raise ValidationError("Required field file is not provided.")
427430

@@ -437,7 +440,7 @@ def _validate_object_image(
437440
attrs: dict[str, typing.Any],
438441
mimetype: AssetMimetypeEnum | None,
439442
) -> None:
440-
file = attrs.get("file")
443+
file: ContentFile[bytes] | None = attrs.get("file")
441444
if not file:
442445
return
443446

apps/tutorial/serializers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
)
2626
from .tasks import push_tutorial_to_firebase
2727

28+
if typing.TYPE_CHECKING:
29+
from django.core.files.base import ContentFile
30+
2831

2932
class TutorialTaskSerializerContextType(DrfContextType):
3033
scenario: TutorialScenarioPage
@@ -461,7 +464,7 @@ def _validate_information_block_image(
461464
attrs: dict[str, typing.Any],
462465
mimetype: AssetMimetypeEnum | None,
463466
) -> None:
464-
file = attrs.get("file")
467+
file: ContentFile[bytes] | None = attrs.get("file")
465468
if not file:
466469
raise ValidationError("Required field file is not provided.")
467470

firebase

utils/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@
1111
from django.core.files.base import ContentFile
1212
from django.core.serializers.json import DjangoJSONEncoder
1313
from django.db import models
14+
from django.db.models.fields import files
1415
from django.utils.translation import gettext
1516
from geojson_pydantic import FeatureCollection
1617
from ulid import ULID
1718

1819

20+
# NOTE: We are treating file with empty name as None as well
21+
def is_file_empty(file: files.FieldFile | None) -> typing.TypeIs[None]:
22+
if not file:
23+
return True
24+
if not file.name: # noqa: SIM103
25+
return True
26+
return False
27+
28+
1929
def validate_imagery_url(url: str, *, support_quad_key: bool | None = True):
2030
"""Check if imagery url contains xyz or quad key placeholders."""
2131
if all([substring in url for substring in ["{x}", "{y}", "{z}"]]) and not any(

0 commit comments

Comments
 (0)