/
page_feedback.py
93 lines (77 loc) · 2.71 KB
/
page_feedback.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
APIv3 endpoint for feedback bout single pages
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from django.http import Http404, JsonResponse
from ....cms.models import PageFeedback
from ...decorators import feedback_handler, json_response
if TYPE_CHECKING:
from ....cms.models import Language, Region
logger = logging.getLogger(__name__)
@feedback_handler
@json_response
def page_feedback(
data: dict[str, str],
region: Region,
language: Language,
comment: str,
rating: bool,
is_technical: bool,
) -> JsonResponse:
"""
Decorate function for storing feedback about single page in database
:param data: HTTP request body data
:param region: The region of this sitemap's urls
:param language: The language of this sitemap's urls
:param comment: The comment sent as feedback
:param rating: up or downvote, neutral
:param is_technical: is feedback on content or on tech
:return: decorated function that saves feedback in database
"""
return page_feedback_internal(data, region, language, comment, rating, is_technical)
def page_feedback_internal(
data: dict[str, str],
region: Region,
language: Language,
comment: str,
rating: bool,
is_technical: bool,
) -> JsonResponse:
"""
Store feedback about single event in database
:param data: HTTP request body data
:param region: The region of this sitemap's urls
:param language: The language of this sitemap's urls
:param comment: The comment sent as feedback
:param rating: up or downvote, neutral
:param is_technical: is feedback on content or on tech
:raises ~django.http.Http404: HTTP status 404 if no page with the given slug exists.
:return: JSON object according to APIv3 single page feedback endpoint definition
"""
page_translation_slug = data.get("slug")
pages = region.pages.filter(
translations__slug=data.get("slug"),
translations__language=language,
).distinct()
if len(pages) > 1:
logger.error(
"Page translation slug %r is not unique per region and language, found multiple: %r",
page_translation_slug,
pages,
)
return JsonResponse({"error": "Internal Server Error"}, status=500)
if not pages:
raise Http404("No matching page found for slug.")
page = pages[0]
page_translation = page.get_translation(language.slug)
PageFeedback.objects.create(
page_translation=page_translation,
region=region,
language=language,
rating=rating,
comment=comment,
is_technical=is_technical,
)
return JsonResponse({"success": "Feedback successfully submitted"}, status=201)