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