2022-02-14 11:05:35 -05:00
|
|
|
from django.core import mail
|
|
|
|
from django.contrib import admin, messages
|
2023-01-31 18:53:36 -05:00
|
|
|
from django.db.models.functions import Now, Concat, LPad
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-02-11 13:48:47 -05:00
|
|
|
from .models import (
|
|
|
|
CmsRedRiverVeteransScholarship,
|
2023-01-18 21:29:36 -05:00
|
|
|
Department,
|
2022-02-11 13:48:47 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
2023-01-18 21:29:36 -05:00
|
|
|
@admin.register(Department)
|
|
|
|
class Department(admin.ModelAdmin):
|
|
|
|
search_fields = ["name"]
|
2023-01-19 16:02:17 -05:00
|
|
|
list_display = [
|
|
|
|
"name",
|
|
|
|
"parent",
|
|
|
|
"has_mailing_list",
|
2023-01-23 21:06:06 -05:00
|
|
|
"shop_lead_flag",
|
2023-01-19 16:02:17 -05:00
|
|
|
"list_reply_to_address",
|
|
|
|
]
|
2023-01-18 21:29:36 -05:00
|
|
|
|
|
|
|
|
2022-02-04 16:30:52 -05:00
|
|
|
class CertificationVersionInline(admin.TabularInline):
|
|
|
|
model = CertificationVersion
|
|
|
|
extra = 1
|
|
|
|
|
2022-11-07 13:49:39 -05:00
|
|
|
readonly_fields = (
|
|
|
|
"semantic_version",
|
|
|
|
"is_latest",
|
|
|
|
"is_current",
|
|
|
|
)
|
|
|
|
|
2023-02-01 00:11:28 -05:00
|
|
|
@admin.display(description="Latest", boolean=True)
|
|
|
|
def is_latest(self, obj):
|
|
|
|
return obj.is_latest
|
|
|
|
|
|
|
|
@admin.display(description="Current", boolean=True)
|
|
|
|
def is_current(self, obj):
|
|
|
|
return obj.is_current
|
|
|
|
|
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"]
|
2022-11-07 13:49:39 -05:00
|
|
|
list_display = [
|
|
|
|
"certification_name",
|
|
|
|
"department",
|
2023-01-31 18:53:36 -05:00
|
|
|
"latest_semantic_version",
|
2022-11-07 13:49:39 -05:00
|
|
|
]
|
2022-02-11 13:48:47 -05:00
|
|
|
list_filter = ["department"]
|
2022-02-04 16:30:52 -05:00
|
|
|
inlines = [CertificationVersionInline]
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-11-07 13:49:39 -05:00
|
|
|
@admin.display(description="Latest Version")
|
2023-01-31 18:53:36 -05:00
|
|
|
def latest_semantic_version(self, obj):
|
|
|
|
return obj.latest_version().semantic_version()
|
2022-11-07 13:49:39 -05:00
|
|
|
|
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",
|
2022-02-24 18:41:53 -05:00
|
|
|
"certification_version__definition__certification_name",
|
|
|
|
"certification_version__definition__department",
|
2022-02-11 13:48:47 -05:00
|
|
|
]
|
|
|
|
autocomplete_fields = ["member"]
|
|
|
|
exclude = ["shop_lead_notified"]
|
2022-02-05 00:28:58 -05:00
|
|
|
|
2023-02-01 00:11:28 -05:00
|
|
|
def get_queryset(self, request):
|
|
|
|
qs = super().get_queryset(request)
|
|
|
|
return qs.prefetch_related("certification_version__definition__department")
|
|
|
|
|
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(
|
2023-01-31 18:53:36 -05:00
|
|
|
description="Certification Version",
|
|
|
|
ordering=(
|
|
|
|
Concat(
|
|
|
|
LPad("certification_version__major", 4, 0),
|
|
|
|
LPad("certification_version__minor", 4, 0),
|
|
|
|
LPad("certification_version__patch", 4, 0),
|
|
|
|
"certification_version__prerelease",
|
|
|
|
"certification_version__approval_date",
|
|
|
|
)
|
|
|
|
),
|
2022-02-11 13:48:47 -05:00
|
|
|
)
|
2023-01-31 18:53:36 -05:00
|
|
|
def certification_semantic_version(self, obj):
|
|
|
|
return obj.certification_version.semantic_version()
|
2022-02-05 00:28:58 -05:00
|
|
|
|
2022-11-07 13:49:39 -05:00
|
|
|
@admin.display(description="Current", boolean=True)
|
2023-02-01 00:11:28 -05:00
|
|
|
def is_current(self, obj):
|
|
|
|
return obj.certification_version.is_current
|
2022-11-07 13:49:39 -05:00
|
|
|
|
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",
|
2023-01-31 18:53:36 -05:00
|
|
|
"certification_semantic_version",
|
2023-02-01 00:11:28 -05:00
|
|
|
"is_current",
|
2022-02-11 13:48:47 -05:00
|
|
|
"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))
|
|
|
|
|
|
|
|
with mail.get_connection() as conn:
|
|
|
|
conn.send_messages(emails)
|
|
|
|
|
|
|
|
for cert in queryset:
|
2022-02-24 21:59:16 -05:00
|
|
|
cert.shop_lead_notified = Now()
|
|
|
|
cert.save()
|
2022-02-14 11:05:35 -05:00
|
|
|
|
|
|
|
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"]
|
2022-07-21 18:49:24 -04:00
|
|
|
list_display = [
|
|
|
|
"name",
|
|
|
|
"instructor_agreement_date",
|
|
|
|
"w9_date",
|
|
|
|
"phone",
|
|
|
|
"email_address",
|
|
|
|
]
|
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"]
|
2022-07-21 18:49:24 -04:00
|
|
|
list_display = [
|
|
|
|
"program_name",
|
|
|
|
"discount_percent",
|
|
|
|
"discount_code",
|
|
|
|
"membership_code",
|
|
|
|
"start_date",
|
|
|
|
"end_date",
|
|
|
|
"program_amount",
|
|
|
|
"program_status",
|
|
|
|
]
|
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
|
|
|
|
2022-07-21 18:49:24 -04:00
|
|
|
list_display = [
|
|
|
|
"name",
|
|
|
|
"date",
|
|
|
|
"emergency_contact_name",
|
|
|
|
"emergency_contact_number",
|
|
|
|
"waiver_version",
|
|
|
|
"guardian_name",
|
|
|
|
"guardian_relation",
|
|
|
|
"guardian_date",
|
|
|
|
]
|
|
|
|
|
2021-03-23 17:08:00 -04:00
|
|
|
|
|
|
|
admin.site.register(CmsRedRiverVeteransScholarship)
|