-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
105 lines (91 loc) · 3.69 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
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
from django.shortcuts import get_object_or_404, render_to_response
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
from models import MultiuploaderImage
from django.core.files.uploadedfile import UploadedFile
#importing json parser to generate jQuery plugin friendly json response
from django.utils import simplejson
#for generating thumbnails
#sorl-thumbnails must be installed and properly configured
from sorl.thumbnail import get_thumbnail
from django.views.decorators.csrf import csrf_exempt
import logging
log = logging
@csrf_exempt
def multiuploader_delete(request, pk):
"""
View for deleting photos with multiuploader AJAX plugin.
made from api on:
https://github.com/blueimp/jQuery-File-Upload
"""
if request.method == 'POST':
log.info('Called delete image. image id='+str(pk))
image = get_object_or_404(MultiuploaderImage, pk=pk)
image.delete()
log.info('DONE. Deleted photo id='+str(pk))
return HttpResponse(str(pk))
else:
log.info('Received not POST request to delete image view')
return HttpResponseBadRequest('Only POST accepted')
@csrf_exempt
def multiuploader(request):
"""
Main Multiuploader module.
Parses data from jQuery plugin and makes database changes.
"""
if request.method == 'POST':
log.info('received POST to main multiuploader view')
if request.FILES == None:
return HttpResponseBadRequest('Must have files attached!')
#getting file data for farther manipulations
file = request.FILES[u'files[]']
wrapped_file = UploadedFile(file)
filename = wrapped_file.name
file_size = wrapped_file.file.size
log.info ('Got file: "%s"' % str(filename))
log.info('Content type: "$s" % file.content_type')
#writing file manually into model
#because we don't need form of any type.
image = MultiuploaderImage()
image.filename=str(filename)
image.image=file
image.key_data = image.key_generate
image.save()
log.info('File saving done')
#getting thumbnail url using sorl-thumbnail
if 'image' in file.content_type.lower():
im = get_thumbnail(image, "80x80", quality=50)
thumb_url = im.url
else:
thumb_url = ''
#settings imports
try:
file_delete_url = settings.MULTI_FILE_DELETE_URL+'/'
file_url = settings.MULTI_IMAGE_URL+'/'+image.key_data+'/'
except AttributeError:
file_delete_url = 'multi_delete/'
file_url = 'multi_image/'+image.key_data+'/'
#generating json response array
result = []
result.append({"name":filename,
"size":file_size,
"url":file_url,
"thumbnail_url":thumb_url,
"delete_url":file_delete_url+str(image.pk)+'/',
"delete_type":"POST",})
response_data = simplejson.dumps(result)
#checking for json data type
#big thanks to Guy Shapiro
if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
mimetype = 'application/json'
else:
mimetype = 'text/plain'
return HttpResponse(response_data, mimetype=mimetype)
else: #GET
return HttpResponse('Only POST accepted')
def multi_show_uploaded(request, key):
"""Simple file view helper.
Used to show uploaded file directly"""
image = get_object_or_404(MultiuploaderImage, key_data=key)
url = settings.MEDIA_URL+image.image.name
return render_to_response('multiuploader/one_image.html', {"multi_single_url":url,})