Created
June 28, 2022 22:41
-
-
Save wanderindev/06ecec5c2cd16fe6e440e8c535157e18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| . . . | |
| @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