def editCustomItem(request, id, token): db = dataBaseModel() if not (request.user.is_authenticated() and request.method == 'POST' and db.checkIfOwnCustomProduct(request.user.id, id) == True): return HttpResponse(json.dumps({'errCode' : dataBaseModel.ERR_BAD_REQUEST}), content_type='application/json') overlayfilename = "" imagefilename = "" config = db.findPositionalConfig(token) # find before clear temp if 'overlay' in request.FILES: temppath = db.removeTempProduct(request.user.id) # try remove temp product # remove temp image if temppath != []: for path in temppath: ImageRW.removeImage(path, False) ImageRW.removeImage(path.replace('.png', '.jpg'), False) try: filename = token_generator() filename += str(request.user.id) filename += '.jpg' overlayfilename = filename.replace('.jpg', 'ol.jpg') imagefilename = ImageRW.writeImage(request.FILES['overlay'], True, filename) # write image ImageRW.writeImage(request.FILES['overlay'], True, overlayfilename) # write image #check here overlayfilename = ImageRW.Process(overlayfilename, True, request.POST["category"]) # convert it to transparent, return the new ol file name #check here ImageRW.removeImage(overlayfilename.replace('.png', '.jpg'), True) except: return HttpResponse(json.dumps({'errCode' : dataBaseModel.ERR_UNABLE_TO_EDIT_CUSTOM_PRODUCT}), content_type='application/json') form = CustomProductForm(request.POST) if form.is_valid(): pcategory = form.cleaned_data['category'].lower() pname = form.cleaned_data['itemname'].lower() pbrand = form.cleaned_data['brand'] purl = form.cleaned_data['url'] pprice = float(form.cleaned_data['price']) pdescription = form.cleaned_data['description'] result = "" if config != None: result = db.editProduct(request.user.id, imagefilename, overlayfilename, pcategory, pbrand, pname, purl, pprice, pdescription, id, config[0], config[1], config[2], config[3], True) else: result = db.editProduct(request.user.id, imagefilename, overlayfilename, pcategory, pbrand, pname, purl, pprice, pdescription, id) if result!= dataBaseModel.SUCCESS: return HttpResponse(json.dumps({'errCode' : dataBaseModel.ERR_UNABLE_TO_EDIT_CUSTOM_PRODUCT}), content_type='application/json') data = {'errCode' : dataBaseModel.SUCCESS} return HttpResponse(json.dumps(data), content_type='application/json') else: return HttpResponse(json.dumps({'errCode' : dataBaseModel.ERR_UNABLE_TO_EDIT_CUSTOM_PRODUCT}), content_type='application/json')
def previewProduct(request): # authenticate if (not request.user.is_authenticated()) or request.method != "POST": return HttpResponse(json.dumps({"errCode": dataBaseModel.ERR_BAD_REQUEST}), content_type="application/json") # convert image to transparent background, save it to temp folder and let hangout api retrieve it try: # helper method to generate random token import string import random def token_generator(size=32, chars=string.ascii_lowercase + string.digits): return "".join(random.choice(chars) for _ in range(size)) token = token_generator() # generate a random token here token += str(request.user.id) filename = token + "ol.jpg" if ImageRW.writeImage(request.FILES["overlay"], False, filename) == "": return HttpResponse( json.dumps({"errCode": dataBaseModel.ERR_UNABLE_TO_PREVIEW_PRODUCT}), content_type="application/json" ) overlay = ImageRW.Process(filename, False, request.POST["category"]) # now save it to temp table db = dataBaseModel() db.addTempProduct(request.user.id, token, overlay, request.POST["category"]) return HttpResponse( json.dumps({"errCode": dataBaseModel.SUCCESS, "token": token}), content_type="application/json" ) except: return HttpResponse( json.dumps({"errCode": dataBaseModel.ERR_UNABLE_TO_PREVIEW_PRODUCT}), content_type="application/json" )
def addProfilePic(request): # verify first if not (request.method == 'POST' and request.user.is_authenticated()): return HttpResponse(json.dumps({'errCode' : dataBaseModel.ERR_BAD_REQUEST}), content_type='application/json') file = request.FILES["profile"] token = token_generator() token += str(request.user.id) profilePicFileName = token + ".jpg" if ImageRW.writeImage(file, True , profilePicFileName, True) == "": return HttpResponse(json.dumps({'errCode' : dataBaseModel.ERR_UNABLE_TO_ADD_PROFILE_PIC}), content_type='application/json') db = dataBaseModel() db.addProfilePic(request.user.id, profilePicFileName) return HttpResponse(json.dumps({'errCode' : dataBaseModel.SUCCESS, 'token' : token}), content_type='application/json')
def addProfilePic(request): # verify first if not (request.method == 'POST' and request.user.is_authenticated()): return HttpResponse(json.dumps( {'errCode': dataBaseModel.ERR_BAD_REQUEST}), content_type='application/json') file = request.FILES["profile"] token = token_generator() token += str(request.user.id) profilePicFileName = token + ".jpg" if ImageRW.writeImage(file, True, profilePicFileName, True) == "": return HttpResponse(json.dumps( {'errCode': dataBaseModel.ERR_UNABLE_TO_ADD_PROFILE_PIC}), content_type='application/json') db = dataBaseModel() db.addProfilePic(request.user.id, profilePicFileName) return HttpResponse(json.dumps({ 'errCode': dataBaseModel.SUCCESS, 'token': token }), content_type='application/json')
def editCustomItem(request, id, token): db = dataBaseModel() if not (request.user.is_authenticated() and request.method == 'POST' and db.checkIfOwnCustomProduct(request.user.id, id) == True): return HttpResponse(json.dumps( {'errCode': dataBaseModel.ERR_BAD_REQUEST}), content_type='application/json') overlayfilename = "" imagefilename = "" config = db.findPositionalConfig(token) # find before clear temp if 'overlay' in request.FILES: temppath = db.removeTempProduct( request.user.id) # try remove temp product # remove temp image if temppath != []: for path in temppath: ImageRW.removeImage(path, False) ImageRW.removeImage(path.replace('.png', '.jpg'), False) try: filename = token_generator() filename += str(request.user.id) filename += '.jpg' overlayfilename = filename.replace('.jpg', 'ol.jpg') imagefilename = ImageRW.writeImage(request.FILES['overlay'], True, filename) # write image ImageRW.writeImage(request.FILES['overlay'], True, overlayfilename) # write image #check here overlayfilename = ImageRW.Process( overlayfilename, True, request.POST["category"] ) # convert it to transparent, return the new ol file name #check here ImageRW.removeImage(overlayfilename.replace('.png', '.jpg'), True) except: return HttpResponse(json.dumps( {'errCode': dataBaseModel.ERR_UNABLE_TO_EDIT_CUSTOM_PRODUCT}), content_type='application/json') form = CustomProductForm(request.POST) if form.is_valid(): pcategory = form.cleaned_data['category'].lower() pname = form.cleaned_data['itemname'].lower() pbrand = form.cleaned_data['brand'] purl = form.cleaned_data['url'] pprice = float(form.cleaned_data['price']) pdescription = form.cleaned_data['description'] result = "" if config != None: result = db.editProduct(request.user.id, imagefilename, overlayfilename, pcategory, pbrand, pname, purl, pprice, pdescription, id, config[0], config[1], config[2], config[3], True) else: result = db.editProduct(request.user.id, imagefilename, overlayfilename, pcategory, pbrand, pname, purl, pprice, pdescription, id) if result != dataBaseModel.SUCCESS: return HttpResponse(json.dumps( {'errCode': dataBaseModel.ERR_UNABLE_TO_EDIT_CUSTOM_PRODUCT}), content_type='application/json') data = {'errCode': dataBaseModel.SUCCESS} return HttpResponse(json.dumps(data), content_type='application/json') else: return HttpResponse(json.dumps( {'errCode': dataBaseModel.ERR_UNABLE_TO_EDIT_CUSTOM_PRODUCT}), content_type='application/json')
def addProduct(request, token): # authenticate if (not request.user.is_authenticated()) or request.method != "POST": return HttpResponse(json.dumps({"errCode": dataBaseModel.ERR_BAD_REQUEST}), content_type="application/json") # delete the item from temp list # add the product db = dataBaseModel() config = db.findPositionalConfig(token) # find it before clear temp temppath = db.removeTempProduct(request.user.id) # try remove temp product # remove temp image if temppath != []: for path in temppath: ImageRW.removeImage(path, False) ImageRW.removeImage(path.replace(".png", ".jpg"), False) try: # helper method to generate random token import string import random def token_generator(size=32, chars=string.ascii_lowercase + string.digits): return "".join(random.choice(chars) for _ in range(size)) filename = token_generator() filename += str(request.user.id) filename += ".jpg" overlayfilename = filename.replace(".jpg", "ol.jpg") imagefilename = ImageRW.writeImage(request.FILES["overlay"], True, filename) # write image ImageRW.writeImage(request.FILES["overlay"], True, overlayfilename) # write image # check here overlayfilename = ImageRW.Process( overlayfilename, True, request.POST["category"] ) # convert it to transparent, return the new ol file name # check here ImageRW.removeImage(overlayfilename.replace(".png", ".jpg"), True) form = CustomProductForm(request.POST) if form.is_valid(): pcategory = form.cleaned_data["category"].lower() pname = form.cleaned_data["itemname"].lower() pbrand = form.cleaned_data["brand"] purl = form.cleaned_data["url"] pprice = float(form.cleaned_data["price"]) pdescription = form.cleaned_data["description"] result = "" if config != None: # print "run" result = db.addProduct( request.user.id, imagefilename, overlayfilename, pcategory, pbrand, pname, purl, pprice, pdescription, config[0], config[1], config[2], config[3], False, ) else: result = db.addProduct( request.user.id, imagefilename, overlayfilename, pcategory, pbrand, pname, purl, pprice, pdescription, ) if result != dataBaseModel.SUCCESS: return HttpResponse( json.dumps({"errCode": dataBaseModel.ERR_UNABLE_TO_ADD_PRODUCT}), content_type="application/json" ) data = {"errCode": dataBaseModel.SUCCESS} return HttpResponse(json.dumps(data), content_type="application/json") else: raise Exception("") except: return HttpResponse( json.dumps({"errCode": dataBaseModel.ERR_UNABLE_TO_ADD_PRODUCT}), content_type="application/json" )
def testWriteWrongFormat(self): from django.core.files.uploadedfile import SimpleUploadedFile uploaded_file = open("wrongformat.png", 'rb') dfile = SimpleUploadedFile(uploaded_file.name, uploaded_file.read()) self.assertEquals(ImageRW.writeImage(dfile, True, "testWrite.jpg"), "", "Able to write wrong format image")
def testWriteVeryLargeImage(self): from django.core.files.uploadedfile import SimpleUploadedFile uploaded_file = open("verylarge.jpg", 'rb') dfile = SimpleUploadedFile(uploaded_file.name, uploaded_file.read()) self.assertEquals(ImageRW.writeImage(dfile, True, "testWrite.jpg"), "", "Able to write large image")
def testWriteImageInTemp(self): from django.core.files.uploadedfile import SimpleUploadedFile uploaded_file = open("testImage.jpg", 'rb') dfile = SimpleUploadedFile(uploaded_file.name, uploaded_file.read()) self.assertEquals(ImageRW.writeImage(dfile, False, "testWrite.jpg"), "testWrite.jpg", "Unable to write image")