Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CloudinaryInput extend ClearableFileInput and behave more like it #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cloudinary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net"
AKAMAI_SHARED_CDN = "res.cloudinary.com"
SHARED_CDN = AKAMAI_SHARED_CDN
UPLOAD_FIELD_NAME = "file"

from cloudinary import utils

Expand Down
25 changes: 20 additions & 5 deletions cloudinary/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import forms
from cloudinary import CloudinaryImage
from django.forms import widgets
from cloudinary import CloudinaryImage, UPLOAD_FIELD_NAME
import cloudinary.uploader
import cloudinary.utils
import re
Expand All @@ -11,8 +12,8 @@ def cl_init_js_callbacks(form, request):
if (isinstance(field, CloudinaryJsFileField)):
field.enable_callback(request)

class CloudinaryInput(forms.TextInput):
input_type = 'file'
class CloudinaryInput(forms.ClearableFileInput):
template_with_initial = u'<span class="initial-text">%(initial_text)s: </span><span class="initial-value">%(initial)s</span> %(clear_template)s<br />%(input_text)s: %(input)s'

def render(self, name, value, attrs=None):
self.build_attrs(attrs)
Expand All @@ -29,10 +30,24 @@ def render(self, name, value, attrs=None):
attrs["data-cloudinary-field"] = name
attrs["class"] = " ".join(["cloudinary-fileupload", self.attrs.get("class", "")])

return super(CloudinaryInput, self).render("file", None, attrs=attrs)
return super(CloudinaryInput, self).render(UPLOAD_FIELD_NAME, value, attrs=attrs)

# From django.forms.widgets.ClearableFileInput
def value_from_datadict(self, data, files, name):
upload = widgets.Widget().value_from_datadict(data, files, name)
if not self.is_required and widgets.CheckboxInput().value_from_datadict(
data, files, self.clear_checkbox_name(name)):
if upload:
# If the user contradicts themselves (uploads a new file AND
# checks the "clear" checkbox), we return a unique marker
# object that FileField will turn into a ValidationError.
return widgets.FILE_INPUT_CONTRADICTION
# False signals to clear any existing value, as opposed to just None
return False
return upload

class CloudinaryJsFileField(forms.Field):

class CloudinaryJsFileField(forms.FileField):
default_error_messages = {
'required': _(u"No image selected!")
}
Expand Down