from django.contrib import admin
from django.db.models import Count

from .models import DataFile, DataFileSet
from taggit_helpers import (TaggitCounter, TaggitListFilter,
    TaggitTabularInline)


class DataFileSetInline(admin.TabularInline):
    model = DataFileSet.data_files.through
    extra = 3
    verbose_name = 'Associated Data File Set'
    verbose_name_plural = 'Associated Data File Sets'


@admin.register(DataFile)
class DataFileAdmin(TaggitCounter, admin.ModelAdmin):

    fieldset_data_file = ('Data File', {
        'fields': [
            'name',
            'slug',
            'file',
            'description',
            'is_published',
        ],
    })

    fieldsets = [
        fieldset_data_file,
    ]

    inlines = [
        TaggitTabularInline,
        DataFileSetInline,
    ]

    list_display = (
        'name',
        'file',
        'description',
        'number_of_data_file_sets',
        'taggit_counter',
        'is_published',
    )

    list_filter = (
        'is_published',
        TaggitListFilter,
    )

    prepopulated_fields = {
        'slug': ('name',),
    }

    save_on_top = True

    search_fields = (
        'name',
        'file__name',
        'file__original_filename',
        'description',
    )

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.annotate(data_file_set_count=Count('datafileset', distinct=True))
        return queryset

    def number_of_data_file_sets(self, obj):
        return obj.data_file_set_count
    number_of_data_file_sets.admin_order_field = 'data_file_set_count'
    number_of_data_file_sets.short_description = '# of Data File Sets'


@admin.register(DataFileSet)
class DataFileSetAdmin(TaggitCounter, admin.ModelAdmin):

    class Media:
        css = {
            'all': ('cms_lab_data/css/admin-publication-filter.css',)
        }

    fieldset_data_file_set = ('Data File Set', {
        'fields': [
            'name',
            'slug',
            'label',
            'description',
            'image',
            'is_published',
        ],
    })

    fieldset_display_settings = ('Display Settings', {
        'fields': [
            'pagination',
            'max_words_file_description',
            'word_buffer_file_description',
            'searchable',
        ],
    })

    fieldset_data_files = ('Data Files', {
        'fields': [
            'data_files',
        ],
    })

    fieldsets = [
        fieldset_data_file_set,
        fieldset_display_settings,
        fieldset_data_files,
    ]

    filter_vertical = (
        'data_files',
    )

    inlines = [
        TaggitTabularInline,
    ]

    list_display = (
        'name',
        'label',
        'description',
        'number_of_data_files',
        'pagination',
        'max_words_file_description',
        'word_buffer_file_description',
        'taggit_counter',
        'searchable',
        'is_published',
    )

    list_filter = (
        'is_published',
        TaggitListFilter,
    )

    prepopulated_fields = {
        'slug': ('name',),
    }

    save_on_top = True

    search_fields = (
        'name',
        'label',
        'description',
    )

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.annotate(data_file_count=Count('data_files', distinct=True))
        return queryset

    def number_of_data_files(self, obj):
        return obj.data_file_count
    number_of_data_files.admin_order_field = 'data_file_count'
    number_of_data_files.short_description = '# of Data Files'

@@ -389,32 +389,888 @@
 ta File Sets'%0A%0A%0A
+class ContentsPublishedFilter(admin.SimpleListFilter):%0A    %22%22%22%0A    Filter Data File Set records by the presence of unpublished data files.%0A    %22%22%22%0A    title = 'Contents Published'%0A    parameter_name = 'contents_published'%0A%0A    def lookups(self, request, model_admin):%0A        return (%0A            ('all_published', 'Yes'),%0A            ('has_unpublished', 'Has Unpublished Content'),%0A        )%0A%0A    def queryset(self, request, queryset):%0A        datasets_with_all_content_published = %5B%0A            ds.pk for ds in queryset%0A            if ds.data_files.count() == len(ds.published_data_files())%0A        %5D%0A%0A        if self.value() == 'all_published':%0A            return queryset.filter(pk__in=datasets_with_all_content_published)%0A        elif self.value() == 'has_unpublished':%0A            return queryset.exclude(pk__in=datasets_with_all_content_published)%0A%0A
 @admin.register(
@@ -3816,32 +3816,62 @@
 'is_published',%0A
+        'contents_published',%0A
     )%0A%0A    list_
@@ -3897,32 +3897,65 @@
 'is_published',%0A
+        ContentsPublishedFilter,%0A
         TaggitLi
@@ -4334,32 +4334,304 @@
 eturn queryset%0A%0A
+    def contents_published(self, obj):%0A        if obj.data_file_count == len(obj.published_data_files()):%0A            return True%0A        else:%0A            return False%0A    contents_published.boolean = True%0A    contents_published.short_description = 'Contents published'%0A%0A
     def number_o

Add admin list display & filter for whether data set contents are published