Skip to content

Added configurable accept types #21

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

Open
wants to merge 4 commits 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
3 changes: 3 additions & 0 deletions ajax_upload/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@


FILE_FIELD_MAX_LENGTH = getattr(settings, 'AJAX_UPLOAD_FILE_FIELD_MAX_LENGTH', 255)
FILE_FIELD_ACCEPT_TYPES = getattr(settings, 'AJAX_UPLOAD_FILE_FIELD_ACCEPT_TYPES', None)
UPLOAD_PERMISSION_CHECKER = getattr(settings, 'AJAX_UPLOAD_PERMISSION_CHECKER', None)

5 changes: 4 additions & 1 deletion ajax_upload/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from django.views.decorators.http import require_POST

from ajax_upload.forms import UploadedFileForm

from ajax_upload.settings import UPLOAD_PERMISSION_CHECKER as permission_checker

@csrf_exempt
@require_POST
def upload(request):
if callable(permission_checker):
if not permission_checker(request):
return HttpResponseBadRequest(json.dumps({'errors': 'Upload not permitted'}))
form = UploadedFileForm(data=request.POST, files=request.FILES)
if form.is_valid():
uploaded_file = form.save()
Expand Down
5 changes: 5 additions & 0 deletions ajax_upload/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import urllib2

from ajax_upload.models import UploadedFile
from ajax_upload.settings import FILE_FIELD_ACCEPT_TYPES


class AjaxUploadException(Exception):
Expand All @@ -30,6 +31,10 @@ def render(self, name, value, attrs=None):
'data-required': self.is_required or '',
'data-upload-url': reverse('ajax-upload')
})
if FILE_FIELD_ACCEPT_TYPES is not None:
attrs.update({
'accept': FILE_FIELD_ACCEPT_TYPES
})
output = super(AjaxClearableFileInput, self).render(name, value, attrs)
return mark_safe(output)

Expand Down