Esempio n. 1
0
def read(group, request):
    """Fetch a group."""

    expand = request.GET.getall("expand") or []

    return GroupJSONPresenter(GroupContext(group,
                                           request)).asdict(expand=expand)
Esempio n. 2
0
def create(request):
    """Create a group from the POST payload."""
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request)
        or request.default_authority).validate(_json_payload(request))

    group_service = request.find_service(name='group')
    group_create_service = request.find_service(name='group_create')

    # Check for duplicate group
    groupid = appstruct.get('groupid', None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group:
            raise ConflictError(
                _("group with groupid '{}' already exists").format(groupid))

    group = group_create_service.create_private_group(
        name=appstruct['name'],
        userid=request.user.userid,
        description=appstruct.get('description', None),
        groupid=groupid,
    )
    return GroupJSONPresenter(GroupContext(
        group, request)).asdict(expand=['organization'])
Esempio n. 3
0
File: groups.py Progetto: kaydoh/h
def create(request):
    """Create a group from the POST payload."""
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=request.effective_authority,
    ).validate(_json_payload(request))

    group_service = request.find_service(name="group")
    group_create_service = request.find_service(name="group_create")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group:
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid))

    group = group_create_service.create_private_group(
        name=appstruct["name"],
        userid=request.user.userid,
        description=appstruct.get("description", None),
        groupid=groupid,
    )
    return GroupJSONPresenter(
        group, request).asdict(expand=["organization", "scopes"])
Esempio n. 4
0
def upsert(context, request):
    """
    Create or update a group from a PUT payload.

    If no group model is present in the passed ``context`` (on ``context.group``),
    treat this as a create action and delegate to ``create``.

    Otherwise, replace the existing group's resource properties entirely and update
    the object.

    :arg context:
    :type context: h.traversal.GroupUpsertContext
    """
    if context.group is None:
        return create(request)

    group = context.group

    # Because this is a PUT endpoint and not a PATCH, a full replacement of the
    # entire resource is expected. Thus, we're validating against the Create schema
    # here as we want to make sure properties required for a fresh object are present
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request) or request.default_authority,
    ).validate(_json_payload(request))

    group_update_service = request.find_service(name="group_update")
    group_service = request.find_service(name="group")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group and (duplicate_group != group):
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid)
            )

    # Need to make sure every resource-defined property is present, as this
    # is meant as a full-resource-replace operation.
    # TODO: This may be better handled in the schema at some point
    update_properties = {
        "name": appstruct["name"],
        "description": appstruct.get("description", ""),
        "groupid": appstruct.get("groupid", None),
    }

    group = group_update_service.update(group, **update_properties)

    # Note that this view takes a ``GroupUpsertContext`` but uses a ``GroupContext`` here
    return GroupJSONPresenter(GroupContext(group, request)).asdict(
        expand=["organization", "scopes"]
    )
Esempio n. 5
0
def create(request):
    """Create a group from the POST payload."""
    schema = CreateGroupAPISchema()

    appstruct = schema.validate(_json_payload(request))
    group_properties = {
        'name': appstruct['name'],
        'description': appstruct.get('description', None),
    }

    group_service = request.find_service(name='group')

    group = group_service.create_private_group(
        group_properties['name'],
        request.user.userid,
        description=group_properties['description'],
    )

    group_context = GroupContext(group, request)
    return GroupJSONPresenter(group_context).asdict(expand=['organization'])
Esempio n. 6
0
def update(group, request):
    """Update a group from a PATCH payload."""
    appstruct = UpdateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request) or request.default_authority,
    ).validate(_json_payload(request))

    group_update_service = request.find_service(name="group_update")
    group_service = request.find_service(name="group")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group and (duplicate_group != group):
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid))

    group = group_update_service.update(group, **appstruct)

    return GroupJSONPresenter(GroupContext(
        group, request)).asdict(expand=["organization", "scopes"])
Esempio n. 7
0
def create(request):
    """Create a group from the POST payload."""
    # @TODO Temporary: Remove this check once private groups supported at other authorities
    if request.authority != request.user.authority:
        raise HTTPBadRequest(
            _('Group creation currently only supported for default authority'))

    schema = CreateGroupAPISchema()

    appstruct = schema.validate(_json_payload(request))
    group_properties = {
        'name': appstruct['name'],
        'description': appstruct.get('description', None),
    }

    group_service = request.find_service(name='group')

    group = group_service.create_private_group(
        group_properties['name'],
        request.user.userid,
        description=group_properties['description'],
    )
    group_context = GroupContext(group, request)
    return GroupJSONPresenter(group_context).asdict(expand=['organization'])