openbackloggery/collection/games/admin.py

124 lines
3.2 KiB
Python
Raw Normal View History

2017-08-16 20:00:41 +00:00
from django.contrib import admin
from django.utils.translation import ugettext as _
from games.forms import GameForm
from games.models import Game
from games.models import Platform
from games.models import Timeline
class PlatformAdmin(admin.ModelAdmin):
list_display = ('shortname', 'name')
2017-08-16 20:00:41 +00:00
class StatusFilter(admin.SimpleListFilter):
"""
Remove CREATED status.
"""
title = _('state')
parameter_name = 'status'
def lookups(self, request, model_admin):
"""
Return only games choices (not CREATED from Item abstract class).
"""
return Game.STATUS_CHOICES
def queryset(self, request, queryset):
"""
Filter on 'status' field.
"""
if self.value():
return queryset.filter(status=self.value())
else:
return queryset
class PlatformFilter(admin.SimpleListFilter):
"""
Display Platform shortname.
"""
title = _('platform')
parameter_name = 'collection'
def lookups(self, request, model_admin):
"""
Get all platforms
"""
return [(x.id, x.shortname) for x in Platform.objects.all()]
def queryset(self, request, queryset):
"""
Filter on 'shortname' field.
"""
if self.value():
return queryset.filter(collection__id=self.value())
else:
return queryset
class GameAdmin(admin.ModelAdmin):
list_display = ('name', 'get_platform', 'playing', 'status', 'wish')
list_filter = [StatusFilter, PlatformFilter, 'playing', 'wish']
search_fields = ('name', )
fieldsets = [(_('Game Information'), {
'fields': [('name', 'collection')]
}), (_('Progress'), {
'fields': [('status'), ('achievement', 'achievement_max'), ('note')]
}), ('', {
'fields': [('playing'), ('unplayed'), ('wish')]
})]
form = GameForm
def get_platform(self, obj):
"""
Display platform shortname
"""
return obj.collection.shortname
get_platform.short_description = _('Platform')
get_platform.admin_order_field = 'collection__shortname'
class TimelinePlatformFilter(admin.SimpleListFilter):
"""
Display Platform shortname.
"""
title = _('platform')
parameter_name = 'collection'
def lookups(self, request, model_admin):
"""
Get all platforms
"""
return [(x.id, x.shortname) for x in Platform.objects.all()]
def queryset(self, request, queryset):
"""
Filter on 'shortname' field.
"""
if self.value():
return queryset.filter(item__collection__id=self.value())
else:
return queryset
class TimelineAdmin(admin.ModelAdmin):
list_display = ('date', 'status', 'item', 'get_platform')
search_fields = ('item__name', )
list_filter = (TimelinePlatformFilter, )
def get_platform(self, obj):
"""
Display platform shortname
"""
return obj.item.collection.shortname
get_platform.short_description = _('Platform')
get_platform.admin_order_field = ('item__collection__shortname')
admin.site.register(Platform, PlatformAdmin)
admin.site.register(Game, GameAdmin)
admin.site.register(Timeline, TimelineAdmin)