def createFabric(request): valid = request.GET['Visible'].lower() == 'true' if request.GET.has_key('Visible') else False # Default = False name = request.FILES['qqfile'].name try: categoryId = request.GET['CID'] category = FabricCategory.objects.get(id = categoryId) except ObjectDoesNotExist: variables = {'success': False, 'reason': 'NoCategory'} return HttpResponse(json.dumps(variables), mimetype='application/json') user = request.user if user.is_anonymous(): variables = {'success': False, 'reason': 'Anonymous'} return HttpResponse(json.dumps(variables), mimetype='application/json') else: try: orderValue = Fabric.objects.filter(category = category).aggregate(Min('orderValue'))['orderValue__min'] - 1 except: orderValue = 0 fabric = Fabric(name = name, valid = valid, user = user, category = category, orderValue = orderValue) fabric.save() IMAGE_EXTENSION = os.path.splitext(name)[-1] fabricImage = request.FILES['qqfile'] image_filename = 'data/fabric_images/' + str(fabric.id/10000) + '/' + str(fabric.id) + IMAGE_EXTENSION fabric.image.save(image_filename, ContentFile(fabricImage.read())) img = Image.open(StringIO(fabric.image.read())) try: fabric.dpi = img.info['dpi'][0] except KeyError: pass fabric.width, fabric.height = img.size THUMBNAIL_SIZE = (115, 115) IMAGE_TYPE = imghdr.what(fabric.image.path) img.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) temp_handle = StringIO() img.save(temp_handle, IMAGE_TYPE) temp_handle.seek(0) thumb_filename = 'data/fabric_thumbs/' + str(fabric.id/10000) + '/' + str(fabric.id) + IMAGE_EXTENSION fabric.thumb.save(thumb_filename, ContentFile(temp_handle.read())) thum_url = settings.MEDIA_URL + image_filename responsePath = settings.MEDIA_URL + thumb_filename variables = {'success': True, 'id': fabric.id, 'filepath': responsePath, 'imgWidth':fabric.width, 'imgHeight':fabric.height, 'dpi':fabric.dpi, 'thum_url':thum_url, 'name':name} return HttpResponse(json.dumps(variables)) wv.setUploader = function(cid) { if(document.getElementById("uploadFabricInput")){ $("#uploadFabricInput").remove(); } var uploadInput = document.createElement("INPUT"); uploadInput.setAttribute("type", "file"); uploadInput.setAttribute("class", "inp-file-fabric"); uploadInput.setAttribute("name", "file-image-fabric"); uploadInput.setAttribute("id", "uploadFabricInput"); uploadInput.setAttribute("accept", ".png, .jpg, .jpeg"); $("#uploadForm").append(uploadInput); $('.inp-file-fabric').on('change', function() { var formData = new FormData($("#uploadForm")[0]); formData.append('Type', 'true'); formData.append('CID', cid); console.log(cid); $.ajax({ url: '/manager/fabric/create', type: 'POST', data: formData, contentType: false, processData: false, success: function (id, filename, response) { if (response['success']) { wv.getFabricList(cid); } else { switch (response['reason']) { case 'Anonymous': alert('please login first'); break; case 'NoCategory': alert('none of categories selected.'); break; default: alert('please login or select a fabric category.'); return false; break; } } $('#uploadForm')[0].reset(); } }); }); };