Skip to content

Commit

Permalink
Remove all non-serializer extend_schema_field usages (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel-Therrien-Beslogic committed Sep 25, 2024
1 parent 3d517fe commit 94157be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
38 changes: 14 additions & 24 deletions canopeum_backend/canopeum_backend/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pyright: reportIncompatibleVariableOverride=false

import random
from decimal import Decimal
from typing import Any

from django.contrib.auth.password_validation import validate_password
Expand Down Expand Up @@ -353,10 +354,8 @@ class Meta:
"widget",
)

# https://github.com/tfranzel/drf-spectacular/issues/1212
@extend_schema_field(list[str]) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
def get_sponsors(self, obj):
return self.context.get("sponsors")
def get_sponsors(self, obj) -> list[str]:
return self.context.get("sponsors", list[str]()) # type: ignore[no-any-return]

@extend_schema_field(WidgetSerializer(many=True))
def get_widget(self, obj):
Expand Down Expand Up @@ -589,27 +588,21 @@ class Meta:
"batches",
)

@extend_schema_field(int)
def get_plant_count(self, obj):
def get_plant_count(self, obj) -> int:
return random.randint(100, 200) # noqa: S311

@extend_schema_field(int)
def get_survived_count(self, obj):
def get_survived_count(self, obj) -> int:
return random.randint(50, 100) # noqa: S311

@extend_schema_field(int)
def get_propagation_count(self, obj):
def get_propagation_count(self, obj) -> int:
return random.randint(5, 50) # noqa: S311

@extend_schema_field(float)
def get_progress(self, obj):
return random.randint(0, 100) # noqa: S311
def get_progress(self, obj) -> float:
return random.randint(0, 10000) / 100 # noqa: S311

# https://github.com/tfranzel/drf-spectacular/issues/1212
@extend_schema_field(list[str]) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
def get_sponsors(self, obj):
def get_sponsors(self, obj) -> list[str]:
batches = Batch.objects.filter(site=obj)
return [batch.sponsor for batch in batches]
return [batch.sponsor for batch in batches if batch.sponsor]


class CoordinatesMapSerializer(serializers.ModelSerializer[Coordinate]):
Expand All @@ -620,12 +613,10 @@ class Meta:
model = Coordinate
fields = ("latitude", "longitude", "address")

@extend_schema_field(float)
def get_latitude(self, obj):
def get_latitude(self, obj: Coordinate) -> Decimal | None:
return obj.dd_latitude

@extend_schema_field(float)
def get_longitude(self, obj):
def get_longitude(self, obj: Coordinate) -> Decimal | None:
return obj.dd_longitude


Expand Down Expand Up @@ -719,11 +710,10 @@ class Meta:
model = Comment
fields = ("id", "body", "author_id", "author_username", "created_at")

@extend_schema_field(int)
def get_author_id(self, obj):
def get_author_id(self, obj: Comment) -> int:
return obj.user.id

def get_author_username(self, obj):
def get_author_username(self, obj: Comment):
return obj.user.username


Expand Down
8 changes: 4 additions & 4 deletions canopeum_frontend/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3263,8 +3263,8 @@ export interface ICoordinates {
}

export class CoordinatesMap implements ICoordinatesMap {
readonly latitude!: number;
readonly longitude!: number;
readonly latitude!: number | undefined;
readonly longitude!: number | undefined;
address?: string | undefined;

[key: string]: any;
Expand Down Expand Up @@ -3311,8 +3311,8 @@ export class CoordinatesMap implements ICoordinatesMap {
}

export interface ICoordinatesMap {
latitude: number;
longitude: number;
latitude: number | undefined;
longitude: number | undefined;
address?: string | undefined;

[key: string]: any;
Expand Down

0 comments on commit 94157be

Please sign in to comment.