|  | 
| 7 | 7 | # ----------------------------------------------------------------------------- | 
| 8 | 8 | #   multi_file_upload | 
| 9 | 9 | # ----------------------------------------------------------------------------- | 
| 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): | 
| 12 | 48 |     """ | 
| 13 | 49 |     Displays a Flash-based interface for uploading multiple files. | 
| 14 | 50 |     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 | +
 | 
| 15 | 56 |     """ | 
| 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 | + | 
0 commit comments