Exemplo n.º 1
0
def update_project(project_id):
    try:
        filtered_data = Project.filter_field(request.get_json())
        for key, value in filtered_data.items():
            Project.update({"_id": ObjectId(project_id)},
                           {'$set': {key: value}})
        update_response = Project.update({"_id": ObjectId(project_id)},
                       {'$set': {'lastUpdateTime': datetime.datetime.utcnow()}},)
        if update_response["n"] == 0:
            return jsonify({'status': 'failed', 'data': '未找到相应更新数据!'})
        return jsonify({'status': 'ok', 'data': '更新成功'})
    except BaseException as e:
        return jsonify({'status': 'failed', 'data': '更新失败: %s' % e})
Exemplo n.º 2
0
    def patch(self, project_id):
        """
        """

        try:
            current_user_id = get_jwt_identity()
            current_user_roles = get_jwt_claims()['roles']

            project_schema = ProjectSchema(only=("name", "description"),
                                           partial=True)

            project_data = request.get_json()

            validate_project_data, errors = project_schema.load(project_data)

            existing_project = False

            if errors:
                return dict(status='fail', message=errors), 400

            if 'name' in validate_project_data:
                existing_project = Project.find_first(
                    name=validate_project_data['name'],
                    owner_id=current_user_id)

            if existing_project:
                return dict(
                    status='fail',
                    message=
                    f'project with name {validate_project_data["name"]} already exists'
                ), 409

            project = Project.get_by_id(project_id)

            if not project:
                return dict(status='fail',
                            message=f'Project {project_id} not found'), 404

            if not is_owner_or_admin(project, current_user_id,
                                     current_user_roles):
                return dict(status='fail', message='unauthorised'), 403

            updated = Project.update(project, **validate_project_data)

            if not updated:
                return dict(status='fail',
                            message='internal server error'), 500

            return dict(
                status='success',
                message=f'project {project_id} updated successfully'), 200

        except Exception as e:
            return dict(status='fail', message=str(e)), 500