Beispiel #1
0
def q_realization_add_relationship_value(request):
    valid_request, msg = validate_request(request)
    if not valid_request:
        return HttpResponseForbidden(msg)

    target_proxy_id = request.POST.get("target_proxy_id")
    property_key = request.POST.get("key")

    session_key = get_key_from_request(request)
    cached_realizations_key = "{0}_realizations".format(session_key)

    cached_realizations = get_cached_object(request.session, cached_realizations_key)
    if not cached_realizations:
        msg = "unable to locate cached_realizations"
        raise QError(msg)

    property = get_property_realization_by_fn(
        lambda r: r.get_key() == property_key,
        cached_realizations
    )
    if not property:
        raise QError("unable to find property w/ key='{0}'".format(property_key))

    target_proxy = QModelProxy.objects.get(id=target_proxy_id)
    new_model_realization = get_new_realizations(
        project=cached_realizations.project,
        ontology=cached_realizations.ontology,
        model_proxy=target_proxy,
        key=target_proxy.name,
    )

    # double-check that adding this model to this property makes sense...
    assert target_proxy in property.proxy.relationship_target_models.all()
    assert property.get_cardinality_max() == '*' or property.relationship_values(manager="allow_unsaved_relationship_values_manager").count() < int(property.get_cardinality_max())

    # add the model...
    property.relationship_values(manager="allow_unsaved_relationship_values_manager").add_potentially_unsaved(new_model_realization)
    with allow_unsaved_fk(QModel, ["relationship_property"]):
        # in theory, Django doesn't store unsaved relationship
        # the custom manager above gets around this for the m2m relationship (property to model) and it is what I ought to use
        # however, in order to work my way up the realization hierarchy I need access to the reverse of that relationship
        # which is a fk relationship; hence this extra bit of code (which only exists so that "model_realizations.py#QRealization.get_parent_model_realization" works)
        new_model_realization.relationship_property = property

    # re-cache the changed realizations...
    request.session[cached_realizations_key] = cached_realizations

    # and return a serialized version of that model...
    new_model_realization_serialization = serialize_new_realizations(new_model_realization)
    return JsonResponse(new_model_realization_serialization)
Beispiel #2
0
def q_realization_remove_relationship_value(request):
    valid_request, msg = validate_request(request)
    if not valid_request:
        return HttpResponseForbidden(msg)

    target_index = int(request.POST.get("target_index"))
    property_key = request.POST.get("key")

    session_key = get_key_from_request(request)
    cached_realizations_key = "{0}_realizations".format(session_key)

    cached_realizations = get_cached_object(request.session, cached_realizations_key)
    if not cached_realizations:
        msg = "unable to locate cached_realizations"
        raise QError(msg)

    property = get_property_realization_by_fn(
        lambda r: r.get_key() == property_key,
        cached_realizations
    )
    if not property:
        raise QError("unable to find property w/ key='{0}'".format(property_key))

    # remove the model...
    try:
        target_to_remove = property.relationship_values(manager="allow_unsaved_relationship_values_manager").all()[target_index]
        property.relationship_values(manager="allow_unsaved_relationship_values_manager").remove_potentially_unsaved(target_to_remove)
        if target_to_remove.is_existing():
            target_to_remove.delete()
    except IndexError:
        raise QError("unable to find target of {0} at index {1}".format(property, target_index))

    # re-cache the changed realizations...
    request.session[cached_realizations_key] = cached_realizations

    # and return a success msg...
    msg = "Successfully removed object"
    # (don't need to explicitly render this msg using Django messaging framework)
    # (b/c it will be obvious to the user that a model was removed since a subform will dissappear)
    # (and, anyway, there is no corresponding msg that is used when a model is added to a property)
    # messages.add_message(request, messages.SUCCESS, msg)
    return JsonResponse({"msg": msg})