PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/example/models.py

http://github.com/feincms/feincms
Python | 103 lines | 90 code | 10 blank | 3 comment | 0 complexity | 968d2710907c0be8c1e35e8ef6fcbf9f MD5 | raw file
  1from django import forms
  2from django.db import models
  3from django.utils.text import capfirst
  4from django.utils.translation import ugettext_lazy as _
  5
  6from feincms.module.blog.models import Entry, EntryAdmin
  7from feincms.module.page.models import Page
  8from feincms.content.raw.models import RawContent
  9from feincms.content.image.models import ImageContent
 10from feincms.content.medialibrary.models import MediaFileContent
 11from feincms.content.application.models import ApplicationContent
 12from feincms.module.page.extensions.navigation import NavigationExtension, PagePretender
 13from feincms.content.application.models import reverse
 14
 15import mptt
 16
 17
 18Page.register_templates({
 19    'key': 'base',
 20    'title': 'Base Template',
 21    'path': 'base.html',
 22    'regions': (
 23        ('main', 'Main region'),
 24        ('sidebar', 'Sidebar', 'inherited'),
 25        ),
 26    })
 27Page.create_content_type(RawContent)
 28MediaFileContent.default_create_content_type(Page)
 29Page.create_content_type(ImageContent, POSITION_CHOICES=(
 30    ('default', 'Default position'),
 31    ))
 32
 33def get_admin_fields(form, *args, **kwargs):
 34    return {
 35        'exclusive_subpages': forms.BooleanField(
 36            label=capfirst(_('exclusive subpages')),
 37            required=False,
 38            initial=form.instance.parameters.get('exclusive_subpages', False),
 39            help_text=_('Exclude everything other than the application\'s content when rendering subpages.'),
 40            ),
 41    }
 42
 43Page.create_content_type(ApplicationContent, APPLICATIONS=(
 44    ('blog_urls', 'Blog', {'admin_fields': get_admin_fields}),
 45    ))
 46
 47
 48Entry.register_regions(
 49    ('main', 'Main region'),
 50    )
 51Entry.create_content_type(RawContent)
 52Entry.create_content_type(ImageContent, POSITION_CHOICES=(
 53    ('default', 'Default position'),
 54    ))
 55
 56
 57class BlogEntriesNavigationExtension(NavigationExtension):
 58    """
 59    Extended navigation for blog entries.
 60
 61    It would be added to 'Blog' page properties in admin.
 62    """
 63    name = _('all blog entries')
 64
 65    def children(self, page, **kwargs):
 66        for entry in Entry.objects.all():
 67            yield PagePretender(
 68                title=entry.title,
 69                url=reverse('blog_urls/blog_entry_details', kwargs={'object_id': entry.id}),
 70                )
 71
 72Page.register_extensions('navigation')
 73Page.register_extensions('sites')
 74
 75
 76try:
 77    from mptt.models import MPTTModel as base
 78    mptt_register = False
 79except ImportError:
 80    base = models.Model
 81    mptt_register = True
 82
 83class Category(base):
 84    name = models.CharField(max_length=20)
 85    slug = models.SlugField()
 86    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
 87
 88    class Meta:
 89        ordering = ['tree_id', 'lft']
 90        verbose_name = 'category'
 91        verbose_name_plural = 'categories'
 92
 93    def __unicode__(self):
 94        return self.name
 95
 96if mptt_register:
 97    mptt.register(Category)
 98
 99# add m2m field to entry so it shows up in entry admin
100Entry.add_to_class('categories', models.ManyToManyField(Category, blank=True, null=True))
101EntryAdmin.list_filter += ('categories',)
102
103