phantasmobile/app/views.py

72 lines
2.9 KiB
Python
Raw Permalink Normal View History

2023-08-14 11:56:22 +00:00
import xml.etree.ElementTree as ET
2023-09-23 15:27:24 +00:00
from django.conf import settings
2023-07-25 19:06:14 +00:00
from django.http import HttpResponse
2023-09-23 15:27:24 +00:00
from django.shortcuts import render, redirect
from django.utils.timezone import datetime, timedelta
2023-07-25 19:06:14 +00:00
def robots_txt(request):
txt = '''User-agent: *
Disallow:
Sitemap: {}
'''.format(request.build_absolute_uri('/sitemap.xml'))
return HttpResponse(txt, 'text/plain')
def sitemap_xml(request):
2023-08-14 11:56:22 +00:00
now = datetime.now()
from .item.models import Item
urlset = ET.Element('urlset')
urlset.attrib['xmlns'] = "http://www.sitemaps.org/schemas/sitemap/0.9"
urlset.attrib['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
urlset.attrib['xsi:schemaLocation'] = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
url = ET.SubElement(urlset, "url")
loc = ET.SubElement(url, "loc")
loc.text = request.build_absolute_uri('/')
lastmod = ET.SubElement(url, "lastmod")
lastmod.text = now.strftime("%Y-%m-%d")
changefreq = ET.SubElement(url, "changefreq")
changefreq.text = 'hourly'
priority = ET.SubElement(url, "priority")
priority.text = '1.0'
first = Item.objects.exclude(published=None).exclude(published__gt=now).order_by('published').first()
if first:
for year in reversed(range(first.published.year, now.year + 1)):
url = ET.SubElement(urlset, "url")
loc = ET.SubElement(url, "loc")
loc.text = request.build_absolute_uri('/_%s/' % year)
lastmod = ET.SubElement(url, "lastmod")
changefreq = ET.SubElement(url, "changefreq")
priority = ET.SubElement(url, "priority")
if year == now.year:
lastmod.text = now.strftime("%Y-%m-%d")
changefreq.text = 'weekly'
priority.text = '1.0'
else:
lastmod.text = now.strftime("%s-12-31" % year)
changefreq.text = 'yearly'
priority.text = '0.8'
2023-08-14 11:56:22 +00:00
for item in Item.objects.exclude(published=None).exclude(published__gt=now).order_by('-published'):
url = ET.SubElement(urlset, "url")
loc = ET.SubElement(url, "loc")
loc.text = request.build_absolute_uri(item.get_absolute_url())
# This date should be in W3C Datetime format, can be %Y-%m-%d
lastmod = ET.SubElement(url, "lastmod")
lastmod.text = item.modified.strftime("%Y-%m-%d")
# always, hourly, daily, weekly, monthly, yearly, never
changefreq = ET.SubElement(url, "changefreq")
changefreq.text = 'daily'
# priority of page on site values 0.1 - 1.0
priority = ET.SubElement(url, "priority")
priority.text = '0.9'
data = b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset)
return HttpResponse(data, 'application/xml')
2023-07-25 19:06:14 +00:00
2023-09-23 15:27:24 +00:00
def telegram(request):
return redirect(settings.TELEGRAM_CHANNEL)