Skip to content

Commit 26325e3

Browse files
committed
added file templates
1 parent 55ed790 commit 26325e3

File tree

6 files changed

+83
-35
lines changed

6 files changed

+83
-35
lines changed

djangocms_file/cms_plugins.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ class FilePlugin(CMSPluginBase):
2424
(_('Advanced settings'), {
2525
'classes': ('collapse',),
2626
'fields': (
27+
'template',
2728
('link_target', 'link_title'),
2829
'show_file_size',
2930
'attributes',
3031
)
3132
}),
3233
]
3334

35+
def get_render_template(self, context, instance, placeholder):
36+
return 'djangocms_file/{}/file.html'.format(instance.template)
37+
3438

3539
class FolderPlugin(CMSPluginBase):
3640
model = Folder
@@ -40,13 +44,13 @@ class FolderPlugin(CMSPluginBase):
4044
fieldsets = [
4145
(None, {
4246
'fields': (
43-
'template',
4447
'folder_src',
4548
)
4649
}),
4750
(_('Advanced settings'), {
4851
'classes': ('collapse',),
4952
'fields': (
53+
'template',
5054
'link_target',
5155
'show_file_size',
5256
'attributes',

djangocms_file/migrations/0007_adapted_fields.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Migration(migrations.Migration):
1919
name='attributes',
2020
field=djangocms_attributes_field.fields.AttributesField(default=dict, verbose_name='Attributes', blank=True),
2121
),
22+
migrations.AddField(
23+
model_name='file',
24+
name='template',
25+
field=models.CharField(default=b'default', max_length=255, verbose_name='Template', choices=[(b'default', 'Default')]),
26+
),
2227
migrations.RenameField(
2328
model_name='file',
2429
old_name='title',

djangocms_file/models.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ class File(CMSPlugin):
4040
Renders a file wrapped by an anchor
4141
"""
4242
search_fields = ('name',)
43+
TEMPLATE_CHOICES = [
44+
('default', _('Default')),
45+
]
4346

47+
template = models.CharField(
48+
verbose_name=_('Template'),
49+
choices=TEMPLATE_CHOICES + get_templates(),
50+
default=TEMPLATE_CHOICES[0][0],
51+
max_length=255,
52+
)
4453
file_src = FilerFileField(
4554
verbose_name=_('File'),
4655
blank=False,
@@ -75,7 +84,7 @@ class File(CMSPlugin):
7584
attributes = AttributesField(
7685
verbose_name=_('Attributes'),
7786
blank=True,
78-
excluded_keys=['title'],
87+
excluded_keys=['title', 'target'],
7988
)
8089

8190
# Add an app namespace to related_name to avoid field name clashes
@@ -103,7 +112,6 @@ class Folder(CMSPlugin):
103112
('default', _('Default')),
104113
]
105114

106-
# The label will be displayed as help text in the structure board view.
107115
template = models.CharField(
108116
verbose_name=_('Template'),
109117
choices=TEMPLATE_CHOICES + get_templates(),
@@ -133,7 +141,7 @@ class Folder(CMSPlugin):
133141
attributes = AttributesField(
134142
verbose_name=_('Attributes'),
135143
blank=True,
136-
excluded_keys=['title'],
144+
excluded_keys=['target'],
137145
)
138146

139147
# Add an app namespace to related_name to avoid field name clashes
@@ -147,6 +155,13 @@ class Folder(CMSPlugin):
147155
)
148156

149157
def __str__(self):
150-
if self.folder_src and self.folder_src.label:
151-
return self.folder_src.label
158+
if self.folder_src and self.folder_src.name:
159+
return self.folder_src.name
152160
return ugettext('<folder is missing>')
161+
162+
def get_files(self):
163+
folder_files = []
164+
165+
for folder in self.folder_src.files:
166+
folder_files.append(folder)
167+
return folder_files
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<a href="{{ instance.file_src.url }}"
2+
{% if instance.link_target %} target="{{ instance.link_target }}"{% endif %}
3+
{% if instance.link_title %} title="{{ instance.link_title }}"{% endif %}
4+
{{ instance.attributes_str }}>
5+
{% if instance.file_name %}
6+
{{ instance.file_name }}
7+
{% else %}
8+
{{ instance.file_src.name }}
9+
{% endif %}
10+
{% if instance.show_file_size %}
11+
<span>{{ intsance.file_src.size|filesizeformat }}</span>
12+
{% endif %}
13+
</a>
14+
15+
{% comment %}
16+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
17+
# https://github.com/divio/django-filer/blob/master/filer/models/filemodels.py
18+
{{ instance.file_src }}
19+
{{ instance.file_src.extension }}
20+
# Available variables:
21+
{{ instance.template }}
22+
{{ instance.file_name }}
23+
{{ instance.link_target }}
24+
{{ instance.link_title }}
25+
{{ instance.show_file_size }}
26+
{{ instance.attributes_str }}
27+
{% endcomment %}
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
<ul>
2-
<li>
3-
test
4-
</li>
5-
</ul>
1+
{% load i18n %}
2+
3+
{% for file in instance.get_files %}
4+
<a href="{{ file.url }}"
5+
{% if instance.link_target %} target="{{ instance.link_target }}"{% endif %}
6+
{{ instance.attributes_str }}>
7+
{{ file }}
8+
{% if instance.show_file_size %}
9+
<span>{{ file.size|filesizeformat }}</span>
10+
{% endif %}
11+
</a>
12+
{% empty %}
13+
<p>{% trans "No files were found in the specified folder." %}</p>
14+
{% endfor %}
15+
16+
{% comment %}
17+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
18+
# https://github.com/divio/django-filer/blob/master/filer/models/filemodels.py
19+
{{ instance.folder_src }}
20+
{{ instance.get_files }}
21+
# Available variables:
22+
{{ instance.template }}
23+
{{ instance.link_target }}
24+
{{ instance.show_file_size }}
25+
{{ instance.attributes_str }}
26+
{% endcomment %}

djangocms_file/templates/djangocms_file/file.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)