def test_upload(self): i1 = Image() i1.title = "aaa" i1.thumb = File(open("static/images/comments4.png")) i1.save() p = Image.objects.get(id=1).thumb.path self.failUnless(open(p), 'file not found')
def download(request, id): remote_file, path = RemoteModel().download(id) image = Image() image.title = remote_file.title image.pathname = path image.save_remote() return HttpResponseRedirect(reverse('local_list'))
def create(self, request, *args, **kwargs): image = Image() image.title = request.data['title'] image.format = request.data['format'] image.file = request.data['file'] image.date_created = timezone.now() image.date_updated = timezone.now() #return super().create(request, *args, **kwargs) image.save() return Response({"message": "Image entry successful"}, status=status.HTTP_201_CREATED)
def multiuploader(request): """ Main Multiuploader module. Parses data from jQuery plugin and makes database changes. """ """ CL Instead of using MultiuploaderImage as the model, need to use our own Image model. """ user = User.objects.filter(username=request.user)[0] 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') image = Image() image.filename = str(filename) image.image = file image.title = "" image.caption = "" image.owner = user 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+'/' except AttributeError: file_delete_url = 'multi_delete/' file_url = 'multi_image/' # file_url='image/1/edit' # becomes /upload/image/1/edit file_url = '/account/image/' + str(image.id) + '/edit' #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')