def GET(self, token=None, action="json"): if action == "upload": try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) #key = uuid.UUID('3220eb24-e0f8-4b45-a481-638719cbe7f1') except: cherrypy.response.status = 403 return error403 if token == "new": newToken = database.getUploadToken(key, cherrypy.request.remote.ip) if newToken: qrData = self._url + str(newToken) + "/upload" qr = qrcode.make(qrData, box_size=3) buffer = ioBuffer() qr.save(buffer, format='PNG') cherrypy.response.headers['Token'] = str(newToken) cherrypy.response.headers['Content-Type'] = "image/png" #cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="file.png"' return buffer.getvalue() else: cherrypy.response.status = 403 return error403 else: try: token = uuid.UUID(token) collectedData = database.getCollectedData(token) except: cherrypy.response.status = 500 return error500 cherrypy.response.headers['Content-Type'] = "application/json" return json.dumps(collectedData) if action == "json": try: token = uuid.UUID(token) jsonData = database.getMasterData(token) except: cherrypy.response.status = 404 return error404 cherrypy.response.headers['Content-Type'] = "application/json" return json.dumps(jsonData)
def POST(self, token=None, action="download"): rawData = cherrypy.request.body.read( int(cherrypy.request.headers['Content-Length'])) try: jsonData = json.loads(rawData) except: cherrypy.response.status = 500 return error500 if action == "upload": try: token = uuid.UUID(token) except: cherrypy.response.status = 403 return error403 if database.putCollectedData(token, jsonData): return "OK" else: cherrypy.response.status = 404 return error404 try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) except: cherrypy.response.status = 403 return "Forbidden" token = database.putMasterdata(key, jsonData, cherrypy.request.remote.ip) if token != None: qrData = self._url + str(token) + "/json" qr = qrcode.make(qrData, box_size=3) cherrypy.response.headers['Content-Type'] = "image/png" buffer = ioBuffer() qr.save(buffer, format='PNG') return buffer.getvalue() else: cherrypy.response.status = 403 return error403
def GET(self, token=None, action="json"): if action == "upload": try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) #key = uuid.UUID('3220eb24-e0f8-4b45-a481-638719cbe7f1') except: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] if token == "new": database = MasterData(self.__iniFile) newToken = database.getUploadToken(key, cherrypy.request.remote.ip) if newToken: qrData = self._url + str(newToken) + "/upload" qr = qrcode.make(qrData, box_size=3) buffer = ioBuffer() qr.save(buffer, format='PNG') cherrypy.response.headers['Token'] = str(newToken) cherrypy.response.headers['Content-Type'] = "image/png" #cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="file.png"' return buffer.getvalue() else: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] else: database = MasterData(self.__iniFile) try: token = uuid.UUID(token) collectedData = database.getCollectedData(token) except: cherrypy.response.status = 500 return httpErrors[cherrypy.response.status] cherrypy.response.headers['Content-Type'] = "application/json" return json.dumps(collectedData) elif action == "csv": database = MasterData(self.__iniFile) try: token = uuid.UUID(token) collectedData = database.getCollectedData(token) except: cherrypy.response.status = 500 return httpErrors[cherrypy.response.status] cherrypy.response.headers['Content-Type'] = "text/csv" out = ioBuffer() writer = csv.writer(out) writer.writerows([("barcode", "quantity")]) for row in collectedData: writer.writerows([(row['barcode'], row['quantity'])]) cherrypy.response.headers['Content-Length'] = out.len return out.getvalue() elif action == "json": database = MasterData(self.__iniFile) try: token = uuid.UUID(token) jsonData = database.getMasterData(token) except: cherrypy.response.status = 404 return httpErrors[cherrypy.response.status] cherrypy.response.headers['Content-Type'] = "application/json" if database.removeAds(token): m = hashlib.sha256() try: bearer = cherrypy.request.headers.get("X-Authorization") m.update(bearer) m.update(self._cloudKey) except: m.update(str(uuid.uuid4())) cherrypy.response.headers['X-Authorization'] = m.hexdigest() return json.dumps(jsonData) elif action == "xml": database = MasterData(self.__iniFile) xmlData = database.getXMLdata(token) if xmlData: cherrypy.response.headers['Content-Type'] = "application/xml" return xmlData else: cherrypy.response.status = 404 return httpErrors[cherrypy.response.status] elif action == "barcode": try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) except: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] database = MasterData(self.__iniFile) barcodeData = database.getBarcodeInfo(token) cherrypy.response.headers['Content-Type'] = "application/json" return json.dumps(barcodeData)
def POST(self, token=None, action="download"): rawData = cherrypy.request.body.read( int(cherrypy.request.headers['Content-Length'])) if action == "upload": try: jsonData = json.loads(rawData) except: cherrypy.response.status = 500 return httpErrors[cherrypy.response.status] try: token = uuid.UUID(token) except: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] database = MasterData(self.__iniFile) if database.putCollectedData(token, jsonData): return httpErrors[200] else: cherrypy.response.status = 404 return httpErrors[cherrypy.response.status] elif action == "download": try: jsonData = json.loads(rawData) except: cherrypy.response.status = 500 return httpErrors[cherrypy.response.status] try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) except: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] database = MasterData(self.__iniFile) token = database.putMasterdata(key, jsonData, cherrypy.request.remote.ip) if token != None: qrData = self._url + str(token) + "/json" qr = qrcode.make(qrData, box_size=3) cherrypy.response.headers['Content-Type'] = "image/png" buffer = ioBuffer() qr.save(buffer, format='PNG') return buffer.getvalue() else: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] elif action == "csv": try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) except: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] if not rawData: cherrypy.response.status = 500 return httpErrors[cherrypy.response.status] data = ioBuffer(rawData) reader = csv.DictReader(data) listData = list(reader) database = MasterData(self.__iniFile) token = database.putMasterdata(key, listData, cherrypy.request.remote.ip) if token != None: qrData = self._url + str(token) + "/json" qr = qrcode.make(qrData, box_size=3) cherrypy.response.headers['Content-Type'] = "image/png" buffer = ioBuffer() qr.save(buffer, format='PNG') return buffer.getvalue() else: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] elif action == "xml": try: key = uuid.UUID(cherrypy.request.headers.get('access-key')) except: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status] if not rawData: cherrypy.response.status = 500 return httpErrors[cherrypy.response.status] database = MasterData(self.__iniFile) xmlWrite = database.putXMLdata(key, token, rawData, cherrypy.request.remote.ip) if xmlWrite: qrData = self._url + str(token) + "/xml" qr = qrcode.make(qrData, box_size=3) cherrypy.response.headers['Content-Type'] = "image/png" buffer = ioBuffer() qr.save(buffer, format='PNG') return buffer.getvalue() else: cherrypy.response.status = 403 return httpErrors[cherrypy.response.status]