cciw-website / cciw / cciwmain / admin.py

from cciw.cciwmain.models import Site, Person, Camp
from django.contrib import admin


from functools import wraps
def rename_app_list(func):
    m = {'Cciwmain': 'Camp info',
         'Sitecontent': 'Site content',
         'Sites': 'Web sites'
         }
    @wraps(func)
    def _wrapper(*args, **kwargs):
        response = func(*args, **kwargs)
        app_list = response.context_data.get('app_list')
        if app_list is not None:
            for a in app_list:
                name = a['name']
                a['name'] = m.get(name, name)
        title = response.context_data.get('title')
        if title is not None:
            app_label = title.split(' ')[0]
            if app_label in m:
                response.context_data['title'] = "%s administration" % m[app_label]
        return response
    return _wrapper

admin.site.index = rename_app_list(admin.site.index)
admin.site.app_index = rename_app_list(admin.site.app_index)


class SiteAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('short_name', 'long_name', 'info')}),
    )

class PersonAdmin(admin.ModelAdmin):
    filter_horizontal = ('users',)

class CampAdmin(admin.ModelAdmin):
    fieldsets = (
        ('Public info',
         {'fields': ('year', 'number', 'minimum_age', 'maximum_age', 'start_date', 'end_date',
                     'chaplain', 'leaders', 'site', 'previous_camp')
          }
        ),
        ('Booking constraints',
         {'fields': ('max_campers', 'max_male_campers', 'max_female_campers',
                     'south_wales_transport_available')
          }
         ),
        ('Applications and references',
         {'fields': ('online_applications', 'admins'),
          'description': '<div>Options for managing applications. Officer lists are managed <a href="/officers/leaders/">elsewhere</a>, not here.</div>',
          }
         ),
    )
    ordering = ('-year','number')
    def leaders(camp):
        return camp.leaders_formatted
    def chaplain(camp):
        return camp.chaplain
    chaplain.admin_order_field = 'chaplain__name'
    list_display = ('year', 'number', leaders, chaplain, 'age', 'site', 'start_date')
    list_display_links = ('number', leaders)
    del leaders, chaplain
    list_filter = ('site',)
    filter_horizontal = ('leaders', 'admins')
    date_hierarchy = 'start_date'

    def queryset(self, request):
        return super(CampAdmin, self).queryset(request).select_related('site', 'chaplain')

admin.site.register(Site, SiteAdmin)
admin.site.register(Person, PersonAdmin)
admin.site.register(Camp, CampAdmin)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.