def put(self, file_id): try: update_fields = {} parser = reqparse.RequestParser() parser.add_argument('name', type=str, help="New name for the file/folder") parser.add_argument('parent_id', type=str, help="New parent folder for the file/folder") args = parser.parse_args() name = args.get('name', None) parent_id = args.get('parent_id', None) if name is not None: update_fields['name'] = name if parent_id is not None and g.file['parent_id'] != parent_id: if parent_id != '0': folder_access = Folder.filter({ 'id': parent_id, 'creator': g.user_id }) if not folder_access: abort( 404, message= "You don't have access to the folder you're trying to move this object to" ) if g.file['is_folder']: update_fields['tag'] = g.file[ 'id'] if parent_id == '0' else "{0}#{1}".format( folder_access['tag'], folder['last_index']) Folder.move(g.file, folder_access) else: File.move(g.file, folder_access) update_fields['parent_id'] = parent_id if g.file['is_folder']: Folder.update(file_id, update_fields) else: File.update(file_id, update_fields) return File.find(file_id) except Exception as e: abort(500, message="There was an while processing your request --> {0}". format(e))
def delete(self, file_id): try: hard_delete = request.args.get('hard_delete', False) if not g.file['is_folder']: if hard_delete == 'true': os.remove(g.file['uri']) File.delete(file_id) else: File.update(file_id, {'status': False}) else: if hard_delete == 'true': folders = Folder.filter( lambda folder: folder['tag'].startswith(g.file['tag'])) for folder in folders: files = File.filter({ 'parent_id': folder['id'], 'is_folder': False }) File.delete({ 'parent_id': folder['id'], 'is_folder': False }) for f in files: os.remove(f['uri']) else: File.update(file_id, {'status': False}) File.update({'parent_id': file_id}, {'status': False}) return "File has been deleted successfully", 204 except: abort(500, message= "There was an error while processing your request --> {0}". format(e))