multifile.py 5.1 KB
Newer Older
stef's avatar
stef committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""
A newforms widget and field to allow multiple file uploads.

Created by Edward Dale (www.scompt.com)
Released into the Public Domain
"""

from django.utils.encoding import force_unicode
from django.utils.datastructures import MultiValueDict
from django.utils.translation import ugettext
from django.forms.fields import Field, EMPTY_VALUES
from django.core.files.uploadedfile import UploadedFile
from django.forms.widgets import FileInput
from django.forms.util import ErrorList, ValidationError, flatatt
stef's avatar
stef committed
15
from django.utils.safestring import mark_safe
stef's avatar
stef committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

class MultiFileInput(FileInput):
    """
    A widget to be used by the MultiFileField to allow the user to upload
    multiple files at one time.
    """

    def __init__(self, attrs=None, *args, **kwargs):
        """
        Create a MultiFileInput.
        The 'count' attribute can be specified to default the number of
        file boxes initially presented.
        """
        super(MultiFileInput, self).__init__(attrs, *args, **kwargs)
        self.attrs = {'count':1}
        if attrs:
            self.attrs.update(attrs)

    def render(self, name, value, attrs=None):
        """
        Renders the MultiFileInput.
        Should not be overridden.  Instead, subclasses should override the
        js, link, and/or fields methods which provide content to this method.
        """
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name+'[]')
        count = final_attrs['count']
        if count<1: count=1
        del final_attrs['count']

        js = self.js(name, value, count, final_attrs)
        link = self.link(name, value, count, final_attrs)
        fields = self.fields(name, value, count, final_attrs)

stef's avatar
stef committed
49
        return mark_safe(js+link+fields)
stef's avatar
stef committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

    def fields(self, name, value, count, attrs=None):
        """
        Renders the necessary number of file input boxes.
        """
        return u''.join([u'<input class="attachments"%s />\n' % flatatt(dict(attrs, id=attrs['id']+str(i))) for i in range(count)])

    def link(self, name, value, count, attrs=None):
        """
        Renders a link to add more file input boxes.
        """
        return u"<a id='add_attach' onclick=\"javascript:new_%(name)s()\">+</a>" % {'name':name}

    def js(self, name, value, count, attrs=None):
        """
        Renders a bit of Javascript to add more file input boxes.
        """
        return u"""
        <script>
        <!--
        %(id)s_counter=%(count)d;
        function new_%(name)s() {
            b=document.getElementById('%(id)s0');
            c=b.cloneNode(false);
            c.id='%(id)s'+(%(id)s_counter++);
            b.parentNode.insertBefore(c,b.parentNode.lastChild.nextSibling);
        }
        -->
        </script>
        """ % {'id':attrs['id'], 'name':name, 'count':count}

    def value_from_datadict(self, data, files, name):
        """
        File widgets take data from FILES, not POST.
        """
        name = name+'[]'
        if isinstance(files, MultiValueDict):
            return files.getlist(name)
        else:
            return None

    def id_for_label(self, id_):
        """
        The first file input box always has a 0 appended to it's id.
        """
        if id_:
            id_ += '0'
        return id_
    id_for_label = classmethod(id_for_label)

class MultiFileField(Field):
    """
    A field allowing users to upload multiple files at once.
    """
    widget = MultiFileInput
    count = 1

    def __init__(self, count=1, strict=False, *args, **kwargs):
        """
        strict is whether the number of files uploaded must equal count
        """
        self.count = count
        self.strict = strict
        super(MultiFileField, self).__init__(*args, **kwargs)

    def widget_attrs(self, widget):
        """
        Adds the count to the MultiFileInput widget.
        """
        if isinstance(widget, MultiFileInput):
            return {'count':self.count}
        return {}

    def clean(self, data):
        """
        Cleans the data and makes sure that all the files had some content.
        Also checks whether a file was required.
        """
        super(MultiFileField, self).clean(data)

        if not self.required and data in EMPTY_VALUES:
            return None
        try:
            f = map(lambda a: UploadedFile(a['filename'], a['content']), data)
        except TypeError:
            raise ValidationError(ugettext(u"No file was submitted. Check the encoding type on the form."))
        except KeyError:
            raise ValidationError(ugettext(u"No file was submitted."))

        for a_file in f:
            if not a_file.content:
                raise ValidationError(ugettext(u"The submitted file is empty."))

        if self.strict and len(f) != self.count:
            raise ValidationError(ugettext(u"An incorrect number of files were uploaded."))

        return f

class FixedMultiFileInput(MultiFileInput):
    """
    A MultiFileInput widget that doesn't print the javascript code to allow
    the user to add more file input boxes.
    """
    def link(self, name, value, count, attrs=None):
        return u''

    def js(self, name, value, count, attrs=None):
        return u''