예제 #1
0
def delete_items(portal_type, request, uid=None, endpoint=None):
    """ delete items

    1. If the uid is given, we can ignore the request body and delete the
       object with the given uid (if the uid was valid).
    2. If no uid is given, the user wants to delete more than one item.
       => go through each item and extract the uid. Delete it afterwards.
       // we should do this kind of transaction base. So if we can not get an
       // object for an uid, no item will be deleted.
    3. we could check if the portal_type matches, just to be sure the user
       wants to delete the right content.
    """

    objects = []
    if uid:
        objects.append(get_object_by_uid(uid))
    else:
        payload = get_request_data(request)
        objects = (map(get_object_by_uid, _.pluck(payload, "uid")))

    results = []
    for obj in objects:
        result = {"id": obj.getId()}
        result["deleted"] = ploneapi.content.delete(obj) == None and True or False
        results.append(result)

    return results
예제 #2
0
def update_items(portal_type, request, uid=None, endpoint=None):
    """ update items

    1. If the uid is given, the user wants to update the object with the data
       given in request body
    2. If no uid is given, the user wants to update a bunch of objects.
    """

    # the data to update
    records = get_request_data(request)

    objects = []
    if uid:
        objects.append(get_object_by_uid(uid))
    else:
        # get the objects for the given uids
        objects = (map(get_object_by_uid, _.pluck(records, "uid")))

    results = []
    for obj in objects:
        # get the update dataset for this object
        gsm = getSecurityManager()
        if not gsm.checkPermission(permissions.ModifyPortalContent, obj):
            raise Unauthorized("You may not modify this object")

        if uid:
            record = records and records[0] or {}
        else:
            # the uid is inside the payload
            record = filter(lambda d: get_uid(obj) == d.get("uid"), records)
            record = record and record[0] or {}

        # do a wf transition
        if record.get("transition", None):
            t = record.get("transition")
            logger.info(">>> Do Transition '%s' for Enquiry %s", t, obj.getId())
            do_action_for(obj, t)

        obj = update_object_with_data(obj, record)
        results.append(obj)
    return make_items_for(results, endpoint=endpoint)