Skip to content

Instantly share code, notes, and snippets.

@wanderindev
Created June 28, 2022 22:41
Show Gist options
  • Select an option

  • Save wanderindev/06ecec5c2cd16fe6e440e8c535157e18 to your computer and use it in GitHub Desktop.

Select an option

Save wanderindev/06ecec5c2cd16fe6e440e8c535157e18 to your computer and use it in GitHub Desktop.
. . .
@jwt_required()
def post(self):
parser = self.get_parser()
data = parser.parse_args()
if self.parsed_model['unique']:
for constraint in self.parsed_model['unique']:
_filter = {}
for key in constraint:
_filter[key] = data[key]
if self.model.query.filter_by(**_filter).first():
return {'message': f'El valor "{_filter}" viola '
f'UNIQUE_CONSTRAINT de la tabla'}, 400
record = self.model(**data)
try:
record.save_to_db()
if 'stgid' in data:
return 'ok', 201
return {
'message': 'Registro creado exitosamente.',
'record': self.model.find_by_id(
record.id,
current_identity
).to_dict()
}, 201
except SQLAlchemyError as e:
return {'message': f'Ocurrió un error al tratar de crear '
f'el registro. Error: "{e}"'}, 500
. . .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment