Parcourir la source

Form to add a mailling list subscription in the member/mailling list admin.

SimonBoulier il y a 6 ans
Parent
commit
0e032cbe40
2 fichiers modifiés avec 26 ajouts et 7 suppressions
  1. 24 7
      maillists/admin.py
  2. 2 0
      maillists/models.py

+ 24 - 7
maillists/admin.py

@@ -12,14 +12,31 @@ from .models import MaillingList, MaillingListSubscription, SyncCommandError
 import coin.members.admin
 
 
+class AddMaillingListSubscriptionInline(admin.StackedInline):
+    model = MaillingListSubscription
+    extra = 1
+    max_num = 1
+    fields = ('member', 'maillinglist')
+    verbose_name_plural = "Ajouter un abonnement à la liste mail"
+    verbose_name = "Ajouter un abonnement"
+
+    form = autocomplete_light.modelform_factory(MaillingListSubscription, fields='__all__')
+
+    def get_queryset(self, request):
+        qs = super(AddMaillingListSubscriptionInline, self).get_queryset(request)
+        return qs.none()
+
+    def has_delete_permission(self, request, obj=None):
+        return False
+
+
 class MaillingListSubscriptionInline(admin.TabularInline):
     model = MaillingListSubscription
-    extra = 0
 
-    form = autocomplete_light.modelform_factory(
-        MaillingListSubscription,
-        fields='__all__',
-    )
+    readonly_fields = ('member', 'maillinglist',)
+
+    def has_add_permission(self, request, obj=None):
+        return False
 
 
 class MaillingListAdmin(admin.ModelAdmin):
@@ -43,7 +60,7 @@ class MaillingListAdmin(admin.ModelAdmin):
     sync_to_server.short_description = (
         'Synchroniser les listes sélectionnées vers le serveur')
 
-    inlines = [MaillingListSubscriptionInline,]
+    inlines = [AddMaillingListSubscriptionInline, MaillingListSubscriptionInline,]
 
     def change_view(self, request, object_id, *args, **kwargs):
         try:
@@ -66,7 +83,7 @@ admin.site.register(MaillingList, MaillingListAdmin)
 
 
 class MemberAdmin(coin.members.admin.MemberAdmin):
-    inlines = coin.members.admin.MemberAdmin.inlines + [MaillingListSubscriptionInline]
+    inlines = coin.members.admin.MemberAdmin.inlines + [AddMaillingListSubscriptionInline, MaillingListSubscriptionInline]
 
     def change_view(self, request, *args, **kwargs):
         try:

+ 2 - 0
maillists/models.py

@@ -24,6 +24,8 @@ class MaillingListSubscription(models.Model):
         verbose_name_plural = 'abonnements à des listes mail'
         unique_together = ('member', 'maillinglist')
 
+    def __str__(self):
+        return str(self.maillinglist)
 
 class MaillingList(models.Model):
     short_name = models.CharField(