def delete_data_to_database(model, find_dict, set_default_delete_data_to_database=None): """ :param model: :param find_dict: :return: """ sid = transaction.savepoint() # 开启事务设置事务保存点 try: if set_default_delete_data_to_database: model, find_dict = set_default_delete_data_to_database( model, find_dict) objs = model.objects.filter(**find_dict) if not objs: return_json_j = status_dict.get_FAIL_BY_NOTHING_TO_DO_4040( "没找到需要删除的对象") return_json_j.update({ "status": "FAIL", "type": model.__name__, }) return return_json_j, 400 # 模型备份 for obj in objs: NewBeeDeleteRecord.objects.create(model_name=model.__name__, record_json=json.dumps( models_to_json(obj))) objs.delete() except Exception as e: transaction.savepoint_rollback(sid) # 失败回滚事务(如果数据库操作发生异常,回滚到设置的事务保存点) return { # "msg": "%s文件%s行发生%s错误" % ( # e.__traceback__.tb_frame.f_globals["__file__"], e.__traceback__.tb_lineno, e), "msg": "%s" % e, "code": status_dict.SERVER_ERROR, "status": "FAIL", "type": model.__name__, }, 500 else: transaction.savepoint_commit(sid) # 如果没有异常,成功提交事物 logger.info("%s模型删除了一条数据%s成功" % (model.__str__, str(find_dict))) return_json = status_dict.get_DELETE_SUCCESS_2001() return_json.update({ "status": "OK", "type": model.__name__, }) return return_json, 200
def add_data_to_database(model, add_dict, many_to_many_dict, return_data_key, return_data_dict, default_add_data_to_database=None): """ 增加一条记录到数据库 :param model: :param add_dict: :param many_to_many_dict: :param return_data_dict: :return: """ sid = transaction.savepoint() # 开启事务设置事务保存点 try: if default_add_data_to_database: model, add_dict, many_to_many_dict, return_data_key, return_data_dict = default_add_data_to_database( model, add_dict, many_to_many_dict, return_data_key, return_data_dict) obj = model.objects.create(**add_dict) for k, v in many_to_many_dict.items(): attr = getattr(obj, k) if isinstance(v, list): v = [int(l) for l in v] attr.add(*v) else: attr.add(int(v)) except Exception as e: transaction.savepoint_rollback(sid) # 失败回滚事务(如果数据库操作发生异常,回滚到设置的事务保存点) return { # "msg": "%s文件%s行发生%s错误" % ( # e.__traceback__.tb_frame.f_globals["__file__"], e.__traceback__.tb_lineno, e), "msg": "%s" % e, "code": status_dict.SERVER_ERROR, "status": "FAIL", "type": model.__name__, }, 500 else: transaction.savepoint_commit(sid) # 如果没有异常,成功提交事物 add_data = get_model_data_by_id(model=model, id=obj.id, return_data_dict=return_data_dict) logger.info("%s模型%s添加了一条数据%s成功" % (model.__str__, return_data_key, str(add_dict))) if not add_data: return_json = status_dict.get_ADD_SUCCESS_2000( msg="添加成功, 但查询新增数据时失败") return_json.update({ "status": "OK", "type": model.__name__, }) else: return_json = status_dict.get_ADD_SUCCESS_2000() return_json.update({ "status": "OK", "type": model.__name__, }) data = {} data[return_data_key] = models_to_json(data=add_data) return_json["data"] = data return return_json, 200
def update_data_to_database(model, find_for_write_default_dict, update_dict, many_to_many_dict, return_data_key, return_data_dict, default_update_data_to_database=None): """ 增加一条记录到数据库 :param model: :param add_dict: :param many_to_many_dict: :param return_data_dict: :return: """ logger.debug("更新操作的多对多字典:%s " % many_to_many_dict) sid = transaction.savepoint() # 开启事务设置事务保存点 try: if default_update_data_to_database: logger.debug("执行default_update_data_to_database: %s" % default_update_data_to_database) model, find_for_write_default_dict, update_dict, many_to_many_dict, return_data_key, \ return_data_dict = default_update_data_to_database( model, find_for_write_default_dict, update_dict, many_to_many_dict, return_data_key, return_data_dict) logger.debug("更新的查询字典是: %s" % find_for_write_default_dict) logger.debug("更新字典是: %s" % update_dict) objs = model.objects.filter(**find_for_write_default_dict) if not objs: return_data_f = status_dict.get_FAIL_BY_NOTHING_TO_DO_4040( "没找到需要修改的对象") return_data_f.update({ "status": "FAIL", "type": model.__name__, }) return return_data_f, 400 objs.update(**update_dict) for obj in objs: for k, v in many_to_many_dict.items(): attr = getattr(obj, k) attr.clear() if isinstance(v, list): v = [int(l) for l in v] attr.add(*v) else: attr.add(int(v)) except Exception as e: transaction.savepoint_rollback(sid) # 失败回滚事务(如果数据库操作发生异常,回滚到设置的事务保存点) return { # "msg": "%s文件%s行发生%s错误" % ( # e.__traceback__.tb_frame.f_globals["__file__"], e.__traceback__.tb_lineno, e), "msg": "%s" % e, "code": status_dict.SERVER_ERROR, "status": "FAIL", "type": model.__name__, }, 500 else: transaction.savepoint_commit(sid) # 如果没有异常,成功提交事物 logger.info("%s模型修改了一条数据%s, %s成功" % (model.__str__, str(find_for_write_default_dict), str(update_dict))) if objs.count() > 1: update_data = get_model_data_by_ids( model=model, ids=[obj.id for obj in objs], return_data_dict=return_data_dict) else: update_data = get_model_data_by_id( model=model, id=objs[0].id, return_data_dict=return_data_dict) if not update_data: return_json = status_dict.get_UPDATE_SUCCESS_2002( msg="修改成功, 但查询新增数据时失败") return_json.update({ "status": "OK", "type": model.__name__, }) else: return_json = status_dict.get_UPDATE_SUCCESS_2002() return_json.update({ "status": "OK", "type": model.__name__, }) return_json["data"] = { return_data_key: models_to_json(data=update_data) } return return_json, 200