Repository: OpenEnergyPlatform/oeplatform

Issue:
Scenario Bundles: adding a region does not save Y
## Description of the issue

When I add a region to a scenario and save the bundle, it won't show up. 
## Steps to Reproduce
1. Add a region to a scenario in a bundle
2. Click save
3. Navigate to "view", hit refresh
Sometime it shows up once after refresh, but then misses afterwards.
I added Germany to this bundle a couple of times, but it won't stay : https://openenergy-platform.org/scenario-bundles/id/fd14cec6-9144-3207-eac1-40af83caf4c2

cc @wingechr 

## Ideas of solution

Describe possible ideas for solution and evaluate advantages and disadvantages.

## Context and Environment
* Version used: 
* Operating system: 
* Environment setup and (python) version: 

## Workflow checklist
- [ ] I am aware of the workflow in [CONTRIBUTING.md](https://github.com/OpenEnergyPlatform/oeplatform/blob/develop/CONTRIBUTING.md)



Discussion:

Resolving patch:
diff --git a/factsheet/urls.py b/factsheet/urls.py
--- a/factsheet/urls.py
+++ b/factsheet/urls.py
@@ -31,4 +31,5 @@
     path(
         r"check-owner/<str:bundle_id>/", views.check_ownership, name="check_ownership"
     ),
+    path(r"is_logged_in/", views.is_logged_in),
 ]
diff --git a/factsheet/views.py b/factsheet/views.py
--- a/factsheet/views.py
+++ b/factsheet/views.py
@@ -1,23 +1,24 @@
 import logging
 from django.shortcuts import render
-from django.http import Http404, HttpResponse, JsonResponse, HttpResponseForbidden
+from django.http import Http404, HttpResponse, JsonResponse, StreamingHttpResponse
+from rest_framework import status
 import json
 from django.views.decorators.csrf import csrf_exempt
 from django.core import serializers
 from django.utils.cache import patch_response_headers
 
-# import uuid
-# import requests
-# import rdflib
-from rdflib import Graph, Literal, RDF, URIRef
+import uuid
+import requests
+import rdflib
+from rdflib import ConjunctiveGraph, Graph, Literal, RDF, URIRef, BNode, XSD
+
 from rdflib.compare import to_isomorphic, graph_diff
 from rdflib.plugins.stores import sparqlstore
-from rdflib.namespace import Namespace
+from rdflib.namespace import XSD, Namespace
 from rdflib.graph import DATASET_DEFAULT_GRAPH_ID as default
 import os
 from oeplatform.settings import ONTOLOGY_ROOT, RDF_DATABASES, OPEN_ENERGY_ONTOLOGY_NAME
 
-# from datetime import date
 from SPARQLWrapper import SPARQLWrapper, JSON
 import sys
 from owlready2 import get_ontology
@@ -32,6 +33,7 @@
 # from rest_framework.authentication import TokenAuthentication
 # from rest_framework.permissions import IsAuthenticated
 # from rest_framework.authtoken.models import Token
+
 from django.contrib.auth.decorators import login_required
 
 from .models import OEKG_Modifications, ScenarioBundleAccessControl
@@ -40,11 +42,12 @@
 from factsheet.permission_decorator import only_if_user_is_owner_of_scenario_bundle
 
 versions = os.listdir(
-    Path(ONTOLOGY_ROOT, OPEN_ENERGY_ONTOLOGY_NAME)
+    Path(ONTOLOGY_ROOT, "oeo")
 )  # TODO bad - windows dev will get path error
 # Bryans custom hack!! print(versions.remove(".DS_Store"))
 version = max((d for d in versions), key=lambda d: [int(x) for x in d.split(".")])
-onto_base_path = Path(ONTOLOGY_ROOT, OPEN_ENERGY_ONTOLOGY_NAME)
+ONTHOLOGY_NAME = "oeo"
+onto_base_path = Path(ONTOLOGY_ROOT, ONTHOLOGY_NAME)
 path = onto_base_path / version  # TODO bad - windows dev will get path error
 # file = "reasoned-oeo-full.owl" # TODO- set in settings
 file = "oeo-full.owl"  # TODO- set in settings
@@ -346,24 +349,34 @@ def create_factsheet(request, *args, **kwargs):
                 if "interacting_regions" in item:
                     for interacting_region in item["interacting_regions"]:
                         interacting_region_URI = URIRef(interacting_region["iri"])
-                        interacting_regions = URIRef(
+                        scenario_interacting_region = URIRef(
                             "http://openenergy-platform.org/ontology/oekg/"
                             + interacting_region["iri"]
                         )
 
-                        bundle.add((interacting_regions, RDF.type, OEO.OEO_00020036))
                         bundle.add(
-                            (interacting_regions, RDFS.label, Literal(region["name"]))
+                            (scenario_interacting_region, RDF.type, OEO.OEO_00020036)
+                        )
+                        bundle.add(
+                            (
+                                scenario_interacting_region,
+                                RDFS.label,
+                                Literal(interacting_region["name"]),
+                            )
                         )
                         bundle.add(
                             (
-                                interacting_regions,
+                                scenario_interacting_region,
                                 OEKG["reference"],
                                 interacting_region_URI,
                             )
                         )
                         bundle.add(
-                            (scenario_URI, OEO.OEO_00020222, interacting_regions)
+                            (
+                                scenario_URI,
+                                OEO.OEO_00020222,
+                                scenario_interacting_region,
+                            )
                         )
 
                 if "scenario_years" in item:
@@ -664,31 +677,42 @@ def update_factsheet(request, *args, **kwargs):
                                 region_URI,
                             )
                         )
+                        new_bundle.add(
+                            (scenario_URI, OEO.OEO_00020220, scenario_region)
+                        )
 
                 if "interacting_regions" in item:
                     for interacting_region in item["interacting_regions"]:
                         interacting_region_URI = URIRef(interacting_region["iri"])
-                        interacting_regions = URIRef(
+                        scenario_interacting_region = URIRef(
                             "http://openenergy-platform.org/ontology/oekg/"
                             + interacting_region["iri"]
                         )
 
                         new_bundle.add(
-                            (interacting_regions, RDF.type, OEO.OEO_00020036)
+                            (scenario_interacting_region, RDF.type, OEO.OEO_00020036)
                         )
                         new_bundle.add(
-                            (interacting_regions, RDFS.label, Literal(region["name"]))
+                            (
+                                scenario_interacting_region,
+                                RDFS.label,
+                                Literal(interacting_region["name"]),
+                            )
                         )
                         new_bundle.add(
                             (
-                                interacting_regions,
+                                scenario_interacting_region,
                                 OEKG["reference"],
                                 interacting_region_URI,
                             )
                         )
 
                         new_bundle.add(
-                            (scenario_URI, OEO.OEO_00020222, interacting_regions)
+                            (
+                                scenario_URI,
+                                OEO.OEO_00020222,
+                                scenario_interacting_region,
+                            )
                         )
 
                 if "scenario_years" in item:
@@ -912,6 +936,21 @@ def update_factsheet(request, *args, **kwargs):
         return response
 
 
+def is_logged_in(request, *args, **kwargs):
+    user = None
+    if request.user.is_authenticated:
+        user = True
+
+    output = ""
+    if user is None:
+        output = "NOT_LOGGED_IN"
+    else:
+        output = "LOGGED_IN"
+    response = JsonResponse(output, safe=False, content_type="application/json")
+    patch_response_headers(response, cache_timeout=1)
+    return response
+
+
 def factsheet_by_name(request, *args, **kwargs):
     name = request.GET.get("name")
     factsheet = Factsheet.objects.get(name=name)


Test patch: