def delete(self): ProductTagModel.delete_all_product_tags() image = RepoFileModel.find_by_id(self.imageId) image.decrease_user() db.session.delete(self) db.session.commit()
def delete(self): ArticleTagModel.delete_all_article_tags() image = RepoFileModel.find_by_id(self.imageId) image.decrease_user() db.session.delete(self) db.session.commit()
def put(self, param): data = AdminProduct.parser.parse_args() product = ProductModel.find_by_id(param) if bool(product): if product.imageId != data.pro__image: product.image.decrease_users() image = RepoFileModel.find_by_id(data.pro__image) image.increase_users() product.title = data.pro__title product.body = data.pro__body product.summary = data.pro__summary product.imageId = data.pro__image product.save() ProductTagModel.update_tags(product.id, json.loads(data.pro__tags)) try: return {"error": 0} except: return {"error": 2} else: return {"error": 1, "error_msg": "Product doesn't exist!"}
def delete(self, id): file = RepoFileModel.find_by_id(id) if file: file.delete() return {"error": 0} else: return {"error": 1, "error_msg": "Folder doesn't exist"}
def get_root_content(cls, folderId): files = RepoFileModel.get_files_by_folder(folderId) folders = cls.query.filter_by(parent=folderId) resp = { "files": [x.json() for x in files], "folders": [x.json() for x in folders] } return resp
def get(self, id): file = RepoFileModel.find_by_id(id) if file: used = file.check_if_used() if not used: file.delete() return {"error": 0} else: return {"error": 1, "error_msg": "File is being used"} else: return {"error": 2, "error_msg": "File doesn't exist"}
def post(self, param): data = AdminProduct.parser.parse_args() try: product = ProductModel(data.pro__title, data.pro__body, data.pro__summary, data.pro__image) product.save() post = PostModel(product.id, 1) post.save() image = RepoFileModel.find_by_id(data.pro__image) image.increase_users() for tag in json.loads(data.pro__tags): newTag = ProductTagModel(product.id, tag) newTag.save() return {"error": 0} except: return {"error": 1}
def handle_upload(cls,f, attrs): """ Handle a chunked or non-chunked upload. """ if attrs['folderId'] == "root": folderId = 0 else : folderId = attrs['folderId'] chunked = False dest_folder = os.path.join(cls.UPLOAD_DIRECTORY, attrs['qquuid']) dest = os.path.join(dest_folder, attrs['qqfilename']) # Chunked if 'qqtotalparts' in attrs and int(attrs['qqtotalparts']) > 1: chunked = True dest_folder = os.path.join(cls.CHUNKS_DIRECTORY, attrs['qquuid']) dest = os.path.join(dest_folder, attrs['qqfilename'], str(attrs['qqpartindex'])) cls.save_upload(f, dest) else: # Generate repo file in database before saving actual file chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRTUVWXYZ0123456789" fileName = "".join(random.sample(chars, 6)) while RepoFileModel.exists(fileName): fileName = "".join(random.sample(chars, 6)) ogName = attrs['qqfilename'] p = ogName.split(".") newFile = RepoFileModel( fileName, p[0], p[1], folderId, attrs['qquuid']) newFile.save() dest = os.path.join(cls.UPLOAD_DIRECTORY, fileName + "." + p[1]) cls.save_upload(f, dest) ImageManager().createThumbnails(newFile) # ^^^ Generate repo file in database before saving actual file if chunked and (int(attrs['qqtotalparts']) - 1 == int(attrs['qqpartindex'])): chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRTUVWXYZ0123456789" fileName = "".join(random.sample(chars, 6)) while RepoFileModel.exists(fileName): fileName = "".join(random.sample(chars, 6)) ogName = attrs['qqfilename'] p = ogName.split(".") newFile = RepoFileModel(fileName, p[0], p[1],folderId, attrs['qquuid']) newFile.save() dst = os.path.join(cls.UPLOAD_DIRECTORY,fileName + "." + p[1]) cls.combine_chunks(attrs['qqtotalparts'], attrs['qqtotalfilesize'], source_folder=os.path.dirname(dest), dest=dst) shutil.rmtree(os.path.dirname(os.path.dirname(dest))) ImageManager().createThumbnails(newFile)