2022-02-14 11:05:35 -05:00
|
|
|
from django.core import mail
|
|
|
|
from django.contrib import admin, messages
|
|
|
|
from django.db.models.functions import Now
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-02-11 13:48:47 -05:00
|
|
|
from .models import (
|
|
|
|
CmsRedRiverVeteransScholarship,
|
|
|
|
CertificationDefinition,
|
|
|
|
Certification,
|
|
|
|
CertificationVersion,
|
|
|
|
InstructorOrVendor,
|
|
|
|
SpecialProgram,
|
|
|
|
Waiver,
|
|
|
|
)
|
2022-02-14 11:05:35 -05:00
|
|
|
from .certification_emails import all_certification_emails
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
2022-02-04 16:30:52 -05:00
|
|
|
class CertificationVersionInline(admin.TabularInline):
|
|
|
|
model = CertificationVersion
|
|
|
|
extra = 1
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(CertificationVersion)
|
|
|
|
class CertificationVersionAdmin(admin.ModelAdmin):
|
2022-02-11 13:48:47 -05:00
|
|
|
search_fields = ["definition__certification_name", "version"]
|
|
|
|
list_display = ["definition", "version"]
|
|
|
|
list_filter = ["definition__department", "definition__certification_name"]
|
2022-02-04 16:30:52 -05:00
|
|
|
|
|
|
|
|
2022-02-03 13:45:58 -05:00
|
|
|
@admin.register(CertificationDefinition)
|
2021-03-23 17:08:00 -04:00
|
|
|
class CertificationDefinitionAdmin(admin.ModelAdmin):
|
2022-02-11 13:48:47 -05:00
|
|
|
search_fields = ["certification_name"]
|
|
|
|
list_display = ["certification_name", "department"]
|
|
|
|
list_filter = ["department"]
|
2022-02-04 16:30:52 -05:00
|
|
|
inlines = [CertificationVersionInline]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
2022-02-03 13:45:58 -05:00
|
|
|
@admin.register(Certification)
|
2021-03-23 17:08:00 -04:00
|
|
|
class CertificationAdmin(admin.ModelAdmin):
|
2022-02-11 13:48:47 -05:00
|
|
|
search_fields = [
|
|
|
|
"name",
|
|
|
|
"certification__certification_name",
|
|
|
|
"certification__department",
|
|
|
|
]
|
|
|
|
autocomplete_fields = ["member"]
|
|
|
|
exclude = ["shop_lead_notified"]
|
2022-02-05 00:28:58 -05:00
|
|
|
|
2022-02-11 13:48:47 -05:00
|
|
|
@admin.display(
|
|
|
|
description="Certification Name",
|
|
|
|
ordering="certification_version__definition__certification_name",
|
|
|
|
)
|
2022-02-05 00:28:58 -05:00
|
|
|
def certification_name(self, obj):
|
|
|
|
return obj.certification_version.definition.certification_name
|
|
|
|
|
2022-02-11 13:48:47 -05:00
|
|
|
@admin.display(
|
|
|
|
description="Certification Version", ordering="certification_version__version"
|
|
|
|
)
|
2022-02-05 00:28:58 -05:00
|
|
|
def certification_version_version(self, obj):
|
|
|
|
return obj.certification_version.version
|
|
|
|
|
2022-02-11 13:48:47 -05:00
|
|
|
@admin.display(
|
|
|
|
description="Department",
|
|
|
|
ordering="certification_version__definition__department",
|
|
|
|
)
|
2022-02-05 00:28:58 -05:00
|
|
|
def certification_department(self, obj):
|
|
|
|
return obj.certification_version.definition.department
|
|
|
|
|
|
|
|
list_display = [
|
2022-02-11 13:48:47 -05:00
|
|
|
"certification_name",
|
|
|
|
"name",
|
|
|
|
"certification_version_version",
|
|
|
|
"certification_department",
|
|
|
|
"date",
|
|
|
|
"shop_lead_notified",
|
|
|
|
"certified_by",
|
2022-02-05 00:28:58 -05:00
|
|
|
]
|
|
|
|
list_display_links = [
|
2022-02-11 13:48:47 -05:00
|
|
|
"certification_name",
|
|
|
|
"name",
|
2022-02-05 00:28:58 -05:00
|
|
|
]
|
|
|
|
list_filter = [
|
2022-02-11 13:48:47 -05:00
|
|
|
"certification_version__definition__department",
|
|
|
|
("shop_lead_notified", admin.EmptyFieldListFilter),
|
2022-02-05 00:28:58 -05:00
|
|
|
]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-02-14 11:05:35 -05:00
|
|
|
actions = ["send_notifications"]
|
|
|
|
|
|
|
|
@admin.action(
|
|
|
|
description="Notify Shop Leads and Members of selected certifications"
|
|
|
|
)
|
|
|
|
def send_notifications(self, request, queryset):
|
|
|
|
try:
|
|
|
|
emails = list(all_certification_emails(queryset))
|
|
|
|
print(emails)
|
|
|
|
|
|
|
|
with mail.get_connection() as conn:
|
|
|
|
conn.send_messages(emails)
|
|
|
|
|
|
|
|
for cert in queryset:
|
|
|
|
cert.update(shop_lead_notified=Now())
|
|
|
|
|
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
f"{len(emails)} notifications sent for {len(queryset)} certifications",
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
f"Failed to send notifications! {e}",
|
|
|
|
messages.ERROR,
|
|
|
|
)
|
|
|
|
raise
|
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-02-03 13:45:58 -05:00
|
|
|
@admin.register(InstructorOrVendor)
|
2021-03-23 17:08:00 -04:00
|
|
|
class InstructorOrVendorAdmin(admin.ModelAdmin):
|
2022-02-11 13:48:47 -05:00
|
|
|
search_fields = ["name"]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
2022-02-03 13:45:58 -05:00
|
|
|
@admin.register(SpecialProgram)
|
2021-03-23 17:08:00 -04:00
|
|
|
class SpecialProgramAdmin(admin.ModelAdmin):
|
2022-02-11 13:48:47 -05:00
|
|
|
search_fields = ["program_name"]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
2022-02-03 13:45:58 -05:00
|
|
|
@admin.register(Waiver)
|
2021-03-23 17:08:00 -04:00
|
|
|
class WaiverAdmin(admin.ModelAdmin):
|
2022-02-11 13:48:47 -05:00
|
|
|
search_fields = ["name"]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(CmsRedRiverVeteransScholarship)
|