예제 #1
0
def do_edit(request, entity_id, recv_data):
    entity, error = get_obj_with_check_perm(request.user, Entity, entity_id, ACLType.Writable)
    if error:
        return error

    # validation checks
    for attr in recv_data["attrs"]:
        # formalize recv_data format
        if "ref_ids" not in attr:
            attr["ref_ids"] = []

        if int(attr["type"]) & AttrTypeValue["object"] and not attr["ref_ids"]:
            return HttpResponse("Need to specify enabled referral ids", status=400)

        if any([not Entity.objects.filter(id=x).exists() for x in attr["ref_ids"]]):
            return HttpResponse("Specified referral is invalid", status=400)

    # duplication checks
    counter = collections.Counter(
        [
            attr["name"]
            for attr in recv_data["attrs"]
            if "deleted" not in attr or not attr["deleted"]
        ]
    )
    if len([v for v, count in counter.items() if count > 1]):
        return HttpResponse("Duplicated attribute names are not allowed", status=400)

    # prevent to show edit page under the processing
    if entity.get_status(Entity.STATUS_EDITING):
        return HttpResponse("Target entity is now under processing", status=400)

    if custom_view.is_custom("edit_entity"):
        resp = custom_view.call_custom(
            "edit_entity", None, entity, recv_data["name"], recv_data["attrs"]
        )
        if resp:
            return resp

    # update status parameters
    if recv_data["is_toplevel"]:
        entity.set_status(Entity.STATUS_TOP_LEVEL)
    else:
        entity.del_status(Entity.STATUS_TOP_LEVEL)

    # update entity metatada informations to new ones
    entity.set_status(Entity.STATUS_EDITING)

    # Create a new job to edit entity and run it
    job = Job.new_edit_entity(request.user, entity, params=recv_data)
    job.run()

    new_name = recv_data["name"]
    return JsonResponse(
        {
            "entity_id": entity.id,
            "entity_name": new_name,
            "msg": 'Success to schedule to update Entity "%s"' % new_name,
        }
    )
예제 #2
0
def do_edit(request, entity_id, recv_data):
    user = User.objects.get(id=request.user.id)
    entity, error = get_object_with_check_permission(user, Entity, entity_id,
                                                     ACLType.Writable)
    if error:
        return error

    # validation checks
    for attr in recv_data['attrs']:
        # formalize recv_data format
        if 'ref_ids' not in attr:
            attr['ref_ids'] = []

        if int(attr['type']) & AttrTypeValue['object'] and not attr['ref_ids']:
            return HttpResponse('Need to specify enabled referral ids',
                                status=400)

        if any([
                not Entity.objects.filter(id=x).exists()
                for x in attr['ref_ids']
        ]):
            return HttpResponse('Specified referral is invalid', status=400)

    # prevent to show edit page under the processing
    if entity.get_status(Entity.STATUS_EDITING):
        return HttpResponse('Target entity is now under processing',
                            status=400)

    if custom_view.is_custom('edit_entity'):
        resp = custom_view.call_custom('edit_entity', None, entity,
                                       recv_data['name'], recv_data['attrs'])
        if resp:
            return resp

    # update status parameters
    if recv_data['is_toplevel']:
        entity.set_status(Entity.STATUS_TOP_LEVEL)
    else:
        entity.del_status(Entity.STATUS_TOP_LEVEL)

    # update entity metatada informations to new ones
    entity.set_status(Entity.STATUS_EDITING)

    # Create a new job to edit entity and run it
    job = Job.new_edit_entity(user, entity, params=recv_data)
    job.run()

    new_name = recv_data['name']
    return JsonResponse({
        'entity_id':
        entity.id,
        'entity_name':
        new_name,
        'msg':
        'Success to schedule to update Entity "%s"' % new_name,
    })