from django.contrib.auth.decorators import login_required, permission_required
from django.core.mail import mail_admins
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _

from ..models import Event


def page_not_found(request, exception=None):
    return render(request, "page-erreur.html", status=404, context={"error": 404})


def internal_server_error(request):
    try:
        mail_admins(
            request.site.name + _(": error 500"),
            _("An internal error has occurred on site {} at address {}.").format(
                request.site.name, request.build_absolute_uri()
            ),
        )
    except Exception:
        pass
    return render(request, "page-erreur.html", status=500, context={"error": 500})


@login_required(login_url="/accounts/login/")
@permission_required("agenda_culturel.change_event")
def error_next_event(request, pk):
    obj = Event.objects.filter(pk=pk).first()

    return render(
        request,
        "agenda_culturel/event_next_error_message.html",
        {"pk": pk, "object": obj},
    )
