def author_ac(request): if request.method == 'GET': a = request.params['term'] log.debug('author autocomplete') alla = Author.objects(name__icontains = a) if alla.count() > 0: data = {'authors': [{'label': aname.name, 'value': str(aname.id)} for aname in alla ]} else: data = {'authors': []} return data['authors']
def img_upload(request): if request.method == 'POST': plat = float(request.POST['plat']) plng = float(request.POST['plng']) description = request.POST['updesc'] picauthor = request.POST['picauthor'] picauthid = request.POST['picauthid'] # basically we like the GPS exif data gpsIgnore = False if 'gpsignore' in request.params: gpsIgnore = True exifData = None imgFormat = None ePos = [] filename = request.POST['files[]'].filename log.debug('filename: '+filename) rcvFile = request.POST['files[]'].file # get the original filename extension origFnExt = getOriginalFileExt(filename) finalFName = str(uuid.uuid4())+origFnExt # final path for the file filePath = os.path.join('gimsey/static/userfiles/orig/', '%s'% finalFName) # We first write to a temporary file to prevent incomplete files from # being used. tempFilePath = filePath + '~' outputFile = open(tempFilePath, 'wb') # Finally write the data to a temporary file rcvFile.seek(0) while True: data = rcvFile.read(2<<16) if not data: break outputFile.write(data) # If your data is really critical you may want to force it to disk first # using output_file.flush(); os.fsync(output_file.fileno()) outputFile.close() log.debug(tempFilePath) log.debug(request.curUser.id) if picauthid == '-1': nAuthor = Author(name = picauthor, createdOn=datetime.utcnow, createdBy = request.curUser.id ) nAuthor.save() imgVerify = img_supported_format(tempFilePath, 'gimsey/static/userfiles/thumb/') if imgVerify['success']: exifData = imgVerify['exifData'] imgFormat = imgVerify['format'] ePos = get_lat_lng(imgVerify['exifData']) if (len(ePos) > 1) and (gpsIgnore == False): plng = ePos[1] plat = ePos[0] # Now that we know the file has been fully saved to disk move it into place. os.rename(tempFilePath, filePath) fileInfo = os.stat(filePath) #check if this place exists FIX: should use $near query #exPlace = Place.objects.get(createdBy=request.curUser.id, position=[plat,plng]) #exPlace = Place.objects(createdBy=request.curUser.id,position__near=[plat, plng], position__max_distance=50) exPlace = Place.objects(createdBy=request.curUser.id,__raw__={'position':{'$near':{'$geometry':{'type': "Point", 'coordinates': [plat, plng]},'$maxDistance': 100}}}) # create the place if it doesn't exist log.debug('****** places found near: '+str(exPlace.count())) # use embeddedpos if exPlace.count() == 0: p = Place( name=' ', description = description, objType = 'image', createdBy = request.curUser.id, createdOn = datetime.utcnow(), position = [plat, plng]) p.save() exPlace = p else: exPlace = exPlace[0] imgObj = ImageObject( fileName = finalFName, path = filePath, size = fileInfo.st_size, takenOn = datetime.utcnow(), imgFormat = imgFormat, exifData = exifData, embeddedPos = ePos ) # add the image to the vector exPlace.images.append(imgObj) exPlace.save() result = {'files':[]} result['files'].append({ 'name': '', 'url': '', 'thumbnail_url': imgVerify['thumb'][6:], 'delete_url':'/object/delete/'+str(exPlace.id), 'delete_type': 'POST', 'id': 1 }) return result else: return {'result': 'none'}