def post(self): if GhettoAuth(self.request) == False: self.error(404) return zipFileModel = ZipFileModel.get( db.Key(self.request.get('key')) ) if not zipFileModel: result = { 'success' : False, 'exception' : 'InvalidFileKeyError', 'msg' : 'Requested file does not exist!' } self.response.headers['Content-Type'] = 'application/json' self.response.out.write( json.dumps( result ) ) return if not zipFileModel.blob: result = { 'success' : False, 'status' : zipFileModel.status, 'msg' : '' } self.response.headers['Content-Type'] = 'application/json' self.response.out.write( json.dumps( result ) ) return else: self.response.headers['Content-Disposition'] = 'attachment; filename="LandStreamData.zip"' self.send_blob(zipFileModel.blob) return result = { 'success' : False, 'msg' : 'Unkown failure' } self.response.headers['Content-Type'] = 'application/json' self.response.out.write( json.dumps( result ) ) return
def post(self): if GhettoAuth(self.request) == False: self.error(404) return try: dateParam = self.request.get('date') logging.debug(dateParam) date = None if dateParam: date = dateutil.parser.parse(dateParam) zipFileModel = ZipFileModel() zipFileModel.status = 'started' zipFileModel.put() deferred.defer(CreateZipFile, date, str(zipFileModel.key())) #self.response.out.write("started deferred task") self.response.headers['Content-Type'] = 'application/json' result = { 'success': True, 'msg': '', 'key': str(zipFileModel.key()) } self.response.out.write(json.dumps(result)) return except Exception, detail: self.response.headers['Content-Type'] = 'application/json' result = {'success': False, 'msg': repr(detail)} self.response.out.write(json.dumps(result)) return
def post(self): if GhettoAuth(self.request) == False: self.error(404) return try: dateParam = self.request.get('date') logging.debug(dateParam) date = None if dateParam: date = dateutil.parser.parse(dateParam) zipFileModel = ZipFileModel() zipFileModel.status = 'started' zipFileModel.put() deferred.defer( CreateZipFile, date, str(zipFileModel.key()) ) #self.response.out.write("started deferred task") self.response.headers['Content-Type'] ='application/json' result = { 'success' : True, 'msg' : '', 'key' : str(zipFileModel.key()) } self.response.out.write(json.dumps( result )) return except Exception, detail: self.response.headers['Content-Type'] ='application/json' result = { 'success' : False, 'msg' : repr(detail) } self.response.out.write(json.dumps( result )) return
def post(self): if GhettoAuth(self.request) == False: self.error(404) return zipFileModel = ZipFileModel.get(db.Key(self.request.get('key'))) if not zipFileModel: result = { 'success': False, 'exception': 'InvalidFileKeyError', 'msg': 'Requested file does not exist!' } self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(result)) return if not zipFileModel.blob: result = { 'success': False, 'status': zipFileModel.status, 'msg': '' } self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(result)) return else: self.response.headers[ 'Content-Disposition'] = 'attachment; filename="LandStreamData.zip"' self.send_blob(zipFileModel.blob) return result = {'success': False, 'msg': 'Unkown failure'} self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(result)) return
def CreateZipFile(date, zipFileKey): # leases = OilGasLease.all().filter("date >", date) try: zipFileModel = ZipFileModel.get(db.Key(zipFileKey)) zipFileModel.status = "running" zipFileModel.put() stream = StringIO.StringIO() zipFile = zipfile.ZipFile(stream, "w", compression=zipfile.ZIP_DEFLATED) leases = OilGasLease.all() if date: leases = leases.filter("date >", date) s = OilGasLease.ToCsv(leases) zipFile.writestr("OilGasLease.csv", s.getvalue()) tracts = list() tempFiles = list() count = 0 try: for lease in leases: tracts.extend(lease.Tracts) if count % 100 == 0: zipFileModel.status = str(count) zipFileModel.put() count += 1 except db.Timeout: leases = OilGasLease.all() if date: leases = leases.filter("date >", date) for lease in leases.run(offset=count): tracts.extend(lease.Tracts) if csvOnly == False: DocImage.WriteToZip(lease.Images, zipFile) if count % 100 == 0: zipFileModel.status = str(count) zipFileModel.put() count += 1 zipFileModel.status = "writing Tract data to zipfile" zipFileModel.put() s = Tract.ToCsv(tracts) zipFile.writestr("Tracts.csv", s.getvalue()) zipFile.close() blob = files.blobstore.create(mime_type="application/zip", _blobinfo_uploaded_filename="LandStreamData.zip") zipFileModel.status = "writing zip file to blobstore" zipFileModel.put() with files.open(blob, "a") as f: stream.seek(0) while True: buf = stream.read(2048) if buf == "": break f.write(buf) files.finalize(blob) zipFileModel.blob = files.blobstore.get_blob_key(blob) zipFileModel.status = "done" zipFileModel.put() except Exception, detail: zipFileModel.status = repr(detail) zipFileModel.put()
def CreateZipFile(date, zipFileKey): #leases = OilGasLease.all().filter("date >", date) try: zipFileModel = ZipFileModel.get(db.Key(zipFileKey)) zipFileModel.status = 'running' zipFileModel.put() stream = StringIO.StringIO() zipFile = zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED) leases = OilGasLease.all() if date: leases = leases.filter('date >', date) s = OilGasLease.ToCsv(leases) zipFile.writestr("OilGasLease.csv", s.getvalue()) tracts = list() tempFiles = list() count = 0 try: for lease in leases: tracts.extend(lease.Tracts) if count % 100 == 0: zipFileModel.status = str(count) zipFileModel.put() count += 1 except db.Timeout: leases = OilGasLease.all() if date: leases = leases.filter('date >', date) for lease in leases.run(offset=count): tracts.extend(lease.Tracts) if csvOnly == False: DocImage.WriteToZip(lease.Images, zipFile) if count % 100 == 0: zipFileModel.status = str(count) zipFileModel.put() count += 1 zipFileModel.status = 'writing Tract data to zipfile' zipFileModel.put() s = Tract.ToCsv(tracts) zipFile.writestr("Tracts.csv", s.getvalue()) zipFile.close() blob = files.blobstore.create( mime_type='application/zip', _blobinfo_uploaded_filename='LandStreamData.zip') zipFileModel.status = 'writing zip file to blobstore' zipFileModel.put() with files.open(blob, 'a') as f: stream.seek(0) while True: buf = stream.read(2048) if buf == "": break f.write(buf) files.finalize(blob) zipFileModel.blob = files.blobstore.get_blob_key(blob) zipFileModel.status = 'done' zipFileModel.put() except Exception, detail: zipFileModel.status = repr(detail) zipFileModel.put()