Source

leaves-cms / leaves / admin.py

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.conf import settings
from leaves.models import *

class AttachmentInline (admin.TabularInline):
	model = Attachment
	extra = 1

class MetaInline (admin.TabularInline):
	model = LeafMeta
	extra = 1

class UserSiteAdmin (admin.ModelAdmin):

	def formfield_for_foreignkey(self, db_field, request, **kwargs):
		if db_field.rel.to == User:
			if not request.user.is_superuser:
				kwargs['queryset'] = User.objects.filter(pk=request.user.pk)
			kwargs['initial'] = request.user.pk
		if not request.user.is_superuser:
			if db_field.rel.to == Site:
				kwargs['queryset'] = Site.objects.filter(members__user=request.user).distinct()
			else:
				names = db_field.rel.to._meta.get_all_field_names()
				if 'sites' in names:
					kwargs['queryset'] = db_field.rel.to.objects.filter(sites=request.user.memberships.sites.all()).distinct()
				elif 'site' in names:
					kwargs['queryset'] = db_field.rel.to.objects.filter(site__in=request.user.memberships.sites.all())
		return super(UserSiteAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

	def formfield_for_manytomany(self, db_field, request, **kwargs):
		if not request.user.is_superuser:
			if db_field.rel.to == Site:
				kwargs['queryset'] = Site.objects.filter(members__user=request.user).distinct()
			else:
				names = db_field.rel.to._meta.get_all_field_names()
				if 'sites' in names:
					kwargs['queryset'] = db_field.rel.to.objects.filter(sites=request.user.memberships.sites.all()).distinct()
				elif 'site' in names:
					kwargs['queryset'] = db_field.rel.to.objects.filter(site__in=request.user.memberships.sites.all())
		return super(UserSiteAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

	def queryset(self, request):
		qs = super(UserSiteAdmin, self).queryset(request)
		if request.user.is_superuser:
			return qs
		names = qs.model._meta.get_all_field_names()
		if 'sites' in names:
			return qs.filter(sites=request.user.memberships.sites.all())
		elif 'site' in names:
			return qs.filter(site__in=request.user.memberships.sites.all())
		return qs

class LeafAdmin (UserSiteAdmin):
	list_display = ('title', 'author_name', 'pub_date', 'status', 'num_views', 'last_viewed')
	list_filter = ('pub_date', 'status', 'author_name', 'sites', 'stream', 'feed', 'allow_comments')
	date_hierarchy = 'pub_date'
	prepopulated_fields = {'slug': ('title',)}
	search_fields = ('title', 'summary', 'content')
	inlines = (AttachmentInline, MetaInline)

	def formfield_for_dbfield(self, db_field, **kwargs):
		if db_field.name == 'author_name' and 'request' in kwargs:
			kwargs['initial'] = kwargs['request'].user.get_full_name().strip()
		return super(LeafAdmin, self).formfield_for_dbfield(db_field, **kwargs)

class CommentAdmin (admin.ModelAdmin):
	list_display = ('leaf', 'title', 'author_name', 'email', 'status', 'pub_date')
	list_filter = ('pub_date', 'status', 'leaf__sites')
	date_hierarchy = 'pub_date'
	search_fields = ('title', 'comment', 'author_name', 'email')
	actions = ('approve_comments',)
	ordering = ('-pub_date',)

	def approve_comments(self, request, queryset):
		update_count = queryset.update(status='published')
		term = 'comment was' if update_count == 1 else 'comments were'
		self.message_user(request, '%s %s successfully approved.' % (update_count, term))
	approve_comments.short_description = 'Mark selected comments as published'

class TagAdmin (UserSiteAdmin):
	list_display = ('name', 'slug')
	list_filter = ('sites',)
	prepopulated_fields = {'slug': ('name',)}

class TranslationAdmin (admin.ModelAdmin):
	list_display = ('__unicode__', 'content_type', 'object_id', 'field', 'language')
	list_filter = ('content_type', 'language')

class ConfigAdmin (UserSiteAdmin):
	list_display = ('site',)
	list_filter = ('site',)

class RedirectAdmin (UserSiteAdmin):
	list_display = ('old_path', 'new_path', 'site', 'redirect_type')
	list_filter = ('site', 'redirect_type')

class PrefsAdmin (ConfigAdmin):
	list_display = ('site', 'theme', 'stream_count', 'default_comment_status', 'feed_count', 'analytics_id')

admin.site.register(Comment, CommentAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(Preferences, PrefsAdmin)
admin.site.register(Redirect, RedirectAdmin)
admin.site.register(UserSites)

if settings.LEAVES_ENABLE_TRANSLATION:
	admin.site.register(Translation, TranslationAdmin)