def get_graph_json(request): cache = get_cache('fat') key = 'path_graph_json' result = cache.get(key) latest = Path.latest_updated() if result and latest: cache_latest, json_graph = result # Not empty and still valid if cache_latest and cache_latest >= latest: return HttpJSONResponse(json_graph) # cache does not exist or is not up to date # rebuild the graph and cache the json def path_modifier(path): l = 0.0 if math.isnan(path.length) else path.length return {"id": path.pk, "length": l} graph = graph_lib.graph_edges_nodes_of_qs( Path.objects.all(), value_modifier=path_modifier, key_modifier=graph_lib.get_key_optimizer()) json_graph = json.dumps(graph) cache.set(key, (latest, json_graph)) return HttpJSONResponse(json_graph)
def merge_path(request): """ Path merging view """ response = {} if request.method == 'POST': ids_path_merge = request.POST.getlist('path[]') assert len(ids_path_merge) == 2 path_a = Path.objects.get(pk=ids_path_merge[0]) path_b = Path.objects.get(pk=ids_path_merge[1]) if not path_a.same_structure( request.user) or not path_b.same_structure(request.user): response = { 'error': _(u"You don't have the right to change these paths") } return HttpJSONResponse(response) try: result = path_a.merge_path(path_b) except Exception as exc: response = { 'error': exc, } return HttpJSONResponse(response) if result == 2: response = { 'error': _(u"You can't merge 2 paths with a 3rd path in the intersection" ) } elif result == 0: response = {'error': _(u"No matching points to merge paths found")} else: response = {'success': _(u"Paths merged successfully")} messages.success(request, _(u"Paths merged successfully")) return HttpJSONResponse(response)
def get_graph_json(request): cache = caches['fat'] key = 'path_graph_json' result = cache.get(key) latest = Path.latest_updated() if result and latest: cache_latest, json_graph = result # Not empty and still valid if cache_latest and cache_latest >= latest: return HttpJSONResponse(json_graph) # cache does not exist or is not up to date # rebuild the graph and cache the json graph = graph_lib.graph_edges_nodes_of_qs(Path.objects.exclude(draft=True)) json_graph = json.dumps(graph) cache.set(key, (latest, json_graph)) return HttpJSONResponse(json_graph)