iamkonstantin/base/wagtail_hooks.py

30 lines
996 B
Python

from django.conf import settings
from wagtail import hooks
from wagtail.models import Page
from urllib.parse import urlparse
from base.indexnow import get_key
import requests
from blog.models import BlogPage
@hooks.register('after_publish_page')
def after_publish_page(request, page):
page_url = page.full_url
# avoid if the host is localhost (development)
print("preparing to notify")
if urlparse(page_url).hostname == "localhost":
print("not notifying indexnow for localhost" + get_key() + ", page url: " + page_url)
return
if urlparse(page_url).path.endswith("--priv") or urlparse(page_url).path.endswith("--priv/"):
print("not notifying indexnow for blog page --priv")
return
session = requests.Session()
session.post(
"https://api.indexnow.org/indexnow",
json={
"host": urlparse(page_url).hostname,
"urlList": [page_url],
"key": get_key(),
},
).raise_for_status()