Exemplo n.º 1
0
 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."})