Skip to content

Commit

Permalink
initial batch uploader commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ninapavlich committed Sep 3, 2015
1 parent 2809e8a commit f7876eb
Show file tree
Hide file tree
Showing 15 changed files with 1,171 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -55,3 +55,6 @@ docs/_build/

# PyBuilder
target/


*.DS_Store
1 change: 1 addition & 0 deletions LICENSE.txt
@@ -0,0 +1 @@
Copyright 2015 C&G Partners LLC
2 changes: 2 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,2 @@
include README.rst
recursive-include media/ *
57 changes: 55 additions & 2 deletions README.md
@@ -1,2 +1,55 @@
# django-bulk-uploader
Django bulk uploading
# django-batch-uploader
Django batch uploading


#settings.py

INSTALLED_APPS = (
...
'django_batch_uploader',
...
)

#views.py

from django_batch_uploader.views import BaseBatchUploadView

class ImageBatchView(BaseBatchUploadView):

model = Image

#Same form used in admin.py
form_class = ImageAddForm

#Media file name
media_file_name = 'image'

#All possible editable fields
fields = ['title','credit', 'caption', 'alt', 'admin_description',
'use_png', 'is_searchable', 'clean_filename_on_upload',
'allow_file_to_override', 'creator', 'tags']


#Which fields can be applied in bulk
default_fields = ['credit', 'caption', 'admin_description', 'use_png',
'is_searchable', 'clean_filename_on_upload', 'allow_file_to_override',
'creator', 'tags']

#Which fields can be applied invididually
detail_fields = ['title', 'alt', 'credit', 'caption', 'admin_description',
'use_png', 'is_searchable', 'clean_filename_on_upload',
'allow_file_to_override', 'creator', 'tags']


#urls.py

....
url( r'admin/media/images/batch/$', ImageBatchView.as_view(), name="admin_image_batch_view"),
....


#admin.py

....
url( r'admin/media/images/batch/$', ImageBatchView.as_view(), name="admin_image_batch_view"),
....
Empty file added __init__.py
Empty file.
Empty file.
76 changes: 76 additions & 0 deletions django_batch_uploader/admin.py
@@ -0,0 +1,76 @@
import json
from mimetypes import MimeTypes
import urllib

from django.contrib import admin
from django.contrib.admin.models import LogEntry, ADDITION
from django.contrib.admin.util import flatten_fieldsets
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.http import HttpResponse


from .utils import get_media_file_name

class BaseBatchUploadAdmin(admin.ModelAdmin):

#Add batch_url to context
#batch_url_name = 'admin_image_batch_view'
change_list_template = "admin/batch_change_list.html"

def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}

if hasattr(self, 'batch_url_name'):
extra_context['batch_url_name'] = self.batch_url_name
extra_context['batch_url'] = reverse(self.batch_url_name)

return super(BaseBatchUploadAdmin, self).changelist_view(request, extra_context=extra_context)


def add_view(self, request, form_url='', extra_context=None):
default_response = super(BaseBatchUploadAdmin, self).add_view(request, form_url, extra_context)

if request.method == 'POST' and "batch" in request.POST:

response = self.batch_upload_response(request)
if response != None:
return response

return default_response

def batch_upload_response(self, request):

output_fields = flatten_fieldsets(self.fieldsets)
media_file_name = get_media_file_name(self, self.model)

try:
latest_log_entry = LogEntry.objects.filter(action_flag=ADDITION).order_by('-action_time')[0]
ct = ContentType.objects.get_for_id(latest_log_entry.content_type_id)
obj = ct.get_object_for_this_type(pk=latest_log_entry.object_id)
if obj:

object_data = {}

mime = MimeTypes()
media_file = getattr(obj, media_file_name)
url = urllib.pathname2url(media_file.url)

mime_type = mime.guess_type(url)
edit_url = reverse('admin:%s_%s_change' %(obj._meta.app_label, obj._meta.model_name), args=[obj.id] )
data = {
"files":[
{
"url": obj.image_url,
"edit_url": edit_url,
"thumbnailUrl": obj.thumbnail_url,
"name": obj.title,
"type": mime_type[0],
"size": obj.image.size
}
]
}
return HttpResponse(json.dumps(data), content_type='application/json')
except:
return None
3 changes: 3 additions & 0 deletions django_batch_uploader/static/batch/css/batchupload.css
@@ -0,0 +1,3 @@
.grp-row textarea{
width: 278px !important;
}

0 comments on commit f7876eb

Please sign in to comment.