From 90ffaae2f222a5a9f587d1a12a7eb166cdf85572 Mon Sep 17 00:00:00 2001
From: Alexandre Iooss <erdnaxe@crans.org>
Date: Wed, 17 Jul 2019 09:49:59 +0200
Subject: [PATCH] More models in admin

---
 member/admin.py |  7 ++++++-
 note/admin.py   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/member/admin.py b/member/admin.py
index d055d268..dc595d7e 100644
--- a/member/admin.py
+++ b/member/admin.py
@@ -7,7 +7,7 @@ from django.contrib.auth.admin import UserAdmin
 from django.contrib.auth.models import User
 
 from .forms import CustomUserChangeForm
-from .models import Club, Profile
+from .models import Club, Membership, Profile, Role
 
 
 class ProfileInline(admin.StackedInline):
@@ -33,6 +33,11 @@ class CustomUserAdmin(UserAdmin):
         return super().get_inline_instances(request, obj)
 
 
+# Update Django User with profile
 admin.site.unregister(User)
 admin.site.register(User, CustomUserAdmin)
+
+# Add other models
 admin.site.register(Club)
+admin.site.register(Membership)
+admin.site.register(Role)
diff --git a/note/admin.py b/note/admin.py
index 78936da9..e634e607 100644
--- a/note/admin.py
+++ b/note/admin.py
@@ -26,6 +26,21 @@ class NoteClubAdmin(admin.ModelAdmin):
     list_filter = ('is_active',)
     search_fields = ['club__name']
 
+    # We can't change club after creation
+    readonly_fields = ('club',)
+
+    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
+
 
 class NoteSpecialAdmin(admin.ModelAdmin):
     """
@@ -47,6 +62,21 @@ class NoteUserAdmin(admin.ModelAdmin):
     date_hierarchy = 'user__date_joined'
     ordering = ['-user__date_joined']
 
+    # We can't change user after creation
+    readonly_fields = ('user',)
+
+    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
+
 
 class TransactionTemplateAdmin(admin.ModelAdmin):
     """
-- 
GitLab