142 lines
4.0 KiB
Python
142 lines
4.0 KiB
Python
from django.contrib import admin, messages
|
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
|
|
|
from .models import OtpCode, RegistrationTicket, User
|
|
from .sms import send_registration_approved, send_registration_rejected
|
|
|
|
|
|
@admin.register(User)
|
|
class UserAdmin(BaseUserAdmin):
|
|
ordering = ["-date_joined"]
|
|
list_display = [
|
|
"mobile",
|
|
"full_name",
|
|
"status",
|
|
"idnumber",
|
|
"island",
|
|
"auth_method",
|
|
"date_joined",
|
|
]
|
|
list_filter = ["status", "auth_method", "atoll", "is_staff", "is_active"]
|
|
search_fields = ["mobile", "full_name", "email", "idnumber"]
|
|
readonly_fields = [
|
|
"date_joined",
|
|
"updated_at",
|
|
"last_login",
|
|
"terms_accepted_at",
|
|
"policy_accepted_at",
|
|
"reviewed_at",
|
|
"reviewed_by",
|
|
]
|
|
autocomplete_fields = ["atoll", "island"]
|
|
actions = ["approve_registrations", "reject_registrations"]
|
|
fieldsets = [
|
|
(None, {"fields": ["mobile", "password"]}),
|
|
(
|
|
"Applicant",
|
|
{
|
|
"fields": [
|
|
"full_name",
|
|
"email",
|
|
"idnumber",
|
|
"date_of_birth",
|
|
"atoll",
|
|
"island",
|
|
]
|
|
},
|
|
),
|
|
(
|
|
"Registration review",
|
|
{
|
|
"fields": [
|
|
"status",
|
|
"rejection_reason",
|
|
"terms_accepted_at",
|
|
"policy_accepted_at",
|
|
"reviewed_at",
|
|
"reviewed_by",
|
|
]
|
|
},
|
|
),
|
|
("Portal", {"fields": ["auth_method", "mobile_verified"]}),
|
|
(
|
|
"Permissions",
|
|
{
|
|
"fields": [
|
|
"is_active",
|
|
"is_staff",
|
|
"is_superuser",
|
|
"groups",
|
|
"user_permissions",
|
|
]
|
|
},
|
|
),
|
|
("Dates", {"fields": ["last_login", "date_joined", "updated_at"]}),
|
|
]
|
|
add_fieldsets = [
|
|
(
|
|
None,
|
|
{
|
|
"classes": ["wide"],
|
|
"fields": [
|
|
"mobile",
|
|
"full_name",
|
|
"auth_method",
|
|
"password1",
|
|
"password2",
|
|
],
|
|
},
|
|
),
|
|
]
|
|
|
|
@admin.action(description="Approve selected registrations")
|
|
def approve_registrations(self, request, queryset):
|
|
pending = queryset.exclude(status=User.Status.APPROVED)
|
|
for user in pending:
|
|
user.approve(reviewer=request.user)
|
|
send_registration_approved(user.mobile)
|
|
self.message_user(
|
|
request, f"Approved {pending.count()} registration(s).", messages.SUCCESS
|
|
)
|
|
|
|
@admin.action(description="Reject selected registrations")
|
|
def reject_registrations(self, request, queryset):
|
|
pending = queryset.exclude(status=User.Status.REJECTED)
|
|
for user in pending:
|
|
user.reject(reviewer=request.user)
|
|
send_registration_rejected(user.mobile, user.rejection_reason)
|
|
self.message_user(
|
|
request, f"Rejected {pending.count()} registration(s).", messages.WARNING
|
|
)
|
|
|
|
|
|
@admin.register(OtpCode)
|
|
class OtpCodeAdmin(admin.ModelAdmin):
|
|
list_display = [
|
|
"mobile",
|
|
"purpose",
|
|
"attempts",
|
|
"created_at",
|
|
"expires_at",
|
|
"consumed_at",
|
|
]
|
|
list_filter = ["purpose"]
|
|
search_fields = ["mobile"]
|
|
readonly_fields = [
|
|
"user",
|
|
"mobile",
|
|
"purpose",
|
|
"code_hash",
|
|
"attempts",
|
|
"created_at",
|
|
"expires_at",
|
|
"consumed_at",
|
|
]
|
|
|
|
|
|
@admin.register(RegistrationTicket)
|
|
class RegistrationTicketAdmin(admin.ModelAdmin):
|
|
list_display = ["mobile", "created_at", "expires_at", "consumed_at"]
|
|
search_fields = ["mobile"]
|
|
readonly_fields = ["key", "mobile", "created_at", "expires_at", "consumed_at"]
|