Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/3.6.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreJunod committed May 22, 2024
2 parents 9560557 + 10f9cba commit a5737cd
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions geocity/apps/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import mimetypes
import os
from datetime import datetime, timedelta, timezone

import requests
from django.conf import settings
Expand Down Expand Up @@ -65,13 +65,13 @@ def get_queryset(self):

base_filter = Q()
if starts_at:
start = datetime.datetime.strptime(starts_at, "%Y-%m-%d")
start = datetime.strptime(starts_at, "%Y-%m-%d")
base_filter &= Q(starts_at__gte=start)
if ends_at:
end = datetime.datetime.strptime(ends_at, "%Y-%m-%d")
end = datetime.strptime(ends_at, "%Y-%m-%d")
base_filter &= Q(ends_at__lte=end)
if show_only_future == "true":
base_filter &= Q(ends_at__gte=datetime.datetime.now())
base_filter &= Q(ends_at__gte=datetime.now())
if administrative_entities:
base_filter &= Q(
submission__administrative_entity__in=administrative_entities.split(",")
Expand All @@ -90,7 +90,7 @@ def get_queryset(self):
).select_related("category"),
)

today = datetime.datetime.today()
today = datetime.today()
current_inquiry_prefetch = Prefetch(
"submission__inquiries",
queryset=SubmissionInquiry.objects.filter(
Expand Down Expand Up @@ -241,7 +241,7 @@ def get_queryset(self, geom_type=None):
queryset=Form.objects.select_related("category"),
)

today = datetime.datetime.today()
today = datetime.today()
current_inquiry_prefetch = Prefetch(
"inquiries",
queryset=SubmissionInquiry.objects.filter(
Expand Down Expand Up @@ -485,6 +485,18 @@ def get_queryset(self):
SubmissionPolyViewSet = submission_view_set_subset_factory("polygons")


class CustomFileResponse(FileResponse):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Add header Expires
expiration_date = datetime.now() + timedelta(days=7)
expiration_date_http_format = expiration_date.strftime(
"%a, %d %b %Y %H:%M:%S GMT"
)
self["Expires"] = expiration_date_http_format


def image_display(request, submission_id, image_name):
safe_submission_id = get_valid_filename(submission_id)
safe_image_name = get_valid_filename(image_name)
Expand All @@ -496,7 +508,7 @@ def image_display(request, submission_id, image_name):
):
image_file = open(image_path, "rb")
mime_type, encoding = mimetypes.guess_type(image_path)
response = FileResponse(image_file, content_type=mime_type)
response = CustomFileResponse(image_file, content_type=mime_type)
return response
else:
return JsonResponse({"message": "unauthorized."}, status=404)
Expand Down Expand Up @@ -541,7 +553,7 @@ def image_thumbor_display(request, submission_id, image_name):
response = requests.get(image_url)

mime_type = get_mime_type(response.content)
thumbor_response = FileResponse(response, content_type=mime_type)
thumbor_response = CustomFileResponse(response, content_type=mime_type)

return thumbor_response

Expand Down Expand Up @@ -625,21 +637,19 @@ def get_queryset(self):
submissions = get_agenda_submissions(entities, submissions)

if "starts_at" in query_params:
starts_at = datetime.datetime.strptime(
query_params["starts_at"], "%Y-%m-%d"
)
starts_at = starts_at.replace(tzinfo=datetime.timezone.utc)
starts_at = datetime.strptime(query_params["starts_at"], "%Y-%m-%d")
starts_at = starts_at.replace(tzinfo=timezone.utc)
submissions = submissions.filter(geo_time__ends_at__gte=starts_at)

if "ends_at" in query_params:
ends_at = datetime.datetime.strptime(query_params["ends_at"], "%Y-%m-%d")
ends_at = datetime.strptime(query_params["ends_at"], "%Y-%m-%d")
ends_at = ends_at.replace(
hour=23, minute=59, second=59, tzinfo=datetime.timezone.utc
hour=23, minute=59, second=59, tzinfo=timezone.utc
)
submissions = submissions.filter(geo_time__starts_at__lte=ends_at)

if "starts_at" not in query_params and "ends_at" not in query_params:
today = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
today = datetime.now(timezone.utc) + timedelta(
hours=settings.LOCAL_TIME_ZONE_UTC
)
submissions = submissions.filter(geo_time__ends_at__gte=today)
Expand Down

0 comments on commit a5737cd

Please sign in to comment.