/
regions.py
78 lines (63 loc) · 2.44 KB
/
regions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
This module includes functions related to the regions API endpoint.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from django.http import Http404, JsonResponse
if TYPE_CHECKING:
from typing import Any
from django.http import HttpRequest
from ...cms.constants import region_status
from ...cms.models import Region
from ..decorators import json_response
from .languages import transform_language
def transform_region(region: Region) -> dict[str, Any]:
"""
Function to create a JSON from a single region object, including information if region is live/active.
:param region: The region object which should be converted
:return: data necessary for API
"""
return {
"id": region.id,
"name": region.full_name,
"path": region.slug,
"live": region.status == region_status.ACTIVE,
"prefix": region.prefix,
"name_without_prefix": region.name,
"plz": region.postal_code,
"extras": region.offers.exists(),
"events": region.events_enabled,
"pois": region.locations_enabled,
"push_notifications": region.push_notifications_enabled,
"longitude": region.longitude,
"latitude": region.latitude,
"bounding_box": region.bounding_box.api_representation,
"aliases": region.aliases,
"tunews": region.external_news_enabled,
"external_news": region.external_news_enabled,
"languages": list(map(transform_language, region.visible_languages)),
}
@json_response
def regions(_: HttpRequest) -> JsonResponse:
"""
List all regions that are not archived and transform result into JSON
:return: JSON object according to APIv3 regions endpoint definition
"""
result = list(
map(transform_region, Region.objects.exclude(status=region_status.ARCHIVED))
)
return JsonResponse(
result, safe=False
) # Turn off Safe-Mode to allow serializing arrays
@json_response
# pylint: disable=unused-argument
def region_by_slug(request: HttpRequest, region_slug: str) -> JsonResponse:
"""
Retrieve a single region and transform result into JSON
:return: JSON object according to APIv3 live regions endpoint definition
"""
if request.region.status == region_status.ARCHIVED:
raise Http404("This region is archived.")
return JsonResponse(
transform_region(request.region), safe=False
) # Turn off Safe-Mode to allow serializing arrays