Skip to content

Commit

Permalink
Add image and reivew support for legacy api
Browse files Browse the repository at this point in the history
  • Loading branch information
ces92 committed Jan 10, 2025
1 parent e53cbcb commit f7de89e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 25 additions & 1 deletion backend/bvdata/data/api_legacy/serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from datetime import time
from typing import Dict, List, Optional

from django.db.models import QuerySet
from django.http import HttpRequest

from bvdata.data.models import (
BaseLocation,
BooleanAttribute,
OpeningHours,
PositiveIntegerAttribute,
ReviewImage,
WeekdayChoices,
)

Expand Down Expand Up @@ -143,6 +147,7 @@ def _build_base(location: BaseLocation, data_to_field_name: dict) -> dict:

if location.review:
base_dict.update(reviewURL=location.review.url.split("/")[-2])
base_dict.update(review=location.review.text)

base_dict.update(
_build_opening_hours(
Expand Down Expand Up @@ -172,7 +177,25 @@ def _build_positive_int_attributes(
}


def build_gastro(location: BaseLocation) -> dict:
def _build_images(location: BaseLocation, request: HttpRequest) -> List[dict]:
images_list = []
if location.review:
images_list = [
dict(url=image.url, width=image.width, height=image.height)
for image in location.review.reviewimage_set.all()
]
images_list = images_list + [
dict(
url=request.build_absolute_uri(image.image.url),
width=image.width,
height=image.height,
)
for image in location.image_set.all()
]
return images_list


def build_gastro(location: BaseLocation, request: HttpRequest) -> dict:
gastro_dict = _build_base(
location=location, data_to_field_name=GASTRO_DATA_FIELD_NAME_TO_FIELD_NAME
)
Expand All @@ -181,6 +204,7 @@ def build_gastro(location: BaseLocation) -> dict:
gastro_dict.update(
_build_positive_int_attributes(location.positive_integer_attributes.all())
)
gastro_dict.update(picture=_build_images(location, request))
return gastro_dict


Expand Down
4 changes: 3 additions & 1 deletion backend/bvdata/data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def get_serializer(self):
return self.serializer

def get(self, request, *args, **kwargs):
results = [self.serializer(location) for location in self.get_queryset()]
results = [
self.serializer(location, request) for location in self.get_queryset()
]
return HttpResponse(json.dumps(results), content_type="application/json")


Expand Down

0 comments on commit f7de89e

Please sign in to comment.