def edit(self, id=None): m = Model() try: print(" .. GET Edit Data (ID): " + id) res = m.find_by_id(id) self.success(message="image, edit id: " + str(id), data=res) except Exception as e: self.error(message="image, edit id: " + str(id) + "msg: " + str(e), data=None)
def destroy(self, id=None): try: data_json = self.request.body print(" .. DELETE Data: ID:" + str(data_json)) m = Model() m.init_from_json(data_json) res = m.find_by_id(m.id) res.delete() self.success(message="todo, destroy id: " + str(m.id)) except Exception as e: self.error(message="todo, destroy id: " + str(e))
def create(self): try: data_json = self.request.body m = Model() m.init_from_json(data_json, simple_conversion=True) m.upsert() self.success(message="image, successfully created " + str(m.id), data=m, format="json") except Exception as e: self.error(message="image, error updating " + str(m.id) + "msg: " + str(e), data=m, format="json")
def add_image(self, article_id=None): print("add_image: :" + str(article_id)) #print("request.files[file]: {}".format(str(self.request.files['file']))) print(str(self.request)) image = Model() try: # {'files[]': [{'filename': 'bstrap_blog.PNG', 'body': b'\x89 ... '}] print("request.files[name]: {}".format( str(self.request.files["files[]"][0]["filename"]))) file1 = self.request.files["files[]"][0] except Exception as e: print(str(e)) try: #file1_data = f["body"] #for key in file1.keys(): # print("key=>" + str(key)) original_fname = file1['filename'] fname, extension = os.path.splitext(original_fname) print("fname, extension :" + str(fname) + ", " + str(extension)) image.articles.append(article_id) image.filename = fname image.extension = extension #fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6)) sec_filename = secure_filename(original_fname) final_filename = os.path.join(myapp["upload_path"], sec_filename) output_file = open(final_filename, 'wb') output_file.write(file1['body']) try: image.upsert() except Exception as e: print("error upserting image") #self.success(message="final_filename is uploaded", data=image, format="json") if myapp["proxy"]: file_path = myapp["app_proxy_url"] + myapp[ "upload_url"] + "/" + sec_filename else: file_path = myapp["app_base_url"] + myapp[ "upload_url"] + "/" + sec_filename out_d = {"files": [{"url": file_path}]} print(json.dumps(out_d)) #self.write(json.dumps(out_d)) self.success(pure=True, data=out_d, format="json") except Exception as e: print("raw error: {}".format(str(e))) self.error(message="Error uploading final_filename:" + str(e), format="json")
def update(self, id=None): data_json = self.request.body m = Model() m.init_from_json(data_json) res = m.find_by_id(m.id) res.init_from_json(data_json) try: #res.tags= res.tags.split(",") res.upsert() self.success(message="image, successfully updated " + str(res.id), data=res, format="json") except Exception as e: self.error(message="image, error updating: " + str(m.id) + "msg: " + str(e), data=data_json, format="json")
def page(self, page=0): m = Model() res = m.page(page=int(page), page_size=myapp["page_size"]) self.success(message="image page: #" + str(page), data=res)
def list(self): m = Model() res = m.get_all() self.success(message="image, index", data=res)
def show(self, id=None): m = Model() res = m.find_by_id(id) self.success(message="image show", data=res)