diff --git a/epdb/models.py b/epdb/models.py index 6c6ba94c..88ed9855 100644 --- a/epdb/models.py +++ b/epdb/models.py @@ -534,6 +534,16 @@ class AliasMixin(models.Model): class ScenarioMixin(models.Model): scenarios = models.ManyToManyField("epdb.Scenario", verbose_name='Attached Scenarios') + @transaction.atomic + def set_scenarios(self, scenarios: List['Scenario']): + self.scenarios.clear() + self.save() + + for s in scenarios: + self.scenarios.add(s) + + self.save() + class Meta: abstract = True diff --git a/epdb/views.py b/epdb/views.py index 290555ee..ab9d4b7d 100644 --- a/epdb/views.py +++ b/epdb/views.py @@ -199,6 +199,15 @@ def breadcrumbs(first_level_object=None, second_level_namespace=None, second_lev return bread +def set_scenarios(current_user, attach_object, scenario_urls: List[str]): + scens = [] + for scenario_url in scenario_urls: + package = PackageManager.get_package_by_url(current_user, scenario_url) + scen = Scenario.objects.get(package=package, uuid=scenario_url.split('/')[-1]) + scens.append(scen) + + attach_object.set_scenarios(scens) + def index(request): context = get_base_context(request) context['title'] = 'enviPath - Home' @@ -691,7 +700,8 @@ def package_model(request, package_uuid, model_uuid): context['breadcrumbs'] = breadcrumbs(current_package, 'model', current_model) context['model'] = current_model - + context['current_object'] = current_model + return render(request, 'objects/model.html', context) elif request.method == 'POST': @@ -881,6 +891,7 @@ def package_compound(request, package_uuid, compound_uuid): context['breadcrumbs'] = breadcrumbs(current_package, 'compound', current_compound) context['compound'] = current_compound + context['current_object'] = current_compound return render(request, 'objects/compound.html', context) @@ -892,6 +903,12 @@ def package_compound(request, package_uuid, compound_uuid): else: return HttpResponseBadRequest() + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_compound, selected_scenarios) + return redirect(current_compound.url) + new_compound_name = request.POST.get('compound-name') new_compound_description = request.POST.get('compound-description') @@ -965,9 +982,19 @@ def package_compound_structure(request, package_uuid, compound_uuid, structure_u context['object_type'] = 'compound' context['compound_structure'] = current_structure + context['current_object'] = current_structure return render(request, 'objects/compound_structure.html', context) + elif request.method == 'POST': + + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_structure, selected_scenarios) + return redirect(current_structure.url) + + return HttpResponseBadRequest() else: return HttpResponseNotAllowed(['GET', ]) @@ -1073,6 +1100,8 @@ def package_rule(request, package_uuid, rule_uuid): context['breadcrumbs'] = breadcrumbs(current_package, 'rule', current_rule) context['rule'] = current_rule + context['current_object'] = current_rule + if isinstance(current_rule, SimpleAmbitRule): return render(request, 'objects/simple_rule.html', context) else: # isinstance(current_rule, ParallelRule) or isinstance(current_rule, SequentialRule): @@ -1086,6 +1115,12 @@ def package_rule(request, package_uuid, rule_uuid): else: return HttpResponseBadRequest() + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_rule, selected_scenarios) + return redirect(current_rule.url) + rule_name = request.POST.get('rule-name', '').strip() rule_description = request.POST.get('rule-description', '').strip() @@ -1171,6 +1206,7 @@ def package_reaction(request, package_uuid, reaction_uuid): context['breadcrumbs'] = breadcrumbs(current_package, 'reaction', current_reaction) context['reaction'] = current_reaction + context['current_object'] = current_reaction return render(request, 'objects/reaction.html', context) @@ -1182,6 +1218,12 @@ def package_reaction(request, package_uuid, reaction_uuid): else: return HttpResponseBadRequest() + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_reaction, selected_scenarios) + return redirect(current_reaction.url) + new_reaction_name = request.POST.get('reaction-name') new_reaction_description = request.POST.get('reaction-description') @@ -1314,6 +1356,7 @@ def package_pathway(request, package_uuid, pathway_uuid): context['breadcrumbs'] = breadcrumbs(current_package, 'pathway', current_pathway) context['pathway'] = current_pathway + context['current_object'] = current_pathway context['breadcrumbs'] = [ {'Home': s.SERVER_URL}, @@ -1334,6 +1377,12 @@ def package_pathway(request, package_uuid, pathway_uuid): else: return HttpResponseBadRequest() + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_pathway, selected_scenarios) + return redirect(current_pathway.url) + pathway_name = request.POST.get('pathway-name') pathway_description = request.POST.get('pathway-description') @@ -1454,6 +1503,8 @@ def package_pathway_node(request, package_uuid, pathway_uuid, node_uuid): ] context['node'] = current_node + context['current_object'] = current_node + context['app_domain_assessment_data'] = json.dumps(current_node.get_app_domain_assessment_data()) return render(request, 'objects/node.html', context) @@ -1471,8 +1522,14 @@ def package_pathway_node(request, package_uuid, pathway_uuid, node_uuid): return redirect(current_pathway.url) else: return HttpResponseBadRequest() - else: - return HttpResponseBadRequest() + + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_node, selected_scenarios) + return redirect(current_node.url) + + return HttpResponseBadRequest() else: return HttpResponseNotAllowed(['GET', 'POST']) @@ -1562,6 +1619,7 @@ def package_pathway_edge(request, package_uuid, pathway_uuid, edge_uuid): context['object_type'] = 'reaction' context['breadcrumbs'] = breadcrumbs(current_package, 'pathway', current_pathway, 'edge', current_edge) context['edge'] = current_edge + context['current_object'] = current_edge return render(request, 'objects/edge.html', context) @@ -1574,6 +1632,14 @@ def package_pathway_edge(request, package_uuid, pathway_uuid, edge_uuid): current_edge.delete() return redirect(current_pathway.url) + selected_scenarios = request.POST.getlist('selected-scenarios') + + if selected_scenarios: + set_scenarios(current_user, current_edge, selected_scenarios) + return redirect(current_edge.url) + + return HttpResponseBadRequest() + else: return HttpResponseNotAllowed(['GET', 'POST']) @@ -1584,6 +1650,12 @@ def package_scenarios(request, package_uuid): current_package = PackageManager.get_package_by_id(current_user, package_uuid) if request.method == 'GET': + + if 'application/json' in request.META.get('HTTP_ACCEPT'): #request.headers.get('Accept') == 'application/json': + scens = Scenario.objects.filter(package=current_package).order_by('name') + res = [{'name': s.name, 'url': s.url, 'uuid': s.uuid} for s in scens] + return JsonResponse(res, safe=False) + context = get_base_context(request) context['title'] = f'enviPath - {current_package.name} - Scenarios' diff --git a/templates/actions/objects/compound.html b/templates/actions/objects/compound.html index 41bbcba2..77cfb4cd 100644 --- a/templates/actions/objects/compound.html +++ b/templates/actions/objects/compound.html @@ -7,6 +7,10 @@ Add Structure +
  • + + Set Scenarios +
  • Delete Compound diff --git a/templates/actions/objects/compound_structure.html b/templates/actions/objects/compound_structure.html index be8bd9a8..9ca6bbe1 100644 --- a/templates/actions/objects/compound_structure.html +++ b/templates/actions/objects/compound_structure.html @@ -3,6 +3,10 @@ Edit Compound Structure
  • +
  • + + Set Scenarios +
  • Delete Compound Structure diff --git a/templates/actions/objects/node.html b/templates/actions/objects/node.html index 77352594..ba0481b8 100644 --- a/templates/actions/objects/node.html +++ b/templates/actions/objects/node.html @@ -3,6 +3,10 @@ Edit Node
  • +
  • + + Set Scenarios +
  • Delete Node diff --git a/templates/actions/objects/pathway.html b/templates/actions/objects/pathway.html index b1083fcf..4b1f92ad 100644 --- a/templates/actions/objects/pathway.html +++ b/templates/actions/objects/pathway.html @@ -17,6 +17,10 @@ Edit Pathway
  • +
  • + + Set Scenarios +
  • {#
  • #} {# #} {# Calculate Compound Properties#} diff --git a/templates/actions/objects/reaction.html b/templates/actions/objects/reaction.html index 413ae1ba..fb72b608 100644 --- a/templates/actions/objects/reaction.html +++ b/templates/actions/objects/reaction.html @@ -3,6 +3,10 @@ Edit Reaction
  • +
  • + + Set Scenarios +
  • Delete Reaction diff --git a/templates/actions/objects/rule.html b/templates/actions/objects/rule.html index be1ec35c..69d4a780 100644 --- a/templates/actions/objects/rule.html +++ b/templates/actions/objects/rule.html @@ -3,4 +3,12 @@ Edit Rule
  • +
  • + + Set Scenarios +
  • +
  • + + Delete Rule +
  • {% endif %} \ No newline at end of file diff --git a/templates/modals/objects/generic_set_scenario_modal.html b/templates/modals/objects/generic_set_scenario_modal.html new file mode 100644 index 00000000..d21e15a4 --- /dev/null +++ b/templates/modals/objects/generic_set_scenario_modal.html @@ -0,0 +1,72 @@ +{% load static %} + + diff --git a/templates/objects/composite_rule.html b/templates/objects/composite_rule.html index f30227cb..5a6de778 100644 --- a/templates/objects/composite_rule.html +++ b/templates/objects/composite_rule.html @@ -4,6 +4,7 @@ {% block action_modals %} {% include "modals/objects/edit_rule_modal.html" %} +{% include "modals/objects/generic_set_scenario_modal.html" %} {% endblock action_modals %}
    @@ -49,7 +50,22 @@
    - + + {% if rule.scenarios.all %} +
    +

    + Scenarios +

    +
    +
    +
    + {% for s in rule.scenarios.all %} + {{ s.name }} ({{ s.package.name }}) + {% endfor %} +
    +
    + {% endif %}
    diff --git a/templates/objects/compound.html b/templates/objects/compound.html index a4d16c06..d7565e4d 100644 --- a/templates/objects/compound.html +++ b/templates/objects/compound.html @@ -5,6 +5,7 @@ {% block action_modals %} {% include "modals/objects/edit_compound_modal.html" %} {% include "modals/objects/add_structure_modal.html" %} + {% include "modals/objects/generic_set_scenario_modal.html" %} {% include "modals/objects/delete_compound_modal.html" %} {% endblock action_modals %} @@ -135,6 +136,23 @@
    {% endif %} + + {% if compound.scenarios.all %} +
    +

    + Scenarios +

    +
    +
    +
    + {% for s in compound.scenarios.all %} + {{ s.name }} ({{ s.package.name }}) + {% endfor %} +
    +
    + {% endif %} + {% if compound.get_external_identifiers %}
    diff --git a/templates/objects/compound_structure.html b/templates/objects/compound_structure.html index 58e3f7ae..bbf050a0 100644 --- a/templates/objects/compound_structure.html +++ b/templates/objects/compound_structure.html @@ -4,6 +4,7 @@ {% block action_modals %} {% include "modals/objects/edit_compound_structure_modal.html" %} +{% include "modals/objects/generic_set_scenario_modal.html" %} {% endblock action_modals %}
    @@ -54,6 +55,22 @@
    + {% if compound_structure.scenarios.all %} +
    +

    + Scenarios +

    +
    +
    +
    + {% for s in compound_structure.scenarios.all %} + {{ s.name }} ({{ s.package.name }}) + {% endfor %} +
    +
    + {% endif %} + diff --git a/templates/objects/edge.html b/templates/objects/edge.html index 9a01ba61..cb1db12d 100644 --- a/templates/objects/edge.html +++ b/templates/objects/edge.html @@ -5,6 +5,7 @@ {% block action_modals %} {# {% include "modals/objects/edit_edge_modal.html" %}#} {# {% include "modals/objects/delete_edge_modal.html" %}#} + {% include "modals/objects/generic_set_scenario_modal.html" %} {% endblock action_modals %}
    @@ -103,6 +104,21 @@
    {% endif %} + {% if edge.scenarios.all %} +
    +

    + Scenarios +

    +
    +
    +
    + {% for s in edge.scenarios.all %} + {{ s.name }} ({{ s.package.name }}) + {% endfor %} +
    +
    + {% endif %} {% endblock content %} diff --git a/templates/objects/node.html b/templates/objects/node.html index a47a9ff2..0e305074 100644 --- a/templates/objects/node.html +++ b/templates/objects/node.html @@ -4,6 +4,7 @@ {% block action_modals %} {% include "modals/objects/edit_node_modal.html" %} + {% include "modals/objects/generic_set_scenario_modal.html" %} {% include "modals/objects/delete_node_modal.html" %} {% endblock action_modals %} @@ -69,6 +70,22 @@ + {% if node.scenarios.all %} +
    +

    + Scenarios +

    +
    +
    +
    + {% for s in node.scenarios.all %} + {{ s.name }} ({{ s.package.name }}) + {% endfor %} +
    +
    + {% endif %} + {% if app_domain_assessment_data %}