def ts(self): """ Search for the TileSetName in the Request and try and load it from Memcache or DataStore, return True or False """ if self._ts: return self._ts if self.tsid: tsid = self.tsid else: tileSetName = self.request.REQUEST.get('ts') if tileSetName: self.tsid = tsid = 'ts:'+tileSetName else: return False ts = memcache.get(tsid) if ts is not None: self._ts = ts else: ts = TileSet.get_by_key_name(tsid) #if ts is not None and not memcache.add(tsid, ts): # logging.error("Memcache set failed [ %s ]"%tsid) self._ts = ts #logging.info(self._ts) if ts is not None: return self._ts else: return False
def ts(self): """ Search for the TileSetName in the Request and try and load it from Memcache or DataStore, return TileSet or False """ if self._ts: return self._ts if self.tsid: tsid = self.tsid else: tileSetName = self.request.REQUEST.get('ts') if tileSetName: self.tsid = tsid = tileSetName else: return False ts = memcache.get(tsid.encode('utf8')) if ts is None: ts = TileSet.get_by_key_name(tsid) if ts is not None and not memcache.set(tsid.encode('utf8'),ts): logger.error("Memcache set failed [ %s ]"%tsid) if ts is not None: if ts.public or ts.owner == self.request.user.username: self._ts = ts return ts return False
def post(self,tsid=''): if self.request.user.is_authenticated(): user = self.request.user logger.info("%s, is creating a new TileSet."%(user.username)) POST = self.request.POST # SINGLE PART UPLOAD, CREATE NEW SHAPEFILE if 'shp' in POST and 'shx' in POST: shp = zlib.decompress(base64.b64decode(POST['shp'])) shx = zlib.decompress(base64.b64decode(POST['shx'])) #dbf = zlib.decompress(base64.b64decode(POST['dbf'])) shapeModel = new_shapefile(shp,shx) ts = TileSet("%s:%s"%(user.username,shapeModel.key_name)) ts.owner = user.username ts.public = True ts.date = time.mktime(time.gmtime()) ts.numregions = shapeModel.n ts.shpfile = shapeModel.key_name return self.write({'TileSetID':ts.put()}) # MULTIPART UPLOAD: Initiate Upload elif 'shpmd5' in POST and 'shx' in POST: shpmd5 = POST['shpmd5'].upper() if TileSet.get_by_key_name('%s:%s'%(user.username,shpmd5)): return self.write({'TileSetID':'%s:%s'%(user.username,shpmd5)}) shapeModel = Shapefile.get_by_key_name(shpmd5) if shapeModel: ts = TileSet("%s:%s"%(user.username,shapeModel.key_name)) ts.owner = user.username ts.public = True ts.date = time.mktime(time.gmtime()) ts.numregions = shapeModel.n ts.shpfile = shapeModel.key_name return self.write({'TileSetID':ts.put()}) else: #need to upload data. shx = zlib.decompress(base64.b64decode(POST['shx'])) uploadID = new_shapefile_from_parts(shpmd5,shx) if uploadID: return self.write({"uploadId":uploadID}) else: return self.write({'error':"Could not initiate multipart upload. Uknown error."}) # MULTIPART UPLOAD: Upload Part elif 'shpmd5' in POST and 'uploadId' in POST and 'partNum' in POST and 'shpPart' in POST: shpPart = zlib.decompress(base64.b64decode(POST['shpPart'])) etag = upload_part_of_shapefile(POST['shpmd5'],POST['uploadId'],int(POST['partNum']),shpPart) if etag: return self.write({"etag":etag}) else: return self.write({'error':"Could not upload part. Try Again."}) # MULTIPART UPLOAD: Complete Upload. elif 'shpmd5' in POST and 'uploadId' in POST and 'partsList' in POST: parts = {} for i,etag in enumerate(POST['partsList'].split(',')): parts[i+1] = etag shapeModel = complete_multipart_upload(POST['shpmd5'],POST['uploadId'],parts) ts = TileSet("%s:%s"%(user.username,shapeModel.key_name)) ts.owner = user.username ts.public = True ts.date = time.mktime(time.gmtime()) ts.numregions = shapeModel.n ts.shpfile = shapeModel.key_name return self.write({'TileSetID':ts.put()}) return self.write({'error':"Missing required arguments 'shp' and/or 'shx'."}) else: return self.write({'error':"User not authenticated."}) return self.write({'error':"An unknown error occured."})