2019-07-16 13:42:31 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2019-07-08 12:30:58 +00:00
|
|
|
from django.contrib import admin
|
2019-07-17 10:14:23 +00:00
|
|
|
from polymorphic.admin import PolymorphicParentModelAdmin, \
|
|
|
|
PolymorphicChildModelAdmin, PolymorphicChildModelFilter
|
2019-07-08 12:30:58 +00:00
|
|
|
|
2019-07-17 09:17:50 +00:00
|
|
|
from .models.notes import Alias, Note, NoteClub, NoteSpecial, NoteUser
|
2019-07-16 13:42:31 +00:00
|
|
|
from .models.transactions import MembershipTransaction, Transaction, \
|
|
|
|
TransactionTemplate
|
2019-07-16 07:05:30 +00:00
|
|
|
|
2019-07-16 12:38:52 +00:00
|
|
|
|
|
|
|
class AliasInlines(admin.TabularInline):
|
|
|
|
"""
|
|
|
|
Define user and club aliases when editing their note
|
|
|
|
"""
|
|
|
|
extra = 0
|
|
|
|
model = Alias
|
|
|
|
|
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
@admin.register(Note)
|
|
|
|
class NoteAdmin(PolymorphicParentModelAdmin):
|
2019-07-16 12:38:52 +00:00
|
|
|
"""
|
2019-07-17 10:14:23 +00:00
|
|
|
Parent regrouping all note types as children
|
|
|
|
"""
|
|
|
|
child_models = (NoteClub, NoteSpecial, NoteUser)
|
|
|
|
list_filter = (PolymorphicChildModelFilter, 'is_active',)
|
|
|
|
|
|
|
|
# Use a polymorphic list
|
|
|
|
list_display = ('__str__', 'balance', 'is_active')
|
|
|
|
polymorphic_list = True
|
|
|
|
|
|
|
|
# Organize notes by registration date
|
|
|
|
date_hierarchy = 'created_at'
|
|
|
|
ordering = ['-created_at']
|
|
|
|
|
|
|
|
# Search by aliases
|
|
|
|
search_fields = ['alias__name']
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(NoteClub)
|
|
|
|
class NoteClubAdmin(PolymorphicChildModelAdmin):
|
|
|
|
"""
|
|
|
|
Child for a club note, see NoteAdmin
|
2019-07-16 12:38:52 +00:00
|
|
|
"""
|
|
|
|
inlines = (AliasInlines,)
|
|
|
|
|
2019-07-17 09:17:50 +00:00
|
|
|
# We can't change club after creation or the balance
|
|
|
|
readonly_fields = ('club', 'balance')
|
2019-07-17 07:49:59 +00:00
|
|
|
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
"""
|
|
|
|
A club note should not be manually added
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
"""
|
|
|
|
A club note should not be manually removed
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2019-07-16 12:38:52 +00:00
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
@admin.register(NoteSpecial)
|
|
|
|
class NoteSpecialAdmin(PolymorphicChildModelAdmin):
|
2019-07-16 12:38:52 +00:00
|
|
|
"""
|
2019-07-17 10:14:23 +00:00
|
|
|
Child for a special note, see NoteAdmin
|
2019-07-16 12:38:52 +00:00
|
|
|
"""
|
2019-07-17 10:14:23 +00:00
|
|
|
readonly_fields = ('balance',)
|
2019-07-16 12:38:52 +00:00
|
|
|
|
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
@admin.register(NoteUser)
|
|
|
|
class NoteUserAdmin(PolymorphicChildModelAdmin):
|
2019-07-16 12:38:52 +00:00
|
|
|
"""
|
2019-07-17 10:14:23 +00:00
|
|
|
Child for an user note, see NoteAdmin
|
2019-07-16 12:38:52 +00:00
|
|
|
"""
|
|
|
|
inlines = (AliasInlines,)
|
|
|
|
|
2019-07-17 09:17:50 +00:00
|
|
|
# We can't change user after creation or the balance
|
|
|
|
readonly_fields = ('user', 'balance')
|
2019-07-17 07:49:59 +00:00
|
|
|
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
"""
|
|
|
|
An user note should not be manually added
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
"""
|
|
|
|
An user note should not be manually removed
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2019-07-16 12:38:52 +00:00
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
@admin.register(TransactionTemplate)
|
2019-07-16 13:42:31 +00:00
|
|
|
class TransactionTemplateAdmin(admin.ModelAdmin):
|
|
|
|
"""
|
|
|
|
Admin customisation for TransactionTemplate
|
|
|
|
"""
|
|
|
|
list_display = ('name', 'destination', 'amount', 'template_type')
|
|
|
|
list_filter = ('destination', 'template_type',)
|
2019-07-17 10:14:23 +00:00
|
|
|
# autocomplete_fields = ('destination',)
|
2019-07-16 13:42:31 +00:00
|
|
|
|
|
|
|
|
2019-07-17 10:14:23 +00:00
|
|
|
# Register other models here.
|
2019-07-16 13:42:31 +00:00
|
|
|
admin.site.register(MembershipTransaction)
|
|
|
|
admin.site.register(Transaction)
|