Introduce a middleware that allows for HttpResponses to be thrown from inner utility functions, to avoid boilerplate in views
This commit is contained in:
parent
1d4be082cf
commit
7d17a92793
4 changed files with 22 additions and 9 deletions
|
@ -1,35 +1,35 @@
|
|||
from typing import Union
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from django.contrib.sites.models import Site
|
||||
from django.http import HttpResponse
|
||||
from django.urls import resolve, Resolver404
|
||||
from micropub.views import error
|
||||
from lemoncurry.middleware import ResponseException
|
||||
|
||||
from .models import Entry
|
||||
|
||||
|
||||
def from_url(url: str) -> Union[Entry, HttpResponse]:
|
||||
def from_url(url: str) -> Entry:
|
||||
domain = Site.objects.get_current().domain
|
||||
if not url:
|
||||
return error.bad_req('url parameter required')
|
||||
raise ResponseException(error.bad_req('url parameter required'))
|
||||
if '//' not in url:
|
||||
url = '//' + url
|
||||
parts = urlparse(url, scheme='https')
|
||||
if parts.scheme not in ('http', 'https') or parts.netloc != domain:
|
||||
return error.bad_req('url does not point to this site')
|
||||
raise ResponseException(error.bad_req('url does not point to this site'))
|
||||
|
||||
try:
|
||||
match = resolve(parts.path)
|
||||
except Resolver404:
|
||||
return error.bad_req('url does not point to a valid page on this site')
|
||||
raise ResponseException(error.bad_req('url does not point to a valid page on this site'))
|
||||
|
||||
if match.view_name != 'entries:entry':
|
||||
return error.bad_req('url does not point to an entry on this site')
|
||||
raise ResponseException(error.bad_req('url does not point to an entry on this site'))
|
||||
|
||||
try:
|
||||
entry = Entry.objects.get(pk=match.kwargs['id'])
|
||||
except Entry.DoesNotExist:
|
||||
return error.bad_req('url does not point to an existing entry')
|
||||
raise ResponseException(error.bad_req('url does not point to an existing entry'))
|
||||
|
||||
return entry
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue