Skip to content

Commit

Permalink
Merge pull request #28 from ppfeufer/development
Browse files Browse the repository at this point in the history
added some filter to the admin backend
  • Loading branch information
ppfeufer authored Sep 26, 2020
2 parents 4082cfe + 1bda7b1 commit 760f9e8
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
102 changes: 100 additions & 2 deletions afat/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
from .models import AFat, AFatLink, AFatLinkType


def custom_filter_title(title):
class Wrapper(admin.FieldListFilter):
def __new__(cls, *args, **kwargs):
instance = admin.FieldListFilter.create(*args, **kwargs)
instance.title = title

return instance

return Wrapper


# Register your models here.
@admin.register(AFatLink)
class AFatLinkAdmin(admin.ModelAdmin):
Expand All @@ -20,13 +31,28 @@ class AFatLinkAdmin(admin.ModelAdmin):
"afattime",
"creator",
"fleet",
"link_type",
"_link_type",
"is_esilink",
"hash",
"deleted_at",
)

list_filter = (
"is_esilink",
("link_type__name", custom_filter_title("fleet type")),
)

ordering = ("-afattime",)

def _link_type(self, obj):
if obj.link_type:
return obj.link_type.name
else:
return "-"

_link_type.short_description = "Fleet Type"
_link_type.admin_order_field = "link_type__name"


@admin.register(AFat)
class AFatAdmin(admin.ModelAdmin):
Expand All @@ -35,6 +61,9 @@ class AFatAdmin(admin.ModelAdmin):
"""

list_display = ("character", "system", "shiptype", "afatlink", "deleted_at")

list_filter = ("character", "system", "shiptype")

ordering = ("-character",)


Expand All @@ -44,5 +73,74 @@ class AFatLinkTypeAdmin(admin.ModelAdmin):
config for fatlinktype model
"""

list_display = ("id", "name")
# list_select_related = True

list_display = (
"id",
"_name",
"_is_enabled",
)

list_filter = ("is_enabled",)

ordering = ("name",)

def _name(self, obj):
return obj.name

_name.short_description = "Fleet Type"
_name.admin_order_field = "name"

def _is_enabled(self, obj):
return obj.is_enabled

_is_enabled.boolean = True
_is_enabled.short_description = "Is Enabled"
_is_enabled.admin_order_field = "is_enabled"

actions = (
"mark_as_active",
"mark_as_inactive",
)

def mark_as_active(self, request, queryset):
"""
Mark fleet type as active
:param request:
:param queryset:
"""

notifications_count = 0

for obj in queryset:
obj.is_enabled = True
obj.save()

notifications_count += 1

self.message_user(
request, "{} fleet types marked as active".format(notifications_count)
)

mark_as_active.short_description = "Activate selected fleet type(s)"

def mark_as_inactive(self, request, queryset):
"""
Mark fleet type as inactive
:param request:
:param queryset:
"""

notifications_count = 0

for obj in queryset:
obj.is_enabled = False
obj.save()

notifications_count += 1

self.message_user(
request, "{} fleet types marked as inactive".format(notifications_count)
)

mark_as_inactive.short_description = "Deactivate selected fleet type(s)"
4 changes: 3 additions & 1 deletion afat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ def link_add(request):
if "msg" in request.session:
msg = request.session.pop("msg")

link_types = AFatLinkType.objects.all().order_by("name")
link_types = AFatLinkType.objects.filter(
is_enabled=True,
).order_by("name")

context = {"link_types": link_types, "msg": msg, "permissions": permissions}

Expand Down

0 comments on commit 760f9e8

Please sign in to comment.