from __future__ import unicode_literals
from django.contrib import admin
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser


class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email']
    list_display = ('email')

admin.site.register(BackdropUser)

from __future__ import unicode_literals
from django.contrib import admin
from django.db import models
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser
from stagecraft.apps.datasets.models.data_set import DataSet


class DataSetInline(admin.StackedInline):
    model = DataSet
    fields = ('name',)
    extra = 0


class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email', 'data_sets']
    list_display = ('email', 'numer_of_datasets_user_has_access_to',)
    list_per_page = 30

    def queryset(self, request):
        return BackdropUser.objects.annotate(
            dataset_count=models.Count('data_sets')
        )

    def numer_of_datasets_user_has_access_to(self, obj):
        return obj.dataset_count

    numer_of_datasets_user_has_access_to.admin_order_field = 'dataset_count'


admin.site.register(BackdropUser, BackdropUserAdmin)

Add user:dataset count to backdrop user admin
Add user:dataset count to backdrop user admin

- Sortable by number of data-sets the user has access to
- Vaguely useful, but mainly an exercise in seeing how easily we can customise the output of the manytomany field (not very easily)

  from __future__ import unicode_literals
  from django.contrib import admin
+ from django.db import models
  import reversion
  from stagecraft.apps.datasets.models.backdrop_user import BackdropUser
+ from stagecraft.apps.datasets.models.data_set import DataSet
+ 
+ 
+ class DataSetInline(admin.StackedInline):
+     model = DataSet
+     fields = ('name',)
+     extra = 0
  
  
  class BackdropUserAdmin(reversion.VersionAdmin):
-     search_fields = ['email']
+     search_fields = ['email', 'data_sets']
-     list_display = ('email')
+     list_display = ('email', 'numer_of_datasets_user_has_access_to',)
+     list_per_page = 30
  
- admin.site.register(BackdropUser)
+     def queryset(self, request):
+         return BackdropUser.objects.annotate(
+             dataset_count=models.Count('data_sets')
+         )
  
+     def numer_of_datasets_user_has_access_to(self, obj):
+         return obj.dataset_count
+ 
+     numer_of_datasets_user_has_access_to.admin_order_field = 'dataset_count'
+ 
+ 
+ admin.site.register(BackdropUser, BackdropUserAdmin)
+ 
Add user:dataset count to backdrop user admin
## Code Before:
from __future__ import unicode_literals
from django.contrib import admin
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser


class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email']
    list_display = ('email')

admin.site.register(BackdropUser)

## Instruction:
Add user:dataset count to backdrop user admin
## Code After:
from __future__ import unicode_literals
from django.contrib import admin
from django.db import models
import reversion
from stagecraft.apps.datasets.models.backdrop_user import BackdropUser
from stagecraft.apps.datasets.models.data_set import DataSet


class DataSetInline(admin.StackedInline):
    model = DataSet
    fields = ('name',)
    extra = 0


class BackdropUserAdmin(reversion.VersionAdmin):
    search_fields = ['email', 'data_sets']
    list_display = ('email', 'numer_of_datasets_user_has_access_to',)
    list_per_page = 30

    def queryset(self, request):
        return BackdropUser.objects.annotate(
            dataset_count=models.Count('data_sets')
        )

    def numer_of_datasets_user_has_access_to(self, obj):
        return obj.dataset_count

    numer_of_datasets_user_has_access_to.admin_order_field = 'dataset_count'


admin.site.register(BackdropUser, BackdropUserAdmin)
