2018-07-02 19:19:50 -04:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.urls import resolve, Resolver404
|
2018-07-02 20:03:35 -04:00
|
|
|
from micropub import error
|
2018-07-02 19:19:50 -04:00
|
|
|
|
|
|
|
from .models import Entry
|
|
|
|
|
|
|
|
|
2018-07-02 19:41:00 -04:00
|
|
|
def from_url(url: str) -> Entry:
|
2018-07-02 19:19:50 -04:00
|
|
|
domain = Site.objects.get_current().domain
|
|
|
|
if not url:
|
2023-08-10 02:52:37 -04:00
|
|
|
raise 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:
|
|
|
|
raise error.bad_req("url does not point to this site")
|
2018-07-02 19:19:50 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
match = resolve(parts.path)
|
|
|
|
except Resolver404:
|
2023-08-10 02:52:37 -04:00
|
|
|
raise error.bad_req("url does not point to a valid page on this site")
|
2018-07-02 19:19:50 -04:00
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
if match.view_name != "entries:entry":
|
|
|
|
raise error.bad_req("url does not point to an entry on this site")
|
2018-07-02 19:19:50 -04:00
|
|
|
|
|
|
|
try:
|
2023-08-10 02:52:37 -04:00
|
|
|
entry = Entry.objects.get(pk=match.kwargs["id"])
|
2018-07-02 19:19:50 -04:00
|
|
|
except Entry.DoesNotExist:
|
2023-08-10 02:52:37 -04:00
|
|
|
raise error.bad_req("url does not point to an existing entry")
|
2018-07-02 19:19:50 -04:00
|
|
|
|
|
|
|
return entry
|