Skip to content

Commit 5af9b5c

Browse files
committed
all uploadify settings in tag, widget
1 parent 68ccd6e commit 5af9b5c

File tree

3 files changed

+93
-44
lines changed

3 files changed

+93
-44
lines changed

uploadify/templates/uploadify/multi_file_upload.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<link rel="stylesheet" type="text/css" href="{{ uploadify_path }}uploadify.css" />
44
<div id="uploadify" class="multi_file_upload">
55
<input id="fileInput" name="fileInput" type="file" />
6+
{% if not uploadify_auto %}
67
<input type="button" value="Upload" id="upload" />
8+
{% endif %}
79
</div>
810
<script type="text/javascript">// <![CDATA[
911
$(document).ready(function() {
@@ -17,12 +19,12 @@
1719
'script': '{% url uploadify_upload %}{{ uploadify_query }}',
1820
'scriptData': {{ uploadify_data|safe }},
1921
'cancelImg': '{{ uploadify_path }}cancel.png',
20-
'fileDataName': '{{ uploadify_filename }}',
2122
'removeCompleted': false,
2223
'multi': true,
2324
'onAllComplete': uploadify_allComplete,
2425
'onCancel': uploadify_cancelUpload,
25-
'onComplete': uploadify_complete
26+
'onComplete': uploadify_complete,
27+
{{ uploadify_options|safe }}
2628
});
2729

2830
$('#upload').click(function(){
@@ -45,7 +47,7 @@
4547

4648
var file_percentage = file_div.find('.percentage');
4749
file_percentage.text(' -');
48-
$(response.{{ uploadify_filename }}).each(function(){
50+
$(response.{{ uploadify_filename|safe }}).each(function(){
4951
file_percentage.text(file_percentage.text()+" "+this);
5052
});
5153

uploadify/templatetags/uploadify_tags.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,70 @@
77
# -----------------------------------------------------------------------------
88
# multi_file_upload
99
# -----------------------------------------------------------------------------
10-
@register.inclusion_tag('uploadify/multi_file_upload.html', takes_context=True)
11-
def multi_file_upload(context, sender=None, filename='Filename', unique_id=None):
10+
class MultiFileUpload(template.Node):
11+
def __init__(self, sender='uploadify', unique_id=None, options={}, data={}):
12+
self.sender = sender
13+
self.unique_id = unique_id
14+
self.options = {'fileDataName': 'Filedata'}
15+
self.options.update(options)
16+
self.data = {'fileDataName': self.options['fileDataName'],
17+
'sender': str(self.sender)}
18+
self.data.update(data)
19+
20+
def render(self, context):
21+
if self.unique_id is not None:
22+
unique_id = "?unique_id=%s" % str(self.unique_id)
23+
else:
24+
unique_id = ""
25+
26+
js_options = ",".join(map(lambda k: "'%s': '%s'" % (k, self.options[k]),
27+
self.options))
28+
29+
auto = False
30+
if self.options.has_key('auto') and self.options['auto']:
31+
auto = True
32+
33+
context.update({
34+
'uploadify_query': unique_id,
35+
'uploadify_data': simplejson.dumps(self.data),
36+
'uploadify_path': settings.UPLOADIFY_PATH,
37+
'uploadify_options': js_options,
38+
'uploadify_filename': self.options['fileDataName'],
39+
'uploadify_auto': auto,
40+
})
41+
42+
t = template.loader.get_template('uploadify/multi_file_upload.html')
43+
return t.render(context)
44+
45+
46+
@register.tag
47+
def multi_file_upload(parser, token):
1248
"""
1349
Displays a Flash-based interface for uploading multiple files.
1450
For each POST request (after file upload) send GET query with `unique_id`.
51+
52+
{% multi_file_upload sender='SomeThing' fileDataName='Filename' %}
53+
54+
For all options see http://www.uploadify.com/documentation/
55+
1556
"""
16-
data = {'fileDataName': filename}
17-
18-
if sender is not None:
19-
data['sender'] = str(sender)
20-
21-
if unique_id is not None:
22-
unique_id = "?unique_id=%s" % str(unique_id)
23-
else:
24-
unique_id = ""
25-
26-
return {
27-
'uploadify_query' : unique_id,
28-
'uploadify_data' : simplejson.dumps(data),
29-
'uploadify_path' : settings.UPLOADIFY_PATH,
30-
'uploadify_filename' : filename,
31-
}
57+
args = token.split_contents()
58+
tag_name = args[0]
59+
args = args[1:]
60+
61+
sender = 'uploadify'
62+
unique_id = None
63+
options = {}
64+
65+
for arg in args:
66+
name, val = arg.split("=")
67+
val = val.replace('\'', '').replace('"', '')
68+
if name == 'sender':
69+
sender = val
70+
elif name == 'unique_id':
71+
unique_id = val
72+
else:
73+
options[name] = val
74+
75+
return MultiFileUpload(sender, unique_id, options)
76+

uploadify/widgets.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,46 @@
22

33
from uploadify import settings
44
from django.contrib.admin.widgets import AdminFileWidget
5-
from django.template import RequestContext
6-
from django.utils import simplejson
7-
from django.template import loader
5+
from uploadify.templatetags.uploadify_tags import MultiFileUpload
86
from django.utils.safestring import mark_safe
7+
from django.template import Context
98

109
class UploadifyAdminWidget(AdminFileWidget):
10+
"""
11+
Create uploadify widget. It receive:
12+
'sender' - string (default = uploadify)
13+
'unique_id'
14+
'options' - http://www.uploadify.com/documentation/
15+
'data' - POST data
16+
17+
Example:
18+
self.fields['uploadify'].widget = UploadifyAdminWidget(
19+
attrs={'data': post_data, 'options': {'fileDataName': 'image'}})
20+
"""
21+
1122
js = """
1223
<script type="text/javascript">
1324
$('#uploadify').bind('allUploadsComplete', function(data){
1425
window.location.reload();
1526
});
1627
</script>"""
28+
1729
def __init__(self, *args, **kwargs):
30+
self.sender = 'uploadify'
31+
self.unique_id = None
32+
self.options = {}
1833
self.data = {}
19-
self.filename = 'Filename'
34+
2035
if kwargs.has_key('attrs'):
21-
if kwargs['attrs'].has_key('data'):
22-
self.data = kwargs['attrs']['data']
23-
del kwargs['attrs']['data']
24-
if kwargs['attrs'].has_key('filename'):
25-
self.filename = kwargs['attrs']['filename']
26-
del kwargs['attrs']['filename']
36+
for attr in kwargs['attrs']:
37+
if attr in ('sender', 'unique_id', 'options', 'data'):
38+
setattr(self, attr, kwargs['attrs'][attr])
2739
super(UploadifyAdminWidget, self).__init__(*args, **kwargs)
2840

2941
def render(self, name, value, attrs=None):
30-
31-
self.data['fileDataName'] = self.filename
32-
33-
output = {
34-
'uploadify_data' : simplejson.dumps(self.data),
35-
'uploadify_path' : settings.UPLOADIFY_PATH,
36-
'uploadify_filename' : self.filename,
37-
}
38-
39-
template = 'uploadify/multi_file_upload.html'
40-
41-
template_data = loader.render_to_string(template, output)
42-
42+
context = Context({})
43+
uploader = MultiFileUpload(self.sender, self.unique_id, self.options, self.data)
44+
template_data = uploader.render(context)
4345
template_data += self.js
4446

4547
return mark_safe(template_data)

0 commit comments

Comments
 (0)