def get_cached_realizations(request): assert request.method == "GET" try: session_key = get_key_from_request(request) cached_realizations_key = "{0}_realizations".format(session_key) realizations = get_cached_object(request.session, cached_realizations_key) except QError: # ** THIS SECTION JUST EXISTS FOR DEBUGGING ** import ipdb; ipdb.set_trace() session_key = "25b9d538-8b22-4cd1-b668-f934b716f323" cached_realizations_key = "{0}_realizations".format(session_key) realizations = get_cached_object(request.session, cached_realizations_key) serialized_realizations = serialize_realizations(realizations) return Response(serialized_realizations)
def get_cached_customization(request): assert request.method == "GET" try: session_key = get_key_from_request(request) cached_customization_set_key = "{0}_customization_set".format(session_key) customization_set = get_cached_object(request.session, cached_customization_set_key) except QError: # TODO: THIS SECTION JUST EXISTS FOR DEBUGGING session_key = "cef3f8d5-f8f9-4490-b396-3c8e7e25aece" cached_customization_set_key = "{0}_customization_set".format(session_key) customization_set = get_cached_object(request.session, cached_customization_set_key) serialized_customization_set = serialize_customization_set(customization_set) return Response(serialized_customization_set)
def get_cached_customizations(request): assert request.method == "GET" try: session_key = get_key_from_request(request) cached_customizations_key = "{0}_customizations".format(session_key) customizations = get_cached_object(request.session, cached_customizations_key) except QError: # ** THIS SECTION JUST EXISTS FOR DEBUGGING ** # import ipdb; ipdb.set_trace() session_key = "d33e5c1c-8c86-4d7f-bf44-b479745411d1" cached_customizations_key = "{0}_customizations".format(session_key) customizations = get_cached_object(request.session, cached_customizations_key) serialized_customizations = serialize_customizations(customizations) return Response(serialized_customizations)
def get_cached_customizations(request): assert request.method == "GET" try: session_key = get_key_from_request(request) cached_customizations_key = "{0}_customizations".format(session_key) customizations = get_cached_object(request.session, cached_customizations_key) except QError: # ** THIS SECTION JUST EXISTS FOR DEBUGGING ** import ipdb ipdb.set_trace() session_key = "36493064-6c85-4e7b-af9b-bd2132b4d927" cached_customizations_key = "{0}_customizations".format(session_key) customizations = get_cached_object(request.session, cached_customizations_key) serialized_customizations = serialize_new_customizations(customizations) return Response(serialized_customizations)
def q_realization_remove_relationship_value(request): # check the request was valid... valid_request, msg = validate_request(request) if not valid_request: return HttpResponseForbidden(msg) target_index = request.POST.get("target_index") target_key = request.POST.get("target_key") 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" return HttpResponseBadRequest(msg) # do some sanity checks... # check the realization to remove from exists... property_realization = get_property_realization_by_key(property_key, cached_realizations) if not property_realization: msg = "unable to find a QPropertyRealization with a key of '{0}'".format(property_key) return HttpResponseBadRequest(msg) # check that the target to remove exists... target_realizations = property_realization.relationship_values(manager="allow_unsaved_relationship_values_manager").filter_potentially_unsaved(key=target_key) if len(target_realizations) != 1: msg = "unable to find a QModelProxy with a key of '{0}'".format(target_key) return HttpResponseBadRequest(msg) target_realization = target_realizations[0] # check that it makes sense to remove this target... if property_realization.relationship_values(manager="allow_unsaved_relationship_values_manager").count() <= property_realization.cardinality_min: msg = "you have cannot remove this many QModelRealizations from this this QPropertyRealization" return HttpResponseBadRequest(msg) # check the user has permission to modify the realization... current_user = request.user project = cached_realizations.project if project.authenticated: if not current_user.is_authenticated() or not is_member_of(current_user, project): msg = "{0} does not have permission to modify a realization".format(current_user) return HttpResponseForbidden(msg) # ...okay, sanity checks are over # now remove the target... property_realization.relationship_values(manager="allow_unsaved_relationship_values_manager").remove_potentially_unsaved(target_realization) if target_realization.is_existing: target_realization.delete() request.session[cached_realizations_key] = cached_realizations # finally return a success msg... msg = "Successfully removed object" return JsonResponse({"msg": msg})
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)
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})
def q_load_section(request, section_type=None): valid_request, msg = validate_request(request) if not valid_request: return HttpResponseForbidden(msg) try: section_info = SECTION_MAP[section_type] except KeyError: msg = "I don't know how to render a section w/ type '{0}'".format(section_type) return HttpResponseBadRequest(msg) model_index = request.POST.get("index") model_key = request.POST.get("key") model_scope = request.POST.get("scope") session_key = get_key_from_request(request) cached_models_key = section_info["cached_models_key"].format(session_key=session_key) cached_models = get_cached_object(request.session, cached_models_key) if not cached_models: msg = "unable to locate cached_models" raise QError(msg) get_model_fn = section_info["get_model_fn"] model = get_model_fn(model_key, cached_models) if not model: raise QError("unable to find instance w/ key='{0}'".format(model_key)) template_context = {} for context_key, context_value in section_info.get("template_context").iteritems(): form_class = context_value["class"] template_context[context_key] = form_class( instance=model, name=context_value["name"].format(safe_key=model_key.replace('-', '_')), scope_prefix=context_value["scope_prefix"].format(index=model_index) ) template = "{0}/sections/{1}".format(APP_LABEL, section_info["template"]) return render_to_response(template, template_context, context_instance=RequestContext(request))
def q_realization_add_relationship_value(request): # check the request was valid... 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" return HttpResponseBadRequest(msg) # do some sanity checks... # check the realization to add to exists... property_realization = get_property_realization_by_key(property_key, cached_realizations) if not property_realization: msg = "unable to find a QPropertyRealization with a key of '{0}'".format(property_key) return HttpResponseBadRequest(msg) # check that the target to add exists... try: target_proxy = QModelProxy.objects.get(id=target_proxy_id) except QModelProxy.DoesNotExist: msg = "unable to find a QModelProxy with an id of '{0}'".format(target_proxy_id) return HttpResponseBadRequest(msg) # check that it makes sense to add this target... if target_proxy not in property_realization.proxy.relationship_target_models.all(): msg = "you are trying to add the wrong type of QModelRealization to this QPropertyRealization" return HttpResponseBadRequest(msg) if (not property_realization.is_infinite) and (property_realization.relationship_values(manager="allow_unsaved_relationship_values_manager").count() >= property_realization.cardinality_max): msg = "you have already added the maximum amount of QModelRealizations to this QPropertyRealization" return HttpResponseBadRequest(msg) # check the user has permission to modify the realization... current_user = request.user project = cached_realizations.project if project.authenticated: if not current_user.is_authenticated() or not is_member_of(current_user, project): msg = "{0} does not have permission to modify a realization".format(current_user) return HttpResponseForbidden(msg) # ...okay, sanity checks are over # now create the model... new_model_realization = get_new_realizations( project=project, ontology=target_proxy.ontology, model_proxy=target_proxy, key=target_proxy.name, ) # now add the model... property_realization.relationship_values(manager="allow_unsaved_relationship_values_manager").add_potentially_unsaved(new_model_realization) with allow_unsaved_fk(QModelRealization, ["relationship_property"]): # the custom manager above ("allow_unsaved_relationship_values_manager") lets me cope w/ an unsaved m2m relationship - it is what I ought to use # however, some fns ("QRealization.get_root_realization") needs access to the reverse of that relationship; hence this extra bit of code new_model_realization.relationship_property = property_realization request.session[cached_realizations_key] = cached_realizations # finally return a serialized version of that model... new_model_realization_serialization = serialize_realizations(new_model_realization) return JsonResponse(new_model_realization_serialization)