Skip to content

Commit

Permalink
Allow views to return an int for status code, error template not requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
davegaeddert committed Aug 1, 2024
1 parent b880d87 commit 3b62bdf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions plain/plain/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ def get_response(self) -> ResponseBase:
if isinstance(result, ResponseBase):
return result

# Allow return of an int (status code)
# or tuple (status code, content)?

if isinstance(result, str):
return Response(result)

Expand All @@ -84,6 +81,11 @@ def get_response(self) -> ResponseBase:
if isinstance(result, dict):
return JsonResponse(result)

if isinstance(result, int):
return Response(status=result)

# Allow tuple for (status_code, content)?

raise ValueError(f"Unexpected view return type: {type(result)}")

def options(self) -> Response:
Expand Down
7 changes: 7 additions & 0 deletions plain/plain/views/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from plain.http import ResponseBase
from plain.templates import TemplateFileMissing

from .templates import TemplateView

Expand All @@ -23,3 +24,9 @@ def get_response(self) -> ResponseBase:
# Set the status code we want
response.status_code = self.status_code
return response

def get(self):
try:
return super().get()
except TemplateFileMissing:
return self.status_code

0 comments on commit 3b62bdf

Please sign in to comment.