<reponame>adam-grandt-tts/data-driven-acquisition<filename>data_driven_acquisition/admin.py

from django.contrib import admin
from django.urls import reverse
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe


from guardian.admin import GuardedModelAdmin
from reversion.admin import VersionAdmin

from import_export.admin import ImportExportModelAdmin

from .models import (
    Folder,
    File,
    PackageTemplate,
    PackageProperty,
    PropertyValue)


class IsPackageFilter(admin.SimpleListFilter):
    title = 'Is package'
    parameter_name = 'parent'

    def lookups(self, request, model_admin):
        return [
            ('package', 'Package'),
            ('folder', 'Folder')
        ]

    def queryset(self, request, queryset):
        if self.value() == 'folder':
            return queryset.distinct().filter(parent__isnull=False)
        if self.value() == 'package':
            return queryset.distinct().filter(parent__isnull=True)
        if self.value():
            return queryset.distinct().filter(pages__isnull=True)


class SubfolderInline(admin.TabularInline):
    model = Folder
    extra = 0
    verbose_name = "Subfolder"
    verbose_name_plural = "Subfolders"
    fields = ["get_edit_link", "name"]
    readonly_fields = ["get_edit_link", "name"]

    def get_edit_link(self, obj=None):
        if obj.pk:  # if object has already been saved and has a primary key, show link to it
            url = reverse('admin:%s_%s_change' % (obj._meta.app_label, obj._meta.model_name), args=[force_text(obj.pk)])
            return mark_safe("""<a href="{url}">{text}</a>""".format(
                url=url,
                text="Edit folder",
            ))
        return ""
    get_edit_link.short_description = "Edit link"
    get_edit_link.allow_tags = True


class PropertyValueInline(admin.TabularInline):
    model = PropertyValue
    extra = 0
    verbose_name = "Property"
    verbose_name_plural = "Properties"
    fields = ["prop", "get_type", "value"]
    readonly_fields = ["get_type"]

    def get_type(self, obj=None):
        if obj.pk:
            return obj.property_type
        else:
            return ""
    get_type.short_description = "Type"
    get_type.allow_tags = True


class FileInline(admin.TabularInline):
    model = File
    extra = 0
    verbose_name = "File"
    verbose_name_plural = "Files"
    fields = ["get_edit_link", "name", "file_type"]
    readonly_fields = ["get_edit_link",]
    save_on_top = True

    def get_edit_link(self, obj=None):
        if obj.pk:  # if object has already been saved and has a primary key, show link to it
            url = reverse('admin:%s_%s_change' % (obj._meta.app_label, obj._meta.model_name), args=[force_text(obj.pk)])
            return mark_safe("""<a href="{url}">{text}</a>""".format(
                url=url,
                text="Edit file",
            ))
        return ""
    get_edit_link.short_description = "Edit link"
    get_edit_link.allow_tags = True


@admin.register(Folder)
class FolderAdmin(GuardedModelAdmin, VersionAdmin):
    list_filter = (IsPackageFilter, )
    inlines = [SubfolderInline, FileInline, PropertyValueInline]
    search_fields = ['name']
    save_on_top = True



@admin.register(File)
class FileAdmin(GuardedModelAdmin, VersionAdmin):
    save_on_top = True
    pass


@admin.register(PackageTemplate)
class PackageTemplateAdmin(GuardedModelAdmin, ImportExportModelAdmin):
    filter_horizontal = ('properties',)
    save_on_top = True

    pass


@admin.register(PackageProperty)
class PackagePropertyAdmin(GuardedModelAdmin, ImportExportModelAdmin):
    list_display = ('name', 'property_type', 'max_length', 'tab')
    list_filter = ('tab', 'property_type')
    search_fields = ['name']
    save_on_top = True


@admin.register(PropertyValue)
class PropertyAdmin(GuardedModelAdmin, VersionAdmin):
    search_fields = ['name']
    list_display = ('package', 'name', 'property_type', 'max_length', 'value')
    save_on_top = True

from django.contrib import admin
from django.urls import reverse
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe


from guardian.admin import GuardedModelAdmin
from reversion.admin import VersionAdmin

from import_export.admin import ImportExportModelAdmin

from .models import (
    Folder,
    File,
    PackageTemplate,
    PackageProperty,
    PropertyValue)


class IsPackageFilter(admin.SimpleListFilter):
    title = 'Is package'
    parameter_name = 'parent'

    def lookups(self, request, model_admin):
        return [
            ('package', 'Package'),
            ('folder', 'Folder')
        ]

    def queryset(self, request, queryset):
        if self.value() == 'folder':
            return queryset.distinct().filter(parent__isnull=False)
        if self.value() == 'package':
            return queryset.distinct().filter(parent__isnull=True)
        if self.value():
            return queryset.distinct().filter(pages__isnull=True)


class SubfolderInline(admin.TabularInline):
    model = Folder
    extra = 0
    verbose_name = "Subfolder"
    verbose_name_plural = "Subfolders"
    fields = ["get_edit_link", "name"]
    readonly_fields = ["get_edit_link", "name"]

    def get_edit_link(self, obj=None):
        if obj.pk:  # if object has already been saved and has a primary key, show link to it
            url = reverse('admin:%s_%s_change' % (obj._meta.app_label, obj._meta.model_name), args=[force_text(obj.pk)])
            return mark_safe("""<a href="{url}">{text}</a>""".format(
                url=url,
                text="Edit folder",
            ))
        return ""
    get_edit_link.short_description = "Edit link"
    get_edit_link.allow_tags = True


class PropertyValueInline(admin.TabularInline):
    model = PropertyValue
    extra = 0
    verbose_name = "Property"
    verbose_name_plural = "Properties"
    fields = ["prop", "get_type", "value"]
    readonly_fields = ["get_type"]

    def get_type(self, obj=None):
        if obj.pk:
            return obj.property_type
        else:
            return ""
    get_type.short_description = "Type"
    get_type.allow_tags = True


class FileInline(admin.TabularInline):
    model = File
    extra = 0
    verbose_name = "File"
    verbose_name_plural = "Files"
    fields = ["get_edit_link", "name", "file_type"]
    readonly_fields = ["get_edit_link",]
    save_on_top = True

    def get_edit_link(self, obj=None):
        if obj.pk:  # if object has already been saved and has a primary key, show link to it
            url = reverse('admin:%s_%s_change' % (obj._meta.app_label, obj._meta.model_name), args=[force_text(obj.pk)])
            return mark_safe("""<a href="{url}">{text}</a>""".format(
                url=url,
                text="Edit file",
            ))
        return ""
    get_edit_link.short_description = "Edit link"
    get_edit_link.allow_tags = True


@admin.register(Folder)
class FolderAdmin(GuardedModelAdmin, VersionAdmin):
    list_filter = (IsPackageFilter, )
    inlines = [SubfolderInline, FileInline, PropertyValueInline]
    search_fields = ['name']
    save_on_top = True



@admin.register(File)
class FileAdmin(GuardedModelAdmin, VersionAdmin):
    save_on_top = True
    pass


@admin.register(PackageTemplate)
class PackageTemplateAdmin(GuardedModelAdmin, ImportExportModelAdmin):
    filter_horizontal = ('properties',)
    save_on_top = True

    pass


@admin.register(PackageProperty)
class PackagePropertyAdmin(GuardedModelAdmin, ImportExportModelAdmin):
    list_display = ('name', 'property_type', 'max_length', 'tab')
    list_filter = ('tab', 'property_type')
    search_fields = ['name']
    save_on_top = True


@admin.register(PropertyValue)
class PropertyAdmin(GuardedModelAdmin, VersionAdmin):
    search_fields = ['name']
    list_display = ('package', 'name', 'property_type', 'max_length', 'value')
    save_on_top = True
# if object has already been saved and has a primary key, show link to it <a href="{url}">{text}</a> # if object has already been saved and has a primary key, show link to it <a href="{url}">{text}</a>