1
Fork 0
yamdl-playground/yamdl_playground/core/views.py

26 lines
877 B
Python
Raw Normal View History

from .models import Page
from django.http import HttpResponse
2023-12-08 14:53:17 +00:00
from django.db import connections
2024-03-31 14:20:03 +01:00
from django.shortcuts import get_object_or_404, render
2024-04-15 21:12:55 +01:00
from django.views.decorators.cache import cache_page
2024-08-03 14:02:33 +01:00
from django.utils.safestring import mark_safe
2024-04-15 21:12:55 +01:00
2023-12-12 18:11:44 +00:00
def search(request):
2024-10-05 14:02:40 +01:00
with connections["yamdl"].cursor() as cursor:
2023-12-08 14:53:17 +00:00
cursor.execute("SELECT rowid FROM search_index WHERE search_index = %s;", ["content"])
row = cursor.fetchone()
pages = Page.objects.filter(id__in=row)
return HttpResponse(str(pages), content_type="text/plain")
2023-12-12 18:11:44 +00:00
2023-12-12 18:14:34 +00:00
def content(request, slug):
page = get_object_or_404(Page, slug=slug)
2023-12-12 18:11:44 +00:00
2024-03-31 14:20:03 +01:00
return render(request, "content.html", {
"page": page,
2024-08-03 14:02:33 +01:00
"content": page.render_content({"request": request}) if page.runtime_render else mark_safe(page.content)
2024-03-31 14:20:03 +01:00
})
2024-04-15 21:12:55 +01:00
cached_content = cache_page(600, cache="mem")(content)