cmsmanage/paperwork/admin.py

206 lines
5.5 KiB
Python
Raw Normal View History

from django.core import mail
from django.contrib import admin, messages
2023-02-02 22:33:13 -05:00
from django.db.models import Value
from django.db.models.functions import Now, Concat, LPad
2022-02-11 13:48:47 -05:00
from .models import (
CmsRedRiverVeteransScholarship,
Department,
2022-02-11 13:48:47 -05:00
CertificationDefinition,
Certification,
CertificationVersion,
InstructorOrVendor,
SpecialProgram,
Waiver,
)
from .certification_emails import all_certification_emails
@admin.register(Department)
2023-02-02 22:33:13 -05:00
class DepartmentAdmin(admin.ModelAdmin):
search_fields = ["name"]
list_display = [
"name",
"parent",
"has_mailing_list",
"shop_lead_flag",
"list_reply_to_address",
]
class CertificationVersionInline(admin.TabularInline):
model = CertificationVersion
extra = 1
readonly_fields = (
"semantic_version",
"is_latest",
"is_current",
)
@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
@admin.register(CertificationDefinition)
class CertificationDefinitionAdmin(admin.ModelAdmin):
2022-02-11 13:48:47 -05:00
search_fields = ["certification_name"]
list_display = [
"certification_name",
"department",
"latest_semantic_version",
]
2022-02-11 13:48:47 -05:00
list_filter = ["department"]
inlines = [CertificationVersionInline]
@admin.display(description="Latest Version")
def latest_semantic_version(self, obj):
return obj.latest_version().semantic_version()
@admin.register(Certification)
class CertificationAdmin(admin.ModelAdmin):
2022-02-11 13:48:47 -05:00
search_fields = [
"name",
"certification_version__definition__certification_name",
"certification_version__definition__department",
2022-02-11 13:48:47 -05:00
]
autocomplete_fields = ["member"]
exclude = ["shop_lead_notified"]
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",
)
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=(
Concat(
2023-02-02 22:33:13 -05:00
LPad("certification_version__major", 4, Value("0")),
LPad("certification_version__minor", 4, Value("0")),
LPad("certification_version__patch", 4, Value("0")),
"certification_version__prerelease",
"certification_version__approval_date",
)
),
2022-02-11 13:48:47 -05:00
)
def certification_semantic_version(self, obj):
return obj.certification_version.semantic_version()
@admin.display(description="Current", boolean=True)
def is_current(self, obj):
return obj.certification_version.is_current
2022-02-11 13:48:47 -05:00
@admin.display(
description="Department",
ordering="certification_version__definition__department",
)
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_semantic_version",
"is_current",
2022-02-11 13:48:47 -05:00
"certification_department",
"date",
"shop_lead_notified",
"certified_by",
]
list_display_links = [
2022-02-11 13:48:47 -05:00
"certification_name",
"name",
]
list_filter = [
2022-02-11 13:48:47 -05:00
"certification_version__definition__department",
("shop_lead_notified", admin.EmptyFieldListFilter),
]
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:
cert.shop_lead_notified = Now()
cert.save()
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
@admin.register(InstructorOrVendor)
class InstructorOrVendorAdmin(admin.ModelAdmin):
2022-02-11 13:48:47 -05:00
search_fields = ["name"]
list_display = [
"name",
"instructor_agreement_date",
"w9_date",
"phone",
"email_address",
]
@admin.register(SpecialProgram)
class SpecialProgramAdmin(admin.ModelAdmin):
2022-02-11 13:48:47 -05:00
search_fields = ["program_name"]
list_display = [
"program_name",
"discount_percent",
"discount_code",
"membership_code",
"start_date",
"end_date",
"program_amount",
"program_status",
]
@admin.register(Waiver)
class WaiverAdmin(admin.ModelAdmin):
2022-02-11 13:48:47 -05:00
search_fields = ["name"]
list_display = [
"name",
"date",
"emergency_contact_name",
"emergency_contact_number",
"waiver_version",
"guardian_name",
"guardian_relation",
"guardian_date",
]
admin.site.register(CmsRedRiverVeteransScholarship)