Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for datetime,date,time converters #1398

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions starlette/convertors.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,43 @@ def to_string(self, value: typing.Any) -> str:
return str(value)


class DateTimeConvertor(Convertor):
regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]+)?"

def convert(self, value: str) -> typing.Any:
return str(value)

def to_string(self, value: typing.Any) -> str:
return value.strftime("%Y-%m-%dT%H:%M:%S")


class DateConvertor(Convertor):
regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}"

def convert(self, value: str) -> typing.Any:
return str(value)

def to_string(self, value: typing.Any) -> str:
return value.strftime("%Y-%m-%d")


class TimeConvertor(Convertor):
regex = "[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]+)?"

def convert(self, value: str) -> typing.Any:
return str(value)

def to_string(self, value: typing.Any) -> str:
return value.strftime("%H:%M:%S")


CONVERTOR_TYPES = {
"str": StringConvertor(),
"path": PathConvertor(),
"int": IntegerConvertor(),
"float": FloatConvertor(),
"uuid": UUIDConvertor(),
"datetime": DateTimeConvertor(),
"date": DateConvertor(),
"time": TimeConvertor(),
}
48 changes: 48 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import functools
import uuid

Expand Down Expand Up @@ -120,6 +121,24 @@ def uuid_converter(request):
return JSONResponse({"uuid": str(uuid_param)})


@app.route("/datetime/{param:datetime}", name="datetime-convertor")
def datetime_convertor(request):
dt = request.path_params["param"]
return JSONResponse({"datetime": dt})


@app.route("/date/{param:date}", name="date-convertor")
def date_convertor(request):
date = request.path_params["param"]
return JSONResponse({"date": date})


@app.route("/time/{param:time}", name="time-convertor")
def time_convertor(request):
time = request.path_params["param"]
return JSONResponse({"time": time})


# Route with chars that conflict with regex meta chars
@app.route("/path-with-parentheses({param:int})", name="path-with-parentheses")
def path_with_parentheses(request):
Expand Down Expand Up @@ -233,6 +252,35 @@ def test_route_converters(client):
== "/uuid/ec38df32-ceda-4cfa-9b4a-1aeb94ad551a"
)

# Test datetime conversion
response = client.get("/datetime/2020-01-01T00:00:00")
assert response.status_code == 200
assert response.json() == {"datetime": "2020-01-01T00:00:00"}
assert (
app.url_path_for(
"datetime-convertor", param=datetime.datetime(2020, 1, 1, 0, 0, 0)
)
== "/datetime/2020-01-01T00:00:00"
)

# Test date conversion
response = client.get("/date/2020-01-01")
assert response.status_code == 200
assert response.json() == {"date": "2020-01-01"}
assert (
app.url_path_for("date-convertor", param=datetime.date(2020, 1, 1))
== "/date/2020-01-01"
)

# Test time conversion
response = client.get("/time/00:00:00")
assert response.status_code == 200
assert response.json() == {"time": "00:00:00"}
assert (
app.url_path_for("time-convertor", param=datetime.time(0, 0, 0))
== "/time/00:00:00"
)


def test_url_path_for():
assert app.url_path_for("homepage") == "/"
Expand Down