from django.contrib import admin
from django.template.response import TemplateResponse
from django.urls import path

import django_rq

from .models import EmailTemplate


scheduler = django_rq.get_scheduler('default')


class EmailTemplateAdmin(admin.ModelAdmin):
    list_display = ['slug', 'subject', 'to_header', 'from_header']

    def get_urls(self):
        original_urls = super().get_urls()
        new_urls = [
            path('queue/', self.admin_site.admin_view(self.email_queue_view),
                 name='autoemails_emailtemplate_queue'),
        ]
        return new_urls + original_urls

    def email_queue_view(self, request):
        jobs = scheduler.get_jobs()
        context = dict(
            self.admin_site.each_context(request),
            title="Queue",
            cl=self.get_changelist_instance(request),
            queue=jobs,
        )
        return TemplateResponse(request, "queue.html", context)


admin.site.register(EmailTemplate, EmailTemplateAdmin)

@@ -161,16 +161,25 @@
 Template
+, Trigger
 %0A%0A%0Asched
@@ -941,16 +941,122 @@
 text)%0A%0A%0A
+class TriggerAdmin(admin.ModelAdmin):%0A    list_display = %5B'active', 'created_at', 'action', 'template'%5D%0A%0A%0A
 admin.si
@@ -1102,8 +1102,51 @@
 eAdmin)%0A
+admin.site.register(Trigger, TriggerAdmin)%0A

add Trigger to Django Admin panel