Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Patched middleware, incorportaing pull requests

*Middleware now supports multiple files
*Fixed up pull requests that created FPUrlField
  • Loading branch information...
commit faa09aef4447f44ef91bc775e2cdedf99d4428b1 1 parent c43a80f
Brett van Zuiden brettcvz authored
4 demo/filepicker_demo/views.py
View
@@ -22,8 +22,8 @@ def home(request):
print fpfile.read()
message = "Save successful. URL for %s: %s" % (fpfile.name, request.POST['fpfile'])
-
- message = "Invalid form"
+ else:
+ message = "Invalid form"
else:
form = models.TestModelForm()
37 django_filepicker/forms.py
View
@@ -10,13 +10,13 @@
from StringIO import StringIO
-class FPUrlField(forms.URLField):
+class FPFieldMixin():
widget = FPFileWidget
default_mimetypes = "*/*"
- def __init__(self, *args, **kwargs):
+ def initialize(self, *args, **kwargs):
"""
- Initializes the Filepicker file field.
+ Initializes the Filepicker field.
Valid arguments:
* apikey. This string is required if it isn't set as settings.FILEPICKER_API_KEY
* mimetypes. Optional, the allowed mimetypes for files. Defaults to "*/*" (all files)
@@ -42,8 +42,6 @@ def __init__(self, *args, **kwargs):
self.services = kwargs.pop('services', getattr(settings, 'FILEPICKER_SERVICES', None))
- super(FPUrlField, self).__init__(*args, **kwargs)
-
def widget_attrs(self, widget):
attrs = {
'data-fp-apikey': self.apikey,
@@ -56,7 +54,34 @@ def widget_attrs(self, widget):
return attrs
-class FPFileField(FPUrlField, forms.FileField):
+class FPUrlField(FPFieldMixin, forms.URLField):
+ widget = FPFileWidget
+ default_mimetypes = "*/*"
+
+ def __init__(self, *args, **kwargs):
+ """
+ Initializes the Filepicker url field.
+ Valid arguments:
+ * apikey. This string is required if it isn't set as settings.FILEPICKER_API_KEY
+ * mimetypes. Optional, the allowed mimetypes for files. Defaults to "*/*" (all files)
+ * services. Optional, the allowed services to pull from.
+ """
+ self.initialize(*args, **kwargs)
+ super(FPUrlField, self).__init__(*args, **kwargs)
+
+
+class FPFileField(FPFieldMixin, forms.FileField):
+ def __init__(self, *args, **kwargs):
+ """
+ Initializes the Filepicker url field.
+ Valid arguments:
+ * apikey. This string is required if it isn't set as settings.FILEPICKER_API_KEY
+ * mimetypes. Optional, the allowed mimetypes for files. Defaults to "*/*" (all files)
+ * services. Optional, the allowed services to pull from.
+ """
+ self.initialize(*args, **kwargs)
+ super(FPFileField, self).__init__(*args, **kwargs)
+
def to_python(self, data):
"""Takes the url in data and creates a File object"""
if not data or not data.startswith('http'):
22 django_filepicker/middleware.py
View
@@ -27,14 +27,20 @@ def process_request(self, request):
#Iterate over GET or POST data, search for filepicker.io urls
for key, val in request.POST.items():
if self.isFilepickerURL(val):
- url_fp = urllib2.urlopen(val)
- disposition = url_fp.info().getheader('Content-Disposition')
- if disposition:
- name = disposition.rpartition("filename=")[2].strip('" ')
- else:
- name = "fp-file"
-
- request.FILES[key] = File(StringIO(url_fp.read()), name=name)
+ splits = val.split(",")
+ for url in splits:
+ url_fp = urllib2.urlopen(url)
+ disposition = url_fp.info().getheader('Content-Disposition')
+ if disposition:
+ name = disposition.rpartition("filename=")[2].strip('" ')
+ else:
+ name = "fp-file"
+
+ fp = File(StringIO(url_fp.read()), name=name)
+ if key in request.FILES:
+ request.FILES.setlist(key, list(request.FILES.getlist(key) + [fp]))
+ else:
+ request.FILES[key] = fp
def isFilepickerURL(self, val):
return bool(self.filepicker_url_regex.match(val))
Please sign in to comment.
Something went wrong with that request. Please try again.