60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
"""Users in the admin (WEB-32). Staff can deactivate or promote an account but never delete one: a user's
|
|
ledger rows are PROTECTed (DAT-76), and a deactivation is an audited action. A user's game records are shown
|
|
read-only on the same page."""
|
|
|
|
from django.contrib import admin
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
|
from apps.lottery import audit
|
|
from apps.lottery.models import Movement, Participation
|
|
|
|
from .models import User
|
|
|
|
|
|
class ReadOnlyInline(admin.TabularInline):
|
|
extra = 0
|
|
can_delete = False
|
|
show_change_link = False
|
|
|
|
def has_add_permission(self, request, obj=None):
|
|
return False
|
|
|
|
def has_change_permission(self, request, obj=None):
|
|
return False
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
return False
|
|
|
|
|
|
class ParticipationInline(ReadOnlyInline):
|
|
model = Participation
|
|
fields = ("round", "status", "bet_amount_sats", "created_at")
|
|
readonly_fields = fields
|
|
ordering = ("-id",)
|
|
max_num = 0
|
|
verbose_name_plural = "Participations (read-only)"
|
|
|
|
|
|
class MovementInline(ReadOnlyInline):
|
|
model = Movement
|
|
fields = ("kind", "status", "amount_sats", "round", "created_at")
|
|
readonly_fields = fields
|
|
ordering = ("-id",)
|
|
max_num = 0
|
|
verbose_name_plural = "Movements (read-only)"
|
|
|
|
|
|
@admin.register(User)
|
|
class AccountAdmin(UserAdmin):
|
|
inlines = [ParticipationInline, MovementInline]
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
return False
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
super().save_model(request, obj, form, change)
|
|
if change and {"is_active", "is_staff", "is_superuser"} & set(form.changed_data):
|
|
audit.record("user_changed", actor=request.user, subject_type="user", subject_id=obj.pk,
|
|
payload={name: getattr(obj, name) for name in ("is_active", "is_staff", "is_superuser")
|
|
if name in form.changed_data})
|