Skip to content

Scenario Bundles feature#

The scenario bundles feature is a response to the complex requirements for the transparent publication of scenarios in a complete and comprehensible manner. Various technologies are used to enable researchers to publish scenarios and any additional information. In addition, existing resources from the open energy platform are used and bundled together. This is intended to increase the visibility of available research work and enable comparability of the scenarios.

What are Scenario Bundles in detail?#

Please continue reading here.

Technologies#

User Interface

  • We offer a modern user interface developed with the REACT library.

Backend & Web-API

  • We build on the backend of the Open Energy Platform and use Django to implement functionalities such as saving and deleting scenario bundles and thus enable communication with the database. In addition, Django provides the WEB-API endpoints that are used to create a scenario bundle or query the database using JSON requests, for example.
  • A Python integration of the SPARQL query language is used to interact with the Grpah database.

Database

  • A graph database is used to store the complex data structure of the scenario bundles in the long term. We use Appache Jenna-Fuseki as a reliable technology.

Functionalities#

Code Documentation#

Django view for the scenario bundles#

Note

Some of the information on this page may be changed in the future. To review the most recent information, please revisit.

def create_factsheet(request, *args, **kwargs):

Creates a scenario bundle based on user's data. Currently, the minimum requirement to create a bundle is the "acronym". The "acronym" must be unique. If the provided acronym already exists in the OEKG, then the function returns a "Duplicate error".

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
uid str

The unique ID for the bundle.

required
acronym str

The acronym for the bundle.

required
abstract str

The abstract for the bundle.

required
institution list of objects

The institutions for the bundle.

required
funding_source list of objects

The funding sources for the bundle.

required
contact_person list of objects

The contact persons for the bundle.

required
sector_divisions list of objects

The sector divisions for the bundle.

required
sectors list of objects

The sectors for the bundle.

required
technologies list of objects

The technologies for the bundle.

required
study_keywords list of strings

The study keywords for the bundle.

required
scenarios list of objects

The scenarios for the bundle.

required
models list of strings

The models for the bundle.

required
frameworks list of strings

The frameworks for the bundle.

required
publications list[object]

A list of n publications related to the bundle study_name (str): The study name for the bundle. date_of_publication (str): The date of publication for the bundle. report_title (str): The report title for the bundle. report_doi (str): The report_doi for the bundle. place_of_publication (str): The place of publication for the bundle. link_to_study_report (str): The link to study for the bundle. authors (list of objects): The authors for the bundle.

required

Returns:

Type Description

"Factsheet saved" if successful, "Duplicate error" if the bundle's

acronym exists.

Source code in factsheet/views.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
def create_factsheet(request, *args, **kwargs):
    """
    Creates a scenario bundle based on user's data. Currently, the minimum requirement
    to create a bundle is the "acronym". The "acronym" must be unique. If the provided
    acronym already exists in the OEKG, then the function returns a "Duplicate error".

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        uid (str): The unique ID for the bundle.
        acronym (str): The acronym for the bundle.
        abstract (str): The abstract for the bundle.
        institution (list of objects): The institutions for the bundle.
        funding_source (list of objects): The funding sources for the bundle.
        contact_person (list of objects): The contact persons for the bundle.
        sector_divisions (list of objects): The sector divisions for the bundle.
        sectors (list of objects): The sectors for the bundle.
        technologies (list of objects): The technologies for the bundle.
        study_keywords (list of strings): The study keywords for the bundle.
        scenarios (list of objects): The scenarios for the bundle.
        models (list of strings): The models for the bundle.
        frameworks (list of strings): The frameworks for the bundle.
        publications (list[object]): A list of n publications related to the bundle
            study_name (str): The study name for the bundle.
            date_of_publication (str): The date of publication for the bundle.
            report_title (str): The report title for the bundle.
            report_doi (str): The report_doi for the bundle.
            place_of_publication (str): The place of publication for the bundle.
            link_to_study_report (str): The link to study for the bundle.
            authors (list of objects): The authors for the bundle.

    Returns:
        "Factsheet saved" if successful, "Duplicate error" if the bundle's
        acronym exists.

    """

    if not request.user.is_authenticated:
        return HttpResponseForbidden("User not authenticated")

    request_body = json.loads(request.body)
    name = request_body["name"]  # noqa
    uid = request_body["uid"]
    acronym = (request_body["acronym"])
    study_name = request_body["study_name"]
    abstract = request_body["abstract"]
    institution = request_body["institution"]
    funding_source = request_body["funding_source"]
    contact_person = request_body["contact_person"]
    sector_divisions = request_body["sector_divisions"]
    sectors = request_body["sectors"]
    # expanded_sectors = request_body["expanded_sectors"]  # noqa
    # energy_carriers = request_body['energy_carriers']
    # expanded_energy_carriers = request_body['expanded_energy_carriers']
    # energy_transformation_processes = request_body['energy_transformation_processes']
    # expanded_energy_transformation_processes = request_body['expanded_energy_transformation_processes'] # noqa
    technologies = request_body["technologies"]
    study_keywords = request_body["study_keywords"]
    scenarios = request_body["scenarios"]
    publications = request_body["publications"]
    models = request_body["models"]
    frameworks = request_body["frameworks"]

    Duplicate_study_factsheet = False

    for s, p, o in oekg.triples((None, RDF.type, OEO.OEO_00010252)):
        study_acronym = oekg.value(s, DC.acronym)
        if str(clean_name(acronym)) == str(study_acronym):
            Duplicate_study_factsheet = True

    if Duplicate_study_factsheet == True:  # noqa
        response = JsonResponse(
            "Factsheet exists", safe=False, content_type="application/json"
        )
        patch_response_headers(response, cache_timeout=1)
        return response
    else:
        bundle = Graph()

        study_URI = URIRef("http://openenergy-platform.org/ontology/oekg/" + uid)
        bundle.add((study_URI, RDF.type, OEO.OEO_00010252))

        if acronym != "":
            bundle.add((study_URI, DC.acronym, Literal(remove_non_printable(acronym))))
        if study_name != "":
            bundle.add((study_URI, OEKG["has_full_name"], Literal(remove_non_printable(study_name))))
        if abstract != "":
            bundle.add((study_URI, DC.abstract, Literal(remove_non_printable(abstract))))

        _publications = json.loads(publications) if publications is not None else []
        for item in _publications:
            publications_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/publication/" + item["id"]
            )
            bundle.add(
                (publications_URI, OEKG["publication_uuid"], Literal(item["id"]))
            )

            bundle.add((study_URI, OEKG["has_publication"], publications_URI))
            if item["report_title"] != "":
                bundle.add(
                    (publications_URI, RDFS.label, Literal(remove_non_printable(item["report_title"])))
                )

            _authors = item["authors"]
            for author in _authors:
                author_URI = URIRef(
                    "http://openenergy-platform.org/ontology/oekg/" + author["iri"]
                )
                bundle.add((publications_URI, OEO.OEO_00000506, author_URI))

            if item["doi"] != "":
                bundle.add((publications_URI, OEKG["doi"], Literal(item["doi"])))

            if (
                item["date_of_publication"] != "01-01-1900"
                and item["date_of_publication"] != ""
            ):
                bundle.add(
                    (
                        publications_URI,
                        OEKG["date_of_publication"],
                        Literal(item["date_of_publication"]),
                    )
                )

            if item["link_to_study_report"] != "":
                bundle.add(
                    (
                        publications_URI,
                        OEKG["link_to_study_report"],
                        Literal(item["link_to_study_report"]),
                    )
                )

            bundle.add((study_URI, OEKG["has_publication"], publications_URI))

        _scenarios = json.loads(scenarios) if scenarios is not None else []
        for item in _scenarios:
            if item["acronym"] != "":
                # TODO- set in settings
                scenario_URI = URIRef(
                    "http://openenergy-platform.org/ontology/oekg/scenario/"
                    + item["id"]
                )
                bundle.add((study_URI, OEKG["has_scenario"], scenario_URI))
                bundle.add((scenario_URI, RDFS.label, Literal(remove_non_printable(item["acronym"]))))
                if item["name"] != "":
                    bundle.add(
                        (scenario_URI, OEKG["has_full_name"], Literal(remove_non_printable(item["name"])))
                    )
                    bundle.add((scenario_URI, RDF.type, OEO.OEO_00000365))
                if item["abstract"] != "":
                    bundle.add((scenario_URI, DC.abstract, Literal(remove_non_printable(item["abstract"]))))

                bundle.add((scenario_URI, OEKG["scenario_uuid"], Literal(item["id"])))

                if "regions" in item:
                    for region in item["regions"]:
                        region_URI = URIRef(region["iri"])
                        scenario_region = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/region/"
                            + region["iri"].rsplit("/", 1)[1]
                        )
                        bundle.add((scenario_region, RDF.type, OEO.OEO_00020032))
                        bundle.add(
                            (scenario_region, RDFS.label, Literal(region["name"]))
                        )
                        bundle.add((scenario_region, OEKG["reference"], region_URI))
                        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"])
                        scenario_interacting_region = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/"
                            + interacting_region["iri"]
                        )

                        bundle.add(
                            (scenario_interacting_region, RDF.type, OEO.OEO_00020036)
                        )
                        bundle.add(
                            (
                                scenario_interacting_region,
                                RDFS.label,
                                Literal(interacting_region["name"]),
                            )
                        )
                        bundle.add(
                            (
                                scenario_interacting_region,
                                OEKG["reference"],
                                interacting_region_URI,
                            )
                        )
                        bundle.add(
                            (
                                scenario_URI,
                                OEO.OEO_00020222,
                                scenario_interacting_region,
                            )
                        )

                if "scenario_years" in item:
                    for scenario_year in item["scenario_years"]:
                        bundle.add(
                            (
                                scenario_URI,
                                OEO.OEO_00020224,
                                Literal(scenario_year["name"]),
                            )
                        )

                if "descriptors" in item:
                    for descriptor in item["descriptors"]:
                        descriptor = URIRef(descriptor["class"])
                        bundle.add(
                            (scenario_URI, OEO["has_scenario_descriptor"], descriptor)
                        )

                # TODO: jh-RLI: Update to avoid duplicated table name entries
                if "input_datasets" in item:
                    for input_dataset in item["input_datasets"]:
                        # TODO- set in settings
                        input_dataset_URI = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/input_datasets/"  # noqa
                            + input_dataset["key"]
                        )
                        bundle.add((input_dataset_URI, RDF.type, OEO.OEO_00030030))
                        bundle.add(
                            (
                                input_dataset_URI,
                                RDFS.label,
                                Literal(remove_non_printable(input_dataset["value"]["label"])),
                            )
                        )
                        bundle.add(
                            (
                                input_dataset_URI,
                                OEO["has_iri"],
                                Literal(input_dataset["value"]["url"]),
                            )
                        )
                        bundle.add(
                            (
                                input_dataset_URI,
                                OEO["has_id"],
                                Literal(input_dataset["idx"]),
                            )
                        )
                        bundle.add(
                            (
                                input_dataset_URI,
                                OEO["has_key"],
                                Literal(input_dataset["key"]),
                            )
                        )
                        bundle.add((scenario_URI, OEO.RO_0002233, input_dataset_URI))

                # TODO: jh-RLI: Update to avoid duplicated table name entries
                if "output_datasets" in item:
                    for output_dataset in item["output_datasets"]:
                        # TODO- set in settings
                        output_dataset_URI = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/output_datasets/"  # noqa
                            + output_dataset["key"]
                        )
                        bundle.add((output_dataset_URI, RDF.type, OEO.OEO_00030029))
                        bundle.add(
                            (
                                output_dataset_URI,
                                RDFS.label,
                                Literal(remove_non_printable(output_dataset["value"]["label"])),
                            )
                        )
                        bundle.add(
                            (
                                output_dataset_URI,
                                OEO["has_iri"],
                                Literal(output_dataset["value"]["url"]),
                            )
                        )
                        bundle.add(
                            (
                                output_dataset_URI,
                                OEO["has_id"],
                                Literal(output_dataset["idx"]),
                            )
                        )
                        bundle.add(
                            (
                                output_dataset_URI,
                                OEO["has_key"],
                                Literal(output_dataset["key"]),
                            )
                        )
                        bundle.add((scenario_URI, OEO.RO_0002234, output_dataset_URI))

        institutions = json.loads(institution) if institution is not None else []
        for item in institutions:
            institution_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/" + item["iri"]
            )
            bundle.add((study_URI, OEO.OEO_00000510, institution_URI))

        funding_sources = (
            json.loads(funding_source) if funding_source is not None else []
        )
        for item in funding_sources:
            funding_source_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/" + item["iri"]
            )
            bundle.add((study_URI, OEO.OEO_00000509, funding_source_URI))
        contact_persons = (
            json.loads(contact_person) if contact_person is not None else []
        )
        for item in contact_persons:
            contact_person_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/" + item["iri"]
            )
            bundle.add((study_URI, OEO.OEO_00000508, contact_person_URI))

        _sector_divisions = (
            json.loads(sector_divisions) if sector_divisions is not None else []
        )
        for item in _sector_divisions:
            sector_divisions_URI = URIRef(item["class"])
            bundle.add(
                (study_URI, OEO["based_on_sector_division"], sector_divisions_URI)
            )

        _sectors = json.loads(sectors) if sectors is not None else []
        for item in _sectors:
            sector_URI = URIRef(item["class"])
            bundle.add((study_URI, OEO.OEO_00000505, sector_URI))

        _technologies = json.loads(technologies) if technologies is not None else []
        for item in _technologies:
            technology_URI = URIRef(item["class"])
            bundle.add((study_URI, OEO.OEO_00000522, technology_URI))

        _models = json.loads(models) if models is not None else []
        for item in _models:
            model_id = item.get("id")
            if item.get("acronym"):
                model_acronym = item.get("acronym")
            else:
                model_acronym = item.get("name")
            model_url = item.get("url")

            if not model_id or not model_acronym or not model_url:
                continue  # Skip this item if any critical field is empty

            model_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/models/" + str(model_id)
            )
            bundle.add((model_URI, RDF.type, OEO.OEO_00000277))
            bundle.add(
                (
                    model_URI,
                    RDFS.label,
                    Literal(remove_non_printable(model_acronym)),
                )
            )
            bundle.add(
                (
                    model_URI,
                    OEO["has_iri"],
                    Literal(model_url),
                )
            )
            bundle.add((study_URI, OEO["has_model"], model_URI))

        _frameworks = json.loads(frameworks) if frameworks is not None else []
        for item in _frameworks:
            framework_id = item.get("id")
            if item.get("acronym"):
                framework_acronym = item.get("acronym")
            else:
                framework_acronym = item.get("name")
            framework_url = item.get("url")

            if not framework_id or not framework_acronym or not framework_url:
                continue  # Skip this item if any critical field is empty

            framework_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/frameworks/"
                + str(framework_id)
            )

            bundle.add((framework_URI, RDF.type, OEO.OEO_00000172))

            if framework_acronym:
                bundle.add(
                    (
                        framework_URI,
                        RDFS.label,
                        Literal(remove_non_printable(framework_acronym)),
                    )
                )

            if framework_url:
                bundle.add(
                    (
                        framework_URI,
                        OEO["has_iri"],
                        Literal(framework_url),
                    )
                )

            bundle.add((study_URI, OEO["has_framework"], framework_URI))

        _study_keywords = (
            json.loads(study_keywords) if study_keywords is not None else []
        )
        if _study_keywords != []:
            for keyword in _study_keywords:
                bundle.add((study_URI, OEO["has_study_keyword"], Literal(keyword)))

        for s, p, o in bundle.triples((None, None, None)):
            oekg.add((s, p, o))

        response = JsonResponse(
            "Factsheet saved", safe=False, content_type="application/json"
        )
        result = set_ownership(bundle_uid=uid, user=request.user)
        logging.info(result)
        patch_response_headers(response, cache_timeout=1)

        return response

def update_factsheet(request, *args, **kwargs):

Updates a scenario bundle based on user's data.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
uid str

The unique ID for the bundle.

required
acronym str

The acronym for the bundle.

required
abstract str

The abstract for the bundle.

required
institution list of objects

The institutions for the bundle.

required
funding_source list of objects

The funding sources for the bundle.

required
contact_person list of objects

The contact persons for the bundle.

required
sector_divisions list of objects

The sector divisions for the bundle.

required
sectors list of objects

The sectors for the bundle.

required
technologies list of objects

The technologies for the bundle.

required
study_keywords list of strings

The study keywords for the bundle.

required
scenarios list of objects

The scenarios for the bundle.

required
models list of strings

The models for the bundle.

required
frameworks list of strings

The frameworks for the bundle.

required
publications list[object]

A list of n publications related to the bundle study_name (str): The study name for the bundle. date_of_publication (str): The date of publication for the bundle. report_title (str): The report title for the bundle. report_doi (str): The report_doi for the bundle. place_of_publication (str): The place of publication for the bundle. link_to_study_report (str): The link to study for the bundle. authors (list of objects): The authors for the bundle.

required
Source code in factsheet/views.py
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
@login_required
@only_if_user_is_owner_of_scenario_bundle
def update_factsheet(request, *args, **kwargs):
    """
    Updates a scenario bundle based on user's data.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        uid (str): The unique ID for the bundle.
        acronym (str): The acronym for the bundle.
        abstract (str): The abstract for the bundle.
        institution (list of objects): The institutions for the bundle.
        funding_source (list of objects): The funding sources for the bundle.
        contact_person (list of objects): The contact persons for the bundle.
        sector_divisions (list of objects): The sector divisions for the bundle.
        sectors (list of objects): The sectors for the bundle.
        technologies (list of objects): The technologies for the bundle.
        study_keywords (list of strings): The study keywords for the bundle.
        scenarios (list of objects): The scenarios for the bundle.
        models (list of strings): The models for the bundle.
        frameworks (list of strings): The frameworks for the bundle.
        publications (list[object]): A list of n publications related to the bundle
            study_name (str): The study name for the bundle.
            date_of_publication (str): The date of publication for the bundle.
            report_title (str): The report title for the bundle.
            report_doi (str): The report_doi for the bundle.
            place_of_publication (str): The place of publication for the bundle.
            link_to_study_report (str): The link to study for the bundle.
            authors (list of objects): The authors for the bundle.
    """
    request_body = json.loads(request.body)
    fsData = request_body["fsData"]
    id = request_body["id"]  # noqa
    uid = request_body["uid"]
    name = request_body["name"]  # noqa
    studyName = request_body["study_name"]
    acronym = request_body["acronym"]
    abstract = request_body["abstract"]
    institution = request_body["institution"]
    funding_source = request_body["funding_source"]
    contact_person = request_body["contact_person"]
    sector_divisions = request_body["sector_divisions"]
    sectors = request_body["sectors"]
    expanded_sectors = request_body["expanded_sectors"]  # noqa
    # energy_carriers = request_body['energy_carriers']
    # expanded_energy_carriers = request_body['expanded_energy_carriers']
    # energy_transformation_processes = request_body['energy_transformation_processes']
    # expanded_energy_transformation_processes = request_body['expanded_energy_transformation_processes'] # noqa
    technologies = request_body["technologies"]
    study_keywords = request_body["study_keywords"]
    scenarios = request_body["scenarios"]
    models = request_body["models"]
    frameworks = request_body["frameworks"]
    publications = request_body["publications"]

    Duplicate_study_factsheet = False

    for s, p, o in oekg.triples((None, RDF.type, OEO.OEO_00010252)):
        study_acronym = oekg.value(s, DC.acronym)
        if str(clean_name(acronym)) == str(study_acronym) and str(
            clean_name(acronym)
        ) != str(fsData["acronym"]):
            Duplicate_study_factsheet = True

    if Duplicate_study_factsheet == True:  # noqa
        response = JsonResponse(
            "Factsheet exists", safe=False, content_type="application/json"
        )
        patch_response_headers(response, cache_timeout=1)
        return response

    if Duplicate_study_factsheet == False:  # noqa
        study_URI = URIRef("http://openenergy-platform.org/ontology/oekg/" + uid)

        old_bundle = Graph()
        for s, p, o in oekg.triples((study_URI, None, None)):
            old_bundle.add((s, p, o))
        for s, p, o in oekg.triples((study_URI, OEKG["has_scenario"], None)):
            for s1, p1, o1 in oekg.triples((o, None, None)):
                old_bundle.add((s1, p1, o1))

        new_bundle = Graph()
        new_bundle.add((study_URI, RDF.type, OEO.OEO_00010252))

        _publications = json.loads(publications) if publications is not None else []
        for item in _publications:
            publications_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/publication/" + item["id"]
            )
            new_bundle.add(
                (publications_URI, OEKG["publication_uuid"], Literal(item["id"]))
            )
            new_bundle.add((study_URI, OEKG["has_publication"], publications_URI))
            if item["report_title"] != "":
                new_bundle.add(
                    (publications_URI, RDFS.label, Literal(item["report_title"]))
                )

            _authors = item["authors"]
            # Check if list is empty to avoid adding empty elements to the OEKG
            if _authors:
                for author in _authors:
                    # TODO: (@adel please check):
                    # Workaround below to avoid adding empty new
                    # autors every time a new publication is added
                    if author["name"]:
                        author_URI = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/"
                            + author["iri"]
                        )
                        new_bundle.add((publications_URI, OEO.OEO_00000506, author_URI))

            if item["doi"] != "":
                new_bundle.add((publications_URI, OEKG["doi"], Literal(item["doi"])))

            if (
                item["date_of_publication"] != "1900"
                and item["date_of_publication"] != ""
            ):
                new_bundle.add(
                    (
                        publications_URI,
                        OEKG["date_of_publication"],
                        Literal(item["date_of_publication"]),
                    )
                )

            if item["link_to_study_report"] != "":
                new_bundle.add(
                    (
                        publications_URI,
                        OEKG["link_to_study_report"],
                        Literal(item["link_to_study_report"]),
                    )
                )

            new_bundle.add((study_URI, OEKG["has_publication"], publications_URI))

            # remove old date in publication
            # iterate to make sure it can only have unique publication date
            for _s, _p, _o in oekg.triples(
                (publications_URI, OEKG["date_of_publication"], None)
            ):
                oekg.remove((_s, _p, _o))

        _scenarios = json.loads(scenarios) if scenarios is not None else []
        for item in _scenarios:
            if item["acronym"] != "":
                scenario_URI = URIRef(
                    "http://openenergy-platform.org/ontology/oekg/scenario/"
                    + item["id"]
                )

                for s, p, o in oekg.triples((scenario_URI, None, None)):
                    oekg.remove((o, p, o))

                new_bundle.add(
                    (scenario_URI, OEKG["scenario_uuid"], Literal(item["id"]))
                )
                new_bundle.add((scenario_URI, RDF.type, OEO.OEO_00000365))
                new_bundle.add((scenario_URI, RDFS.label, Literal((item["acronym"]))))
                if item["name"] != "":
                    new_bundle.add(
                        (scenario_URI, OEKG["has_full_name"], Literal(remove_non_printable(item["name"])))
                    )
                if item["abstract"] != "" and item["abstract"] != None:  # noqa
                    new_bundle.add(
                        (scenario_URI, DC.abstract, Literal(remove_non_printable(item["abstract"])))
                    )
                if "regions" in item:
                    for region in item["regions"]:
                        region_URI = URIRef(region["iri"])
                        scenario_region = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/region/"
                            + region["iri"].rsplit("/", 1)[1]
                        )
                        new_bundle.add((scenario_region, RDF.type, OEO.OEO_00020032))
                        new_bundle.add(
                            (scenario_region, RDFS.label, Literal(remove_non_printable(region["name"])))
                        )
                        new_bundle.add(
                            (
                                scenario_region,
                                OEKG["reference"],
                                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"])
                        scenario_interacting_region = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/"
                            + interacting_region["iri"]
                        )

                        new_bundle.add(
                            (scenario_interacting_region, RDF.type, OEO.OEO_00020036)
                        )
                        new_bundle.add(
                            (
                                scenario_interacting_region,
                                RDFS.label,
                                Literal(interacting_region["name"]),
                            )
                        )
                        new_bundle.add(
                            (
                                scenario_interacting_region,
                                OEKG["reference"],
                                interacting_region_URI,
                            )
                        )

                        new_bundle.add(
                            (
                                scenario_URI,
                                OEO.OEO_00020222,
                                scenario_interacting_region,
                            )
                        )

                if "scenario_years" in item:
                    for scenario_year in item["scenario_years"]:
                        new_bundle.add(
                            (
                                scenario_URI,
                                OEO.OEO_00020224,
                                Literal(scenario_year["name"]),
                            )
                        )

                if "descriptors" in item:
                    for descriptor in item["descriptors"]:
                        descriptor = URIRef(descriptor["class"])
                        new_bundle.add(
                            (scenario_URI, OEO["has_scenario_descriptor"], descriptor)
                        )

                # TODO: jh-RLI: Update to avoid duplicated table name entries
                if "input_datasets" in item:
                    for input_dataset in item["input_datasets"]:
                        input_dataset_URI = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/input_datasets/"  # noqa
                            + input_dataset["key"]
                        )

                        for s, p, o in oekg.triples((input_dataset_URI, None, None)):
                            oekg.remove((o, p, o))

                        new_bundle.add((input_dataset_URI, RDF.type, OEO.OEO_00030030))
                        new_bundle.add(
                            (
                                input_dataset_URI,
                                RDFS.label,
                                Literal(input_dataset["value"]["label"]),
                            )
                        )
                        new_bundle.add(
                            (
                                input_dataset_URI,
                                OEO["has_iri"],
                                Literal(input_dataset["value"]["url"]),
                            )
                        )
                        new_bundle.add(
                            (
                                input_dataset_URI,
                                OEO["has_id"],
                                Literal(input_dataset["idx"]),
                            )
                        )
                        new_bundle.add(
                            (
                                input_dataset_URI,
                                OEO["has_key"],
                                Literal(input_dataset["key"]),
                            )
                        )
                        new_bundle.add(
                            (scenario_URI, OEO.RO_0002233, input_dataset_URI)
                        )

                # TODO: jh-RLI: Update to avoid duplicated table name entries
                if "output_datasets" in item:
                    for output_dataset in item["output_datasets"]:
                        output_dataset_URI = URIRef(
                            "http://openenergy-platform.org/ontology/oekg/output_datasets/"  # noqa: E501
                            + output_dataset["key"]
                        )
                        new_bundle.add((output_dataset_URI, RDF.type, OEO.OEO_00030029))
                        new_bundle.add(
                            (
                                output_dataset_URI,
                                RDFS.label,
                                Literal(output_dataset["value"]["label"]),
                            )
                        )
                        new_bundle.add(
                            (
                                output_dataset_URI,
                                OEO["has_iri"],
                                Literal(output_dataset["value"]["url"]),
                            )
                        )
                        new_bundle.add(
                            (
                                output_dataset_URI,
                                OEO["has_id"],
                                Literal(output_dataset["idx"]),
                            )
                        )
                        new_bundle.add(
                            (
                                output_dataset_URI,
                                OEO["has_key"],
                                Literal(output_dataset["key"]),
                            )
                        )
                        new_bundle.add(
                            (scenario_URI, OEO.RO_0002234, output_dataset_URI)
                        )

                new_bundle.add((study_URI, OEKG["has_scenario"], scenario_URI))

        if acronym != "":
            new_bundle.add((study_URI, DC.acronym, Literal(remove_non_printable(acronym))))

        new_bundle.add((study_URI, OEKG["has_full_name"], Literal(remove_non_printable(studyName))))

        institutions = json.loads(institution) if institution is not None else []
        for item in institutions:
            institution_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/" + item["iri"]
            )
            new_bundle.add((study_URI, OEO.OEO_00000510, institution_URI))

        funding_sources = (
            json.loads(funding_source) if funding_source is not None else []
        )
        for item in funding_sources:
            funding_source_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/" + item["iri"]
            )
            new_bundle.add((study_URI, OEO.OEO_00000509, funding_source_URI))

        if abstract != "":
            new_bundle.add((study_URI, DC.abstract, Literal(remove_non_printable(abstract))))

        contact_persons = (
            json.loads(contact_person) if contact_person is not None else []
        )
        for item in contact_persons:
            contact_person_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/" + item["iri"]
            )
            new_bundle.add((study_URI, OEO.OEO_00000508, contact_person_URI))

        _sector_divisions = (
            json.loads(sector_divisions) if sector_divisions is not None else []
        )
        for item in _sector_divisions:
            sector_divisions_URI = URIRef(item["class"])
            new_bundle.add(
                (study_URI, OEO["based_on_sector_division"], sector_divisions_URI)
            )

        _sectors = json.loads(sectors) if sectors is not None else []
        for item in _sectors:
            sector_URI = URIRef(item["class"])
            new_bundle.add((study_URI, OEO.OEO_00000505, sector_URI))

        _technologies = json.loads(technologies) if technologies is not None else []
        for item in _technologies:
            technology_URI = URIRef(item["class"])
            new_bundle.add((study_URI, OEO.OEO_00000522, technology_URI))

        _models = json.loads(models) if models is not None else []
        for item in _models:
            model_id = item.get("id")

            if item.get("acronym"):
                model_acronym = item.get("acronym")
            else:
                model_acronym = item.get("name")
            model_url = item.get("url")

            if not model_id or not model_acronym or not model_url:
                continue  # Skip this item if any critical field is empty

            model_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/models/" + str(model_id)
            )
            new_bundle.add((model_URI, RDF.type, OEO.OEO_00000277))

            new_bundle.add(
                (
                    model_URI,
                    RDFS.label,
                    Literal(remove_non_printable(model_acronym)),
                )
            )

            new_bundle.add(
                (
                    model_URI,
                    OEO["has_iri"],
                    Literal(model_url),
                )
            )

            new_bundle.add((study_URI, OEO["has_model"], model_URI))

            # remove old labels
            # iterate to make sure only current selection is available
            for _s, _p, _o in oekg.triples((model_URI, RDFS.label, None)):
                oekg.remove((_s, _p, _o))

            # remove old iri´s
            # iterate to make sure only current selection is available
            for _s, _p, _o in oekg.triples((model_URI, OEO["has_iri,"], None)):
                oekg.remove((_s, _p, _o))

        # TODO: Fix
        _frameworks = json.loads(frameworks) if frameworks is not None else []
        for item in _frameworks:
            framework_id = item.get("id")
            if item.get("acronym"):
                framework_acronym = item.get("acronym")
            else:
                framework_acronym = item.get("name")
            framework_url = item.get("url")

            if not framework_id or not framework_url:
                continue  # Skip this item if any critical field is empty

            framework_URI = URIRef(
                "http://openenergy-platform.org/ontology/oekg/frameworks/"
                + str(framework_id)
            )

            new_bundle.add((framework_URI, RDF.type, OEO.OEO_00000172))
            if framework_acronym:
                new_bundle.add(
                    (
                        framework_URI,
                        RDFS.label,
                        Literal(remove_non_printable(framework_acronym)),
                    )
                )
            if framework_url:
                new_bundle.add(
                    (
                        framework_URI,
                        OEO["has_iri"],
                        Literal(framework_url),
                    )
                )

            new_bundle.add((study_URI, OEO["has_framework"], framework_URI))

            # remove old labels
            # iterate to make sure only current selection is available
            for _s, _p, _o in oekg.triples((framework_URI, RDFS.label, None)):
                oekg.remove((_s, _p, _o))

            # remove old iri´s
            # iterate to make sure only current selection is available
            for _s, _p, _o in oekg.triples((framework_URI, OEO["has_iri,"], None)):
                oekg.remove((_s, _p, _o))

        _study_keywords = (
            json.loads(study_keywords) if study_keywords is not None else []
        )
        for keyword in _study_keywords:
            new_bundle.add((study_URI, OEO["has_study_keyword"], Literal(keyword)))

        iso_old_bundle = to_isomorphic(old_bundle)
        iso_new_bundle = to_isomorphic(new_bundle)

        in_both, in_first, in_second = graph_diff(iso_old_bundle, iso_new_bundle)

        in_first_json = str(in_first.serialize(format="json-ld"))  # noqa
        in_second_json = str(in_second.serialize(format="json-ld"))  # noqa

        # remove old bundle from oekg
        for s, p, o in oekg.triples((study_URI, OEKG["has_scenario"], None)):
            oekg.remove((o, None, None))
        oekg.remove((study_URI, None, None))

        for s, p, o in oekg.triples((study_URI, OEKG["has_scenario"], None)):
            oekg.remove((o, None, None))
        oekg.remove((study_URI, None, None))

        # add updated bundle to oekg
        for s, p, o in new_bundle.triples((None, None, None)):
            oekg.add((s, p, o))

        OEKG_Modifications_instance = OEKG_Modifications(  # noqa
            bundle_id=uid,
            user=login_models.myuser.objects.filter(name=request.user).first(),
            old_state=in_first.serialize(format="json-ld"),
            new_state=in_second.serialize(format="json-ld"),
        )
        # OEKG_Modifications_instance.save()

        response = JsonResponse(
            "factsheet updated!", safe=False, content_type="application/json"
        )
        patch_response_headers(response, cache_timeout=1)
        return response

def factsheet_by_id(request, *args, **kwargs):

Returns a scenario bundle based based on the provided ID.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
id str

The unique ID for the bundle.

required
Source code in factsheet/views.py
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
def factsheet_by_id(request, *args, **kwargs):
    """
    Returns a scenario bundle based based on the provided ID.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        id (str): The unique ID for the bundle.
    """

    uid = request.GET.get("id")
    study_URI = URIRef("http://openenergy-platform.org/ontology/oekg/" + uid)
    factsheet = {}

    acronym = ""
    study_name = ""
    abstract = ""

    for s, p, o in oekg.triples((study_URI, DC.acronym, None)):
        acronym = o

    for s, p, o in oekg.triples((study_URI, OEKG["has_full_name"], None)):
        study_name = o

    for s, p, o in oekg.triples((study_URI, DC.abstract, None)):
        abstract = o

    factsheet["acronym"] = acronym
    factsheet["uid"] = uid
    factsheet["study_name"] = study_name
    factsheet["abstract"] = abstract
    factsheet["publications"] = []

    factsheet["funding_sources"] = []
    for s, p, o in oekg.triples((study_URI, OEO.OEO_00000509, None)):
        label = oekg.value(o, RDFS.label)
        if label != None:  # noqa
            factsheet["funding_sources"].append(
                {"iri": str(o).split("/")[-1], "id": label, "name": label}
            )

    factsheet["institution"] = []
    for s, p, o in oekg.triples((study_URI, OEO.OEO_00000510, None)):
        label = oekg.value(o, RDFS.label)
        if label != None:  # noqa
            factsheet["institution"].append(
                {"iri": str(o).split("/")[-1], "id": label, "name": label}
            )

    factsheet["contact_person"] = []
    for s, p, o in oekg.triples((study_URI, OEO.OEO_00000508, None)):
        label = oekg.value(o, RDFS.label)
        if label != None:  # noqa
            factsheet["contact_person"].append(
                {"iri": str(o).split("/")[-1], "id": label, "name": label}
            )

    factsheet["sector_divisions"] = []
    for s, p, o in oekg.triples((study_URI, OEO["based_on_sector_division"], None)):
        label = oeo.value(o, RDFS.label)
        class_iri = o
        if label != None:  # noqa
            factsheet["sector_divisions"].append(
                {"value": label, "name": label, "class": class_iri}
            )

    factsheet["sectors"] = []
    for s, p, o in oekg.triples((study_URI, OEO.OEO_00000505, None)):
        label = oeo.value(o, RDFS.label)
        class_iri = o
        if label != None:  # noqa
            factsheet["sectors"].append(
                {"value": label, "label": label, "class": class_iri}
            )

    # factsheet['energy_carriers'] = []
    # for s, p, o in oekg.triples(( study_URI, OEO["covers_energy_carrier"], None )):
    #     label = oeo.value(o, RDFS.label)
    #     class_label = oeo.value(o, RDFS.label)
    #     if label != None:
    #         factsheet['energy_carriers'].append({ "value": label, "label":label, "class": o }) # noqa: E501

    # factsheet['energy_transformation_processes'] = []
    # for s, p, o in oekg.triples(( study_URI, OEO["covers_transformation_processes"], None )): # noqa: E501
    #     label = oeo.value(o, RDFS.label)
    #     if label != None:
    #         factsheet['energy_transformation_processes'].append({ "value": label, "label":label, "class": o }) # noqa: E501

    factsheet["technologies"] = []
    for s, p, o in oekg.triples((study_URI, OEO.OEO_00000522, None)):
        label = oeo.value(o, RDFS.label)
        if label != None:  # noqa
            factsheet["technologies"].append(
                {"value": label, "label": label, "class": o}
            )

    factsheet["study_keywords"] = []
    for s, p, o in oekg.triples((study_URI, OEO["has_study_keyword"], None)):
        if o != None:  # noqa
            factsheet["study_keywords"].append(o)

    factsheet["models"] = []
    factsheet["frameworks"] = []

    for _, _, o in oekg.triples((study_URI, OEO["has_framework"], None)):
        for _, _, o1 in oekg.triples((o, OEO["has_iri"], None)):
            framework_id = int(str(o).split("/")[-1])
            framework_metadata = get_framework_metadata_by_id(
                framework_id, "energyframework"
            )

            if framework_metadata:
                framework_metadata["url"] = str(o1)
                factsheet["frameworks"].append(framework_metadata)

    for _, _, o in oekg.triples((study_URI, OEO["has_model"], None)):
        for _, _, o1 in oekg.triples((o, OEO["has_iri"], None)):
            model_id = int(str(o).split("/")[-1])
            model_metadata = get_model_metadata_by_id(model_id, "energymodel")

            if model_metadata:
                model_metadata["url"] = str(o1)
                factsheet["models"].append(model_metadata)

    temp = set()
    factsheet["publications"] = []
    for s, p, o in oekg.triples((study_URI, OEKG["has_publication"], None)):
        publication = {}

        publication["report_title"] = ""
        label = oekg.value(o, RDFS.label)
        if label is not None:
            publication["report_title"] = label

        publication_uuid = oekg.value(o, OEKG["publication_uuid"])
        publication["id"] = publication_uuid

        publication["authors"] = []
        for s1, p1, o1 in oekg.triples((o, OEO.OEO_00000506, None)):
            o1_label = oekg.value(o1, RDFS.label)
            publication["authors"].append({"iri": o1, "name": o1_label})

        publication["doi"] = ""
        for s2, p2, o2 in oekg.triples((o, OEKG["doi"], None)):
            publication["doi"] = o2

        publication["date_of_publication"] = ""
        for s3, p3, o3 in oekg.triples((o, OEKG["date_of_publication"], None)):
            publication["date_of_publication"] = serialize_publication_date(str(o3))
            temp.update(serialize_publication_date(str(o3)))

        # Convert set to list before creating the JSON response
        factsheet["collected_scenario_publication_dates"] = list(temp)

        publication["link_to_study_report"] = ""
        for s4, p4, o4 in oekg.triples((o, OEKG["link_to_study_report"], None)):
            publication["link_to_study_report"] = o4

        factsheet["publications"].append(publication)

    factsheet["scenarios"] = []
    for s, p, o in oekg.triples((study_URI, OEKG["has_scenario"], None)):
        scenario = {}
        label = oekg.value(o, RDFS.label)
        scenario_uuid = oekg.value(o, OEKG["scenario_uuid"])

        if label != None:  # noqa
            scenario["acronym"] = label

        scenario["id"] = scenario_uuid
        scenario["scenario_years"] = []
        scenario["regions"] = []
        scenario["interacting_regions"] = []
        scenario["descriptors"] = []
        scenario["input_datasets"] = []
        scenario["output_datasets"] = []

        abstract = oekg.value(o, DC.abstract)
        name = oekg.value(o, OEKG["has_full_name"])
        scenario["name"] = name
        scenario["abstract"] = abstract

        for s1, p1, o1 in oekg.triples((o, OEO.OEO_00020220, None)):
            o1_type = oekg.value(o1, RDF.type)
            o1_label = oekg.value(o1, RDFS.label)
            scenario["regions"].append({"iri": o1, "name": o1_label})

        for s1, p1, o1 in oekg.triples((o, OEO.OEO_00020222, None)):
            o1_type = oekg.value(o1, RDF.type)  # noqa
            o1_label = oekg.value(o1, RDFS.label)
            scenario["interacting_regions"].append(
                {"iri": str(o1).split("/")[-1], "id": o1_label, "name": o1_label}
            )

        for s11, p11, o11 in oekg.triples((o, OEO["has_scenario_descriptor"], None)):
            label = oeo.value(o11, RDFS.label)
            scenario["descriptors"].append(
                {"value": label, "label": label, "class": o11}
            )

        for s2, p2, o2 in oekg.triples((o, OEO.RO_0002233, None)):
            o2_iri = oekg.value(o2, OEO["has_iri"])
            o2_label = oekg.value(o2, RDFS.label)
            o2_key = oekg.value(o2, OEO["has_key"])
            o2_idx = oekg.value(o2, OEO["has_id"])

            scenario["input_datasets"].append(
                {
                    "key": o2_key,
                    "idx": o2_idx,
                    "value": {"label": o2_label, "url": o2_iri},
                }
            )

        for s3, p3, o3 in oekg.triples((o, OEO.RO_0002234, None)):
            o3_iri = oekg.value(o3, OEO["has_iri"])
            o3_label = oekg.value(o3, RDFS.label)
            o3_key = oekg.value(o3, OEO["has_key"])
            o3_idx = oekg.value(o3, OEO["has_id"])

            scenario["output_datasets"].append(
                {
                    "key": o3_key,
                    "idx": o3_idx,
                    "value": {"label": o3_label, "url": o3_iri},
                }
            )

        for s4, p4, o4 in oekg.triples((o, OEO.OEO_00020224, None)):
            scenario["scenario_years"].append({"id": o4, "name": o4})

        factsheet["scenarios"].append(scenario)

    response = JsonResponse(factsheet, safe=False, content_type="application/json")
    patch_response_headers(response, cache_timeout=1)

    scenario_region = URIRef(
        "http://openenergy-platform.org/ontology/oekg/region/Germany"
    )
    for s, p, o in oekg.triples((scenario_region, RDFS.label, None)):
        if str(o) == "None":
            oekg.remove((s, p, o))
            oekg.add((s, p, Literal("Germany")))

    return response

def delete_factsheet_by_id(request, *args, **kwargs):

Removes a scenario bundle based on the provided ID.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
id str

The unique ID for the bundle.

required
Source code in factsheet/views.py
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
@only_if_user_is_owner_of_scenario_bundle
@login_required
def delete_factsheet_by_id(request, *args, **kwargs):
    """
    Removes a scenario bundle based on the provided ID.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        id (str): The unique ID for the bundle.

    """
    id = request.GET.get("id")
    study_URI = URIRef("http://openenergy-platform.org/ontology/oekg/" + id)

    for s, p, o in oekg.triples((study_URI, OEKG["has_scenario"], None)):
        oekg.remove((o, None, None))
    oekg.remove((study_URI, None, None))

    response = JsonResponse(
        "factsheet removed!", safe=False, content_type="application/json"
    )
    patch_response_headers(response, cache_timeout=1)
    return response

def add_entities(request, *args, **kwargs):

Add entities to OEKG. The minimum requirements for adding an entity are the type and label.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
entity_type str

The type(OEO class) of the entity.

required
entity_label str

The label of the entity.

required
entity_iri str

The IRI of the entity.

required
Source code in factsheet/views.py
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
@login_required
def add_entities(request, *args, **kwargs):
    """
    Add entities to OEKG. The minimum requirements for
    adding an entity are the type and label.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        entity_type (str): The type(OEO class) of the entity.
        entity_label (str): The label of the entity.
        entity_iri (str): The IRI of the entity.
    """
    request_body = json.loads(request.body)
    entity_type = request_body["entity_type"]
    entity_label = request_body["entity_label"]
    entity_iri = request_body["entity_iri"]

    vocab = entity_type.split(".")[0]
    classId = entity_type.split(".")[1]
    prefix = ""
    if vocab == "OEO":
        prefix = "http://openenergy-platform.org/ontology/oeo/"
    if vocab == "OBO":
        prefix = "http://purl.obolibrary.org/obo/"

    entity_type_URI = URIRef(prefix + classId)

    entity_URI = URIRef("http://openenergy-platform.org/ontology/oekg/" + entity_iri)

    oekg.add((entity_URI, RDF.type, entity_type_URI))
    oekg.add((entity_URI, RDFS.label, Literal(entity_label)))

    response = JsonResponse(
        "A new entity added!", safe=False, content_type="application/json"
    )
    patch_response_headers(response, cache_timeout=1)
    return response

def delete_entities(request, *args, **kwargs):

Removes an entity from OEKG. The minimum requirements for removing an entity are the type and label.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
entity_type str

The type(OEO class) of the entity.

required
entity_label str

The label of the entity.

required
Source code in factsheet/views.py
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
@login_required
def delete_entities(request, *args, **kwargs):
    """
    Removes an entity from OEKG. The minimum requirements for
    removing an entity are the type and label.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        entity_type (str): The type(OEO class) of the entity.
        entity_label (str): The label of the entity.
    """
    entity_type = request.GET.get("entity_type")
    entity_label = request.GET.get("entity_label")

    entity_URI = URIRef(  # noqa
        "http://openenergy-platform.org/ontology/oekg/" + entity_type
    )
    entity_Label = URIRef(
        "http://openenergy-platform.org/ontology/oekg/" + (entity_label)
    )

    oekg.remove((entity_Label, None, None))
    oekg.remove((None, None, entity_Label))
    response = JsonResponse(
        "entity removed!", safe=False, content_type="application/json"
    )
    patch_response_headers(response, cache_timeout=1)
    return response

def update_an_entity(request, *args, **kwargs):

Updates an entity in OEKG. The minimum requirements for updating an entity are the type, the old label, and the new label.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
entity_type str

The type(OEO class) of the entity.

required
entity_label str

The label of the entity.

required
new_entity_label str

The new label of the entity.

required
entity_id str

The IRI of the entity.

required
Source code in factsheet/views.py
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
@login_required
def update_an_entity(request, *args, **kwargs):
    """
    Updates an entity in OEKG. The minimum requirements for
    updating an entity are the type, the old label, and the
    new label.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        entity_type (str): The type(OEO class) of the entity.
        entity_label (str): The label of the entity.
        new_entity_label (str): The new label of the entity.
        entity_id (str): The IRI of the entity.
    """
    request_body = json.loads(request.body)
    entity_type = request_body["entity_type"]
    entity_label = request_body["entity_label"]
    new_entity_label = request_body["new_entity_label"]
    entity_id = request_body["entity_iri"]

    vocab = entity_type.split(".")[0]
    classId = entity_type.split(".")[1]
    prefix = ""
    if vocab == "OEO":
        prefix = "http://openenergy-platform.org/ontology/oeo/"
    if vocab == "OBO":
        prefix = "http://purl.obolibrary.org/obo/"

    entity_type_URI = URIRef(prefix + classId)  # noqa
    entity_IRI = URIRef("http://openenergy-platform.org/ontology/oekg/" + (entity_id))

    oekg.add((entity_IRI, RDFS.label, Literal(new_entity_label)))
    oekg.remove((entity_IRI, RDFS.label, Literal(entity_label)))

    response = JsonResponse(
        "entity updated!", safe=False, content_type="application/json"
    )
    patch_response_headers(response, cache_timeout=1)
    return response

def query_oekg(request, *args, **kwargs):

This function takes filter objects provided by the user and utilises them to construct a SPARQL query.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
criteria str

An object that contains institutions, authors,

required
Source code in factsheet/views.py
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
def query_oekg(request, *args, **kwargs):
    """
    This function takes filter objects provided by the user and utilises
    them to construct a SPARQL query.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        criteria (str): An object that contains institutions, authors,
        funding sources, start date of the publications, end date of publications
        study descriptors, and a range for scenario years. All of these fields
        are utilised to construct a SPARQL query for execution on the OEKG.

    """
    request_body = json.loads(request.body)
    criteria = request_body["criteria"]

    institutes_list = criteria["institutions"]
    authors_list = criteria["authors"]
    funding_sources_list = criteria["fundingSource"]
    publication_date_start_value = criteria["startDateOfPublication"]
    publication_date_end_value = criteria["endDateOfPublication"]
    study_keywords_list = criteria["studyKewords"]
    scenario_year_start_value = criteria["scenarioYearValue"][0]
    scenario_year_end_value = criteria["scenarioYearValue"][1]

    query_structure = """
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX RDFS: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX OEO: <http://openenergy-platform.org/ontology/oeo/>
        PREFIX OEKG: <http://openenergy-platform.org/ontology/oekg/>
        PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
        PREFIX DC: <http://purl.org/dc/terms/>

        SELECT DISTINCT ?study_acronym
        WHERE
        {{
        ?s  DC:acronym ?study_acronym .

        {funding_sources_exp}
        {study_descriptors_exp}
        {institutes_exp}

        ?s OEKG:has_publication ?publication .
        ?publication OEKG:date_of_publication ?publication_date .

        {authors_exp}

        FILTER ((?institutes IN ({institutes}) )
        || (?authors IN ({authors}) )
        || (?funding_sources IN ({funding_sources}) )
        || (?publication_date >= "{publication_date_start}" && ?publication_date <= "{publication_date_end}")
        || (?study_keywords IN ({study_keywords}) ) )

        }}"""  # noqa: E501

    authors_exp = '?publication OEO:OEO_00000506 ?authors .' if authors_list != [] else ''
    funding_sources_exp = '?s OEO:OEO_00000509 ?funding_sources .' if funding_sources_list != [] else ''
    study_descriptors_exp = '?s OEO:has_study_keyword ?study_keywords .' if study_keywords_list != [] else ''
    institutes_exp = '?s OEO:OEO_00000510 ?institutes .' if institutes_list != [] else ''

    final_query = query_structure.format(
        institutes=str(institutes_list)
        .replace("[", "")
        .replace("]", "")
        .replace("'", ""),
        authors=str(authors_list).replace("[", "").replace("]", "").replace("'", ""),
        funding_sources=str(funding_sources_list)
        .replace("[", "")
        .replace("]", "")
        .replace("'", ""),
        publication_date_start=publication_date_start_value,
        publication_date_end=publication_date_end_value,
        study_keywords=str(study_keywords_list).replace("[", "").replace("]", ""),
        scenario_year_start=scenario_year_start_value,
        scenario_year_end=scenario_year_end_value,
        funding_sources_exp= funding_sources_exp,
        study_descriptors_exp=study_descriptors_exp,
        institutes_exp=institutes_exp,
        authors_exp=authors_exp,
    )

    print(final_query)
    sparql.setReturnFormat(JSON)
    sparql.setQuery(final_query)
    results = sparql.query().convert()

    response = JsonResponse(
        results["results"]["bindings"],
        safe=False,
        content_type="application/json",
    )
    return response

def get_entities_by_type(request, *args, **kwargs):

Returns all entities (from OEKG) with a certain type. The type should be supplied by the user.

Parameters:

Name Type Description Default
request HttpRequest

The incoming HTTP GET request.

required
entity_type str

The type(OEO class) of the entity.

required
Source code in factsheet/views.py
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
def get_entities_by_type(request, *args, **kwargs):
    """
    Returns all entities (from OEKG) with a certain type.
    The type should be supplied by the user.

    Args:
        request (HttpRequest): The incoming HTTP GET request.
        entity_type (str): The type(OEO class) of the entity.
    """
    entity_type = request.GET.get("entity_type")
    vocab = entity_type.split(".")[0]
    classId = entity_type.split(".")[1]
    prefix = ""
    if vocab == "OEO":
        prefix = "http://openenergy-platform.org/ontology/oeo/"
    if vocab == "OBO":
        prefix = "http://purl.obolibrary.org/obo/"

    entity_URI = URIRef(prefix + classId)

    entities = []
    for s, p, o in oekg.triples((None, RDF.type, entity_URI)):
        sl = oekg.value(s, RDFS.label)
        entities.append({"name": sl, "id": sl, "iri": str(s).split("/")[-1]})

    response = JsonResponse(entities, safe=False, content_type="application/json")
    patch_response_headers(response, cache_timeout=1)
    return response