-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
77 lines (64 loc) · 2.42 KB
/
Copy pathviews.py
File metadata and controls
77 lines (64 loc) · 2.42 KB
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
"""Views for the timezone_converter app."""
from datetime import datetime
from typing import TYPE_CHECKING
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from django.shortcuts import render
if TYPE_CHECKING:
from django.http import HttpRequest, HttpResponse
# A simple view to display the converter HTML template.
def converter(request: HttpRequest) -> HttpResponse:
"""Display the converter HTML template."""
return render(
request,
"timezone_converter/converter.html",
)
# A view to receive a timestamp and timezone POSTed from the converter form and return
# the converted time using a template.
def convert(request: HttpRequest) -> HttpResponse:
"""Convert a timestamp to a different timezone and display the result."""
# Get the timestamp and timezone from the POST request.
timestamp: str | None = request.POST.get("timestamp")
timezone: str | None = request.POST.get("timezone")
error_message: str = ""
if not timezone or not timestamp:
error_message = "Please provide a timestamp and timezone."
return render(
request,
"timezone_converter/converted.html",
{"error_message": error_message},
)
# Convert the timestamp to a datetime object.
try:
datetime_obj: datetime = datetime.fromisoformat(timestamp)
except ValueError:
error_message = "Invalid timestamp. Please use the ISO 8601 format."
return render(
request,
"timezone_converter/converted.html",
{"error_message": error_message},
)
# Check the converted timestamp is timezone aware
if datetime_obj.tzinfo is None:
error_message = (
"The timestamp provided must be timezone aware. For example, use 'Z' for UTC, or provide an offset."
)
return render(
request,
"timezone_converter/converted.html",
{"error_message": error_message},
)
# Check that the timezone is valid.
try:
ZoneInfo(timezone)
except ZoneInfoNotFoundError:
error_message = "Invalid timezone. Please use a valid timezone."
return render(
request,
"timezone_converter/converted.html",
{"error_message": error_message},
)
return render(
request,
"timezone_converter/converted.html",
{"timestamp": datetime_obj, "timezone": timezone},
)