Browse Source

amélioration mineur de l’admin

Élie Bouttier 8 years ago
parent
commit
ed9023092b
3 changed files with 21 additions and 15 deletions
  1. 0 11
      adhesions/admin.py
  2. 13 0
      adhesions/models.py
  3. 8 4
      services/admin.py

+ 0 - 11
adhesions/admin.py

@@ -4,8 +4,6 @@ from django.contrib.contenttypes.admin import GenericStackedInline, GenericTabul
 from django.db.models import Q
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
-from django.core.urlresolvers import reverse
-from django.utils.html import format_html
 
 from .forms import AdhesionForm
 from .models import ProxyUser, Corporation, Adhesion
@@ -120,15 +118,6 @@ class AdhesionAdmin(admin.ModelAdmin):
         return 'ADT%d' % obj.id
     get_id.short_description = 'Numéro d’adhérent'
 
-    def get_adherent_link(self, obj):
-        if obj.is_physical():
-            model_name = 'proxyuser'
-        else:
-            model_name = 'corporation'
-        url = reverse('admin:adhesions_%s_change' % model_name, args=(obj.adherent.id,))
-        return format_html(u'<a href="{}">{}</a>', url, str(obj.adherent))
-    get_adherent_link.short_description = 'Nom ou raison sociale'
-
     def get_search_results(self, request, queryset, search_term):
         queryset, use_distinct = super().get_search_results(request, queryset, search_term)
         users = User.objects.filter(

+ 13 - 0
adhesions/models.py

@@ -3,6 +3,8 @@ from django.urls import reverse
 from django.contrib.auth.models import User
 from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
 from django.contrib.contenttypes.models import ContentType
+from django.core.urlresolvers import reverse
+from django.utils.html import format_html
 
 from djadhere.utils import get_active_filter
 from banking.models import Payment
@@ -58,6 +60,17 @@ class Adhesion(models.Model):
             return str(self.adherent)
     get_adherent_name.short_description = 'Nom ou raison sociale'
 
+    def get_adherent_link(self):
+        if self.is_physical():
+            name = str(self.adherent.profile)
+            model_name = 'proxyuser'
+        else:
+            name = str(self.adherent)
+            model_name = 'corporation'
+        url = reverse('admin:adhesions_%s_change' % model_name, args=(self.adherent.id,))
+        return format_html(u'<a href="{}">{}</a>', url, name)
+    get_adherent_link.short_description = 'Nom ou raison sociale'
+
     def __str__(self):
         return 'ADT%d' % self.id
         if self.id is None:

+ 8 - 4
services/admin.py

@@ -5,6 +5,7 @@ from django.utils import timezone
 from django.core.urlresolvers import reverse
 from django.utils.html import format_html
 
+from adhesions.models import Adhesion
 from .models import Service, ServiceType, IPResource, Route, ResourceAllocation
 from djadhere.utils import get_active_filter
 
@@ -91,17 +92,20 @@ ends_resource.short_description = 'Terminer les allocations sélectionnées'
 ### ModelAdmin
 
 class ServiceAdmin(admin.ModelAdmin):
-    list_display = ('id', 'adhesion_link', 'service_type', 'label', 'active')
+    list_display = ('id', 'get_adhesion_link', 'get_adherent_link', 'service_type', 'label', 'active')
     list_filter = (
         'active',
         ('service_type', admin.RelatedOnlyFieldListFilter),
     )
     inlines = (AllocationInline,)
-    search_fields = ('id', 'service_type__name', 'label')
+    search_fields = ('id', 'service_type__name', 'label', 'adhesion__id',)
     raw_id_fields = ('adhesion',)
 
-    adhesion_link = get_foreignkey_link_func('adhesion')
-    adhesion_link.short_description = 'Numéro d’adhésion'
+    get_adhesion_link = get_foreignkey_link_func('adhesion')
+    get_adhesion_link.short_description = 'Numéro d’adhésion'
+
+    get_adherent_link = lambda self, service: service.adhesion.get_adherent_link()
+    get_adherent_link.short_description = Adhesion.get_adherent_link.short_description
 
     def get_actions(self, request):
         actions = super().get_actions(request)