Skip to content

Commit 5a7a8d7

Browse files
committed
Add built-in support for django-filer
1 parent 61b5bad commit 5a7a8d7

5 files changed

Lines changed: 167 additions & 0 deletions

File tree

feincms/contents.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,95 @@ def func_im(self, *args, **kwargs):
148148

149149
if cleanse:
150150
cls.cleanse = to_instance_method(cleanse)
151+
152+
153+
try:
154+
from filer.fields.file import FilerFileField
155+
from filer.fields.image import FilerImageField
156+
except ImportError:
157+
pass
158+
else:
159+
160+
class MediaFileContentInline(FeinCMSInline):
161+
radio_fields = {'type': admin.VERTICAL}
162+
163+
class ContentWithFilerFile(models.Model):
164+
"""
165+
MediaFile Content for use with Django-Filer.
166+
"""
167+
feincms_item_editor_inline = MediaFileContentInline
168+
169+
class Meta:
170+
abstract = True
171+
172+
def render(self, **kwargs):
173+
ctx = {'content': self}
174+
ctx.update(kwargs)
175+
return render_to_string([
176+
'content/filer/%s_%s.html' % (self.file_type, self.type),
177+
'content/filer/%s.html' % self.type,
178+
'content/filer/%s.html' % self.file_type,
179+
'content/filer/default.html',
180+
], ctx, context_instance=kwargs.get('context'))
181+
182+
class FileContent(ContentWithFilerFile):
183+
mediafile = FilerFileField(verbose_name=_('file'), related_name='+')
184+
file_type = 'file'
185+
type = 'download'
186+
187+
class Meta:
188+
abstract = True
189+
verbose_name = _('file')
190+
verbose_name_plural = _('files')
191+
192+
class ImageContent(ContentWithFilerFile):
193+
"""
194+
Create a media file content as follows::
195+
196+
from feincms.content.medialibrary.v2 import MediaFileContent
197+
Page.create_content_type(MediaFileContent, TYPE_CHOICES=(
198+
('inline', _('Default')),
199+
('lightbox', _('Lightbox')),
200+
('whatever', _('Whatever')),
201+
))
202+
203+
For a media file of type 'image' and type 'lightbox', the following
204+
templates are tried in order:
205+
206+
* content/mediafile/image_lightbox.html
207+
* content/mediafile/lightbox.html
208+
* content/mediafile/image.html
209+
* content/mediafile/default.html
210+
211+
The context contains ``content`` and ``request`` (if available).
212+
213+
The content.mediafile attribute are as follows (selection):
214+
label, description, default_caption, default_alt_text,
215+
author, must_always_publish_author_credit,
216+
must_always_publish_copyright, date_taken, file, id, is_public, url
217+
"""
218+
219+
mediafile = FilerImageField(verbose_name=_('image'), related_name='+')
220+
file_type = 'image'
221+
222+
class Meta:
223+
abstract = True
224+
verbose_name = _('image')
225+
verbose_name_plural = _('images')
226+
227+
@classmethod
228+
def initialize_type(cls, TYPE_CHOICES=None):
229+
if TYPE_CHOICES is None:
230+
raise ImproperlyConfigured(
231+
'You have to set TYPE_CHOICES when'
232+
' creating a %s' % cls.__name__)
233+
234+
cls.add_to_class(
235+
'type',
236+
models.CharField(
237+
_('type'),
238+
max_length=20,
239+
choices=TYPE_CHOICES,
240+
default=TYPE_CHOICES[0][0],
241+
),
242+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
from django.core.files import File as DjangoFile
4+
from django.core.management.base import NoArgsCommand
5+
from django.contrib.auth.models import User
6+
7+
from feincms.contents import MediaFileContent, FileContent, ImageContent
8+
from feincms.module.medialibrary.models import MediaFile
9+
from feincms.module.page.models import Page
10+
11+
from filer.models import File, Image
12+
13+
14+
PageMediaFileContent = Page.content_type_for(MediaFileContent)
15+
PageFileContent = Page.content_type_for(FileContent)
16+
PageImageContent = Page.content_type_for(ImageContent)
17+
18+
19+
assert (PageMediaFileContent and PageFileContent and PageImageContent),\
20+
'Not all required models available'
21+
22+
23+
class Command(NoArgsCommand):
24+
help = "Migrate the medialibrary and contents to django-filer"
25+
26+
def handle_noargs(self, **options):
27+
user = User.objects.order_by('pk')[0]
28+
29+
count = MediaFile.objects.count()
30+
31+
for i, mediafile in enumerate(MediaFile.objects.order_by('pk')):
32+
model = Image if mediafile.type == 'image' else File
33+
content_model = PageImageContent if mediafile.type == 'image' else PageFileContent # noqa
34+
35+
filerfile = model.objects.create(
36+
owner=user,
37+
original_filename=mediafile.file.name,
38+
file=DjangoFile(
39+
mediafile.file.file,
40+
name=mediafile.file.name,
41+
),
42+
)
43+
44+
contents = PageMediaFileContent.objects.filter(mediafile=mediafile)
45+
46+
for content in contents:
47+
content_model.objects.create(
48+
parent=content.parent,
49+
region=content.region,
50+
ordering=content.ordering,
51+
type=content.type,
52+
mediafile=filerfile,
53+
)
54+
55+
content.delete()
56+
57+
if not i % 10:
58+
self.stdout.write('%s / %s files\n' % (i, count))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% with mediafile=content.mediafile %}
2+
{{ mediafile }}
3+
{% endwith %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% with mediafile=content.mediafile %}
2+
<a href="{{ mediafile.file.url }}">
3+
{{ mediafile.name|default:mediafile.file.name }}
4+
{% if mediafile.description %}
5+
<small>{{ mediafile.description }}</small>
6+
{% endif %}
7+
</a>
8+
{% endwith %}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% load thumbnail %}{% with image=content.mediafile %}
2+
<img src="{% thumbnail image 200x300 crop upscale %}" alt="{{ image.default_alt_text|default_if_none:image.label }}" />
3+
{% if image.default_caption %}<br><span class="caption">{{ image.default_caption }}</span>{% endif %}
4+
{% endwith %}
5+
6+
{#{% thumbnail obj.img 200x300 crop upscale subject_location=obj.img.subject_location %}#}

0 commit comments

Comments
 (0)