Skip to content

Commit

Permalink
Fix api (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Theo Auffeuvre <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 24, 2024
1 parent 79b59f2 commit 8a5f88f
Show file tree
Hide file tree
Showing 6 changed files with 2,565 additions and 2,636 deletions.
18 changes: 18 additions & 0 deletions canopeum_backend/canopeum_backend/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Meta:
model = Site
fields = "__all__"

@extend_schema_field(SitetreespeciesSerializer(many=True))
def get_site_tree_species(self, obj):
return SitetreespeciesSerializer(obj.sitetreespecies_set.all(), many=True).data

Expand Down Expand Up @@ -258,30 +259,39 @@ class Meta:
"species",
)

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_plant_count(self, obj):
return self.context.get("plant_count")

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_survived_count(self, obj):
return self.context.get("survived_count")

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_replace_count(self, obj):
return self.context.get("replace_count")

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_seed_collected_count(self, obj):
return self.context.get("seed_collected_count")

@extend_schema_field(BatchfertilizerSerializer(many=True))
def get_fertilizers(self, obj):
return BatchfertilizerSerializer(obj.batchfertilizer_set.all(), many=True).data

@extend_schema_field(BatchMulchLayerSerializer(many=True))
def get_mulch_layers(self, obj):
return BatchMulchLayerSerializer(obj.batchmulchlayer_set.all(), many=True).data

@extend_schema_field(BatchSupportedSpeciesSerializer(many=True))
def get_supported_species(self, obj):
return BatchSupportedSpeciesSerializer(obj.batchsupportedspecies_set.all(), many=True).data

@extend_schema_field(BatchSeedSerializer(many=True))
def get_seeds(self, obj):
return BatchSeedSerializer(obj.batchseed_set.all(), many=True).data

@extend_schema_field(BatchSpeciesSerializer(many=True))
def get_species(self, obj):
return BatchSpeciesSerializer(obj.batchspecies_set.all(), many=True).data

Expand All @@ -307,18 +317,23 @@ class Meta:
"progress",
)

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_plant_count(self, obj):
return self.context.get("plant_count")

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_survived_count(self, obj):
return self.context.get("survived_count")

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_propagation_count(self, obj):
return self.context.get("propagation_count")

@extend_schema_field(float) # pyright: ignore[reportArgumentType]
def get_progress(self, obj):
return self.context.get("progress")

@extend_schema_field(BatchSerializer(many=True))
def get_sponsor(self, obj):
return BatchSerializer(obj).data.get("sponsor", None)

Expand Down Expand Up @@ -346,6 +361,7 @@ class Meta:
model = Site
fields = ("id", "name", "site_type", "coordinates", "image")

@extend_schema_field(CoordinatesMapSerializer)
def get_coordinates(self, obj):
return CoordinatesMapSerializer(obj.coordinate).data

Expand All @@ -365,9 +381,11 @@ class Meta:
model = Post
fields = ("id", "site", "created_at", "body", "like_count", "share_count", "comment_count", "has_liked")

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_comment_count(self, obj):
return self.context.get("comment_count")

@extend_schema_field(bool) # pyright: ignore[reportArgumentType]
def get_has_liked(self, obj):
return self.context.get("has_liked")

Expand Down
4 changes: 2 additions & 2 deletions canopeum_backend/canopeum_backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import Announcement, Batch, Comment, Contact, Post, Site, Widget
from .models import Announcement, Batch, Comment, Contact, Like, Post, Site, Widget
from .serializers import (
AnnouncementSerializer,
AuthUserSerializer,
Expand Down Expand Up @@ -193,7 +193,7 @@ class PostListAPIView(APIView):
@extend_schema(responses=PostSerializer(many=True), operation_id="post_all")
def get(self, request):
comment_count = Comment.objects.filter(post=request.data.get("id")).count()
has_liked = 0
has_liked = Like.objects.filter(post=request.data.get("id"), user=request.user).exists()
site_id = request.GET.get("siteId", "")
posts = Post.objects.filter(site=site_id) if not site_id else Post.objects.all()
serializer = PostSerializer(posts, many=True, context={"comment_count": comment_count, "has_liked": has_liked})
Expand Down
4 changes: 2 additions & 2 deletions canopeum_frontend/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const Home = () => {
const fetchData = async () => {
setIsLoading(true)
try {
const response = await api().analytics.batches()
setData(response)
const response = await api().analytics.batches();
setData(response);
} catch (error_: unknown) {
setError(ensureError(error_))
} finally {
Expand Down
Loading

0 comments on commit 8a5f88f

Please sign in to comment.