forked from rc/aircox
		
	work on admin interface
This commit is contained in:
		
							
								
								
									
										51
									
								
								cms/admin.py
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								cms/admin.py
									
									
									
									
									
								
							@ -4,7 +4,6 @@ from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
import aircox.cms.models as models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostAdmin(admin.ModelAdmin):
 | 
			
		||||
    list_display = [ 'title', 'date', 'author', 'published', 'post_tags']
 | 
			
		||||
    list_editable = [ 'published' ]
 | 
			
		||||
@ -42,6 +41,56 @@ class CommentAdmin(admin.ModelAdmin):
 | 
			
		||||
        return post.content[:256]
 | 
			
		||||
    content_slice.short_description = _('content')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostInline(admin.StackedInline):
 | 
			
		||||
    extra = 1
 | 
			
		||||
    max_num = 1
 | 
			
		||||
    verbose_name = _('Post')
 | 
			
		||||
 | 
			
		||||
    fieldsets = [
 | 
			
		||||
        (None, {
 | 
			
		||||
            'fields': ['title', 'content', 'image', 'tags']
 | 
			
		||||
        }),
 | 
			
		||||
        (None, {
 | 
			
		||||
            'fields': ['date', 'published', 'author']
 | 
			
		||||
        })
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def inject_related_inline(post_model, prepend = False, inline = None):
 | 
			
		||||
    """
 | 
			
		||||
    Create an inline class and inject it into the related model admin class.
 | 
			
		||||
    Clean-up bound attributes.
 | 
			
		||||
    """
 | 
			
		||||
    class InlineModel(PostInline):
 | 
			
		||||
        model = post_model
 | 
			
		||||
        verbose_name = _('Related post')
 | 
			
		||||
 | 
			
		||||
    inline = inline or InlineModel
 | 
			
		||||
 | 
			
		||||
    # remove bound attributes
 | 
			
		||||
    for none, dic in inline.fieldsets:
 | 
			
		||||
        if not dic.get('fields'):
 | 
			
		||||
            continue
 | 
			
		||||
        dic['fields'] = [ v for v in dic['fields']
 | 
			
		||||
                            if v not in post_model._relation.bindings.keys() ]
 | 
			
		||||
 | 
			
		||||
    inject_inline(post_model._meta.get_field('related').rel.to,
 | 
			
		||||
                  inline, prepend)
 | 
			
		||||
 | 
			
		||||
def inject_inline(model, inline, prepend = False):
 | 
			
		||||
    registry = admin.site._registry
 | 
			
		||||
    if not model in registry:
 | 
			
		||||
        return TypeError('{} not in admin registry'.format(model.__name__))
 | 
			
		||||
 | 
			
		||||
    inlines = list(registry[model].inlines) or []
 | 
			
		||||
    if prepend:
 | 
			
		||||
        inlines.insert(0, inline)
 | 
			
		||||
    else:
 | 
			
		||||
        inlines.append(inline)
 | 
			
		||||
    registry[model].inlines = inlines
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
admin.site.register(models.Article, PostAdmin)
 | 
			
		||||
admin.site.register(models.Comment, CommentAdmin)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -288,7 +288,8 @@ class RelatedPostBase (models.base.ModelBase):
 | 
			
		||||
            post = model.objects.filter(related = instance)
 | 
			
		||||
            if post.count():
 | 
			
		||||
                post = post[0]
 | 
			
		||||
            elif rel.auto_create:
 | 
			
		||||
            elif rel.auto_create(instance) if callable(rel.auto_create) else \
 | 
			
		||||
                 rel.auto_create:
 | 
			
		||||
                post = model(related = instance)
 | 
			
		||||
            else:
 | 
			
		||||
                return
 | 
			
		||||
@ -375,12 +376,12 @@ class RelatedPost (Post, metaclass = RelatedPostBase):
 | 
			
		||||
        * post_to_rel: auto update related object when post is updated
 | 
			
		||||
        * rel_to_post: auto update the post when related object is updated
 | 
			
		||||
        * thread_model: generated by the metaclass, points to the RelatedPost
 | 
			
		||||
            model generated for the bindings.thread object.
 | 
			
		||||
          model generated for the bindings.thread object.
 | 
			
		||||
        * field_args: dict of arguments to pass to the ForeignKey constructor,
 | 
			
		||||
            such as: ForeignKey(related_model, **field_args)
 | 
			
		||||
          such as: ForeignKey(related_model, **field_args)
 | 
			
		||||
        * auto_create: automatically create a RelatedPost for each new item of
 | 
			
		||||
            the related object and init it with bounded values. Use signals
 | 
			
		||||
            '', ''.
 | 
			
		||||
          the related object and init it with bounded values. Use 'post_save'
 | 
			
		||||
          signal. If auto_create is callable, use `auto_create(related_object)`.
 | 
			
		||||
 | 
			
		||||
        Be careful with post_to_rel!
 | 
			
		||||
        * There is no check of permissions when related object is synchronised
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user