def delete_by(cls, **kwargs): """ 删除符合条件的所有实例 :param kwargs: :return: '删除完成' """ log('Delete by', kwargs) ms = cls.all() delete_list = [] if kwargs.get('id', 'None') != 'None': kwargs['id'] = int(kwargs['id']) for i, m in enumerate(ms): is_what_we_find = False for k, v in kwargs.items(): if getattr(m, k, None) == v and hasattr(m, k): is_what_we_find = True if is_what_we_find: delete_list.append(m) for m in delete_list: ms.remove(m) data = [m.json() for m in ms] save(cls.db_path(), data) return '删除完成'
def wrapper(*args): log('Check wheather logged') u = current_user() if u is None: return redirect(url_for('public.log_in')) else: return f(u)
def api_task_finish(): form = request.get_json() log(form, type(form)) task = Task.find_by(id=int(form['id'])) task.finish = True task.save() result = 'finished' return jsonify(result)
def api_task_delete(): form = request.get_json() log(form) task = Task.find_by(id=int(form['id'])) log(task) Task.delete_by(id=int(form['id'])) result = 'finished' return jsonify(result)
def load(fname): """ 从txt文件加载JSON数据 :param fname: 文件名 :return: JSON字符串 """ # Make sure if the file exists path = os.path.split(fname)[0] if not os.path.exists(path): log('原文件不存在, 准备创建') os.makedirs(path) if not os.path.exists(fname): with open(fname, 'w', encoding='utf-8') as f: f.write('[]') with open(fname, 'r', encoding='utf-8') as f: data = json.load(f) return data