from django.shortcuts import render
from django.urls import path

from .models import AdditionalPage

# Create your views here.


def template(filename, ec={}):
    def closure(request):
        return render(request, filename + ".html", ec)

    return closure


def reverse_template(filename, model, ec={}):
    def closure(request, id):
        return render(
            request,
            filename + "-reverse.html",
            {**ec, "focus": model.objects.get(pk=id)},
        )

    return closure


def tpath(p: str, filename: str, ec={}):
    return path(p, template(filename, ec), name=filename.replace("/", "_"))


def rtpath(p: str, filename: str, model, ec={}):
    return path(
        p,
        reverse_template(filename, model, ec),
        name=filename.replace("/", "_") + "-reverse",
    )


def wildcard_additional_page(request, wildcard):
    return render(
        request,
        "page.html",
        context={
            "page": wildcard,
            "posts": posts.filter(
                published=True
            ) if (posts := AdditionalPage.objects.get(slug=wildcard).posts) is not None else None,
        },
    )
