예제 #1
0
파일: base.py 프로젝트: cgwire/zou
    def post(self):
        """
        Create a model with data given in the request body. JSON format is
        expected. The model performs the validation automatically when
        instantiated.
        """
        try:
            data = request.json
            if data is None:
                raise ArgumentsException(
                    "Data are empty. Please verify that you sent JSON data and"
                    " that you set the right headers.")
            self.check_create_permissions(data)
            self.check_creation_integrity(data)
            data = self.update_data(data)
            instance = self.model.create(**data)
            instance_dict = self.post_creation(instance)
            self.emit_create_event(instance_dict)
            return instance_dict, 201

        except TypeError as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400

        except IntegrityError as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400

        except StatementError as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400

        except ArgumentsException as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400
예제 #2
0
파일: task_type.py 프로젝트: sayanel/zou
 def update_data(self, data):
     name = data.get("name", None)
     task_type = TaskType.get_by(name=name)
     if task_type is not None:
         raise ArgumentsException(
             "A task type with similar name already exists")
     return data
예제 #3
0
파일: task_type.py 프로젝트: sayanel/zou
 def update_data(self, data, instance_id):
     name = data.get("name", None)
     if name is not None:
         task_type = TaskType.get_by(name=name)
         if task_type is not None and instance_id != str(task_type.id):
             raise ArgumentsException(
                 "A task type with similar name already exists")
     return data
예제 #4
0
 def check_creation_integrity(self, data):
     schedule_item = ScheduleItem.get_by(
         project_id=data.get("project_id", None),
         task_type_id=data.get("task_type_id", None),
         object_id=data.get("object_id", None),
     )
     if schedule_item is not None:
         raise ArgumentsException("A similar schedule item already exists")
     return schedule_item
예제 #5
0
파일: base.py 프로젝트: flavienliger/zou
    def put(self, instance_id):
        """
        Update a model with data given in the request body. JSON format is
        expected. Model performs the validation automatically when fields are
        modified.
        """
        try:
            data = self.get_arguments()
            if data is None:
                raise ArgumentsException(
                    "Data are empty. Please verify that you sent JSON data and "
                    "that you set the right headers.")
            instance = self.get_model_or_404(instance_id)
            instance_dict = instance.serialize()
            self.check_update_permissions(instance_dict, data)
            self.pre_update(instance_dict, data)
            data = self.update_data(data, instance_id)
            changes_dict = self.make_changes_dict(instance_dict, data)
            instance.update(data)
            instance_dict = instance.serialize()
            self.emit_update_event(instance_dict,
                                   data={"changes": changes_dict})
            self.post_update(instance_dict)
            return instance_dict, 200

        except TypeError as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400

        except IntegrityError as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400

        except StatementError as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400

        except ArgumentsException as exception:
            current_app.logger.error(str(exception), exc_info=1)
            return {"message": str(exception)}, 400