本文整理汇总了Python中models.Message.is_unique方法的典型用法代码示例。如果您正苦于以下问题:Python Message.is_unique方法的具体用法?Python Message.is_unique怎么用?Python Message.is_unique使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Message
的用法示例。
在下文中一共展示了Message.is_unique方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: patch
# 需要导入模块: from models import Message [as 别名]
# 或者: from models.Message import is_unique [as 别名]
def patch(self, id):
message = Message.query.get_or_404(id)
message_dict = request.get_json(force=True)
if 'message' in message_dict:
message_message = message_dict['message']
if Message.is_unique(id=id, message=message_message):
message.message = message_message
else:
response = {'error': 'A message with the same message already exists'}
return response, status.HTTP_400_BAD_REQUEST
if 'duration' in message_dict:
message.duration = message_dict['duration']
if 'printed_times' in message_dict:
message.printed_times = message_dict['printed_times']
if 'printed_once' in message_dict:
message.printed_once = message_dict['printed_once']
dumped_message, dump_errors = message_schema.dump(message)
if dump_errors:
return dump_errors, status.HTTP_400_BAD_REQUEST
validate_errors = message_schema.validate(dumped_message)
if validate_errors:
return validate_errors, status.HTTP_400_BAD_REQUEST
try:
message.update()
return self.get(id)
except SQLAlchemyError as e:
db.session.rollback()
resp = {"error": str(e)}
return resp, status.HTTP_400_BAD_REQUEST
示例2: post
# 需要导入模块: from models import Message [as 别名]
# 或者: from models.Message import is_unique [as 别名]
def post(self):
request_dict = request.get_json()
if not request_dict:
response = {'message': 'No input data provided'}
return response, status.HTTP_400_BAD_REQUEST
errors = message_schema.validate(request_dict)
if errors:
return errors, status.HTTP_400_BAD_REQUEST
message_message = request_dict['message']
if not Message.is_unique(id=0, message=message_message):
response = {'error': 'A message with the same message already exists'}
return response, status.HTTP_400_BAD_REQUEST
try:
category_name = request_dict['category']['name']
category = Category.query.filter_by(name=category_name).first()
if category is None:
# Create a new Category
category = Category(name=category_name)
db.session.add(category)
# Now that we are sure we have a category
# create a new Message
message = Message(
message=message_message,
duration=request_dict['duration'],
category=category)
message.add(message)
query = Message.query.get(message.id)
result = message_schema.dump(query).data
return result, status.HTTP_201_CREATED
except SQLAlchemyError as e:
db.session.rollback()
resp = {"error": str(e)}
return resp, status.HTTP_400_BAD_REQUEST