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

/feincms/content/medialibrary/v2.py

http://github.com/feincms/feincms
Python | 63 lines | 50 code | 5 blank | 8 comment | 0 complexity | 1668f716353ad8581920afc47c7c384a MD5 | raw file
 1from django.contrib import admin
 2from django.db import models
 3from django.template.loader import render_to_string
 4from django.utils.translation import ugettext_lazy as _
 5
 6from feincms.admin.item_editor import FeinCMSInline
 7from feincms.module.medialibrary.fields import ContentWithMediaFile
 8
 9
10class MediaFileContentInline(FeinCMSInline):
11    raw_id_fields = ('mediafile',)
12    radio_fields = {'type': admin.VERTICAL}
13
14
15class MediaFileContent(ContentWithMediaFile):
16    """
17    Rehashed, backwards-incompatible media file content which does not contain
18    the problems from v1 anymore.
19
20    Create a media file content as follows::
21
22        from feincms.content.medialibrary.v2 import MediaFileContent
23        Page.create_content_type(MediaFileContent, TYPE_CHOICES=(
24            ('default', _('Default')),
25            ('lightbox', _('Lightbox')),
26            ('whatever', _('Whatever')),
27            ))
28
29    For a media file of type 'image' and type 'lightbox', the following
30    templates are tried in order:
31
32    * content/mediafile/image_lightbox.html
33    * content/mediafile/image.html
34    * content/mediafile/lightbox.html
35    * content/mediafile/default.html
36
37    The context contains ``content`` and ``request`` (if available).
38    """
39
40    feincms_item_editor_inline = MediaFileContentInline
41
42    class Meta:
43        abstract = True
44        verbose_name = _('media file')
45        verbose_name_plural = _('media files')
46
47    @classmethod
48    def initialize_type(cls, TYPE_CHOICES=None):
49        cls.add_to_class('type', models.CharField(_('type'),
50            max_length=20, choices=TYPE_CHOICES, default=TYPE_CHOICES[0][0]))
51
52    def render(self, **kwargs):
53        ctx = {'content': self}
54        ctx.update(kwargs)
55
56        return render_to_string([
57            'content/mediafile/%s_%s.html' % (self.mediafile.type, self.type),
58            'content/mediafile/%s.html' % self.mediafile.type,
59            'content/mediafile/%s.html' % self.type,
60            'content/mediafile/default.html',
61            ], ctx)
62
63