Skip to content

Commit

Permalink
Merge pull request open-plan-tool#266 from open-plan-tool/fix/project…
Browse files Browse the repository at this point in the history
…-sharing-edit-rights

Enable saving a shared project with edit rights
  • Loading branch information
Bachibouzouk authored Apr 11, 2024
2 parents e6399a4 + ad70099 commit d5375ea
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
99 changes: 59 additions & 40 deletions app/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,18 +892,24 @@ def scenario_create_topology(request, proj_id, scen_id, step_id=2, max_step=3):

# TODO: if the scenario exists, load it, otherwise default form

scenario = get_object_or_404(Scenario, pk=scen_id)

if (scenario.project.user != request.user) and (
scenario.project.viewers.filter(
user__email=request.user.email, share_rights="edit"
).exists()
is False
):
user_has_right_to_save = False
# raise PermissionDenied
else:
user_has_right_to_save = True

if request.method == "POST":
# called by function save_topology() in templates/scenario/scenario_step2.html

scenario = get_object_or_404(Scenario, pk=scen_id)
if (scenario.project.user != request.user) and (
scenario.project.viewers.filter(
user__email=request.user.email, share_rights="edit"
).exists()
is False
):
if user_has_right_to_save is False:
return JsonResponse({"success": True}, status=403)
# raise PermissionDenied

topologies = json.loads(request.body)
node_list = [NodeObject(topology) for topology in topologies]
Expand Down Expand Up @@ -936,6 +942,7 @@ def scenario_create_topology(request, proj_id, scen_id, step_id=2, max_step=3):
"proj_id": scenario.project.id,
"proj_name": scenario.project.name,
"topology_data_list": json.dumps(topology_data_list),
"user_has_right_to_save": user_has_right_to_save,
"step_id": step_id,
"step_list": STEP_LIST,
"max_step": max_step,
Expand Down Expand Up @@ -977,6 +984,16 @@ def scenario_create_constraints(request, proj_id, scen_id, step_id=3, max_step=4
):
raise PermissionDenied

if (scenario.project.user != request.user) and (
scenario.project.viewers.filter(
user__email=request.user.email, share_rights="edit"
).exists()
is False
):
user_has_right_to_save = False
else:
user_has_right_to_save = True

qs_sim = Simulation.objects.filter(scenario=scenario)

if request.method == "GET":
Expand Down Expand Up @@ -1008,6 +1025,7 @@ def scenario_create_constraints(request, proj_id, scen_id, step_id=3, max_step=4
"scen_id": scen_id,
"proj_id": scenario.project.id,
"proj_name": scenario.project.name,
"user_has_right_to_save": user_has_right_to_save,
"step_id": step_id,
"step_list": STEP_LIST,
"max_step": max_step,
Expand All @@ -1016,38 +1034,39 @@ def scenario_create_constraints(request, proj_id, scen_id, step_id=3, max_step=4
},
)
elif request.method == "POST":
for constraint_type, form_model in constraints_forms.items():
form = form_model(request.POST, prefix=constraint_type)

if form.is_valid():
# check whether the constraint is already associated to the scenario
qs = constraints_models[constraint_type].objects.filter(
scenario=scenario
)
if qs.exists():
if len(qs) == 1:
constraint_instance = qs[0]
for name, value in form.cleaned_data.items():
if name != "help_text":
if getattr(constraint_instance, name) != value:
setattr(constraint_instance, name, value)
if qs_sim.exists():
qs_sim.update(status=MODIFIED)

else:
constraint_instance = form.save(commit=False)
constraint_instance.scenario = scenario

if constraint_type == "net_zero_energy":
constraint_instance.value = constraint_instance.activated

if (scenario.project.user == request.user) or (
scenario.project.viewers.filter(
user__email=request.user.email, share_rights="edit"
).exists()
is True
):
constraint_instance.save()
if user_has_right_to_save is True:
for constraint_type, form_model in constraints_forms.items():
form = form_model(request.POST, prefix=constraint_type)

if form.is_valid():
# check whether the constraint is already associated to the scenario
qs = constraints_models[constraint_type].objects.filter(
scenario=scenario
)
if qs.exists():
if len(qs) == 1:
constraint_instance = qs[0]
for name, value in form.cleaned_data.items():
if name != "help_text":
if getattr(constraint_instance, name) != value:
setattr(constraint_instance, name, value)
if qs_sim.exists():
qs_sim.update(status=MODIFIED)

else:
constraint_instance = form.save(commit=False)
constraint_instance.scenario = scenario

if constraint_type == "net_zero_energy":
constraint_instance.value = constraint_instance.activated

if (scenario.project.user == request.user) or (
scenario.project.viewers.filter(
user__email=request.user.email, share_rights="edit"
).exists()
is True
):
constraint_instance.save()

return HttpResponseRedirect(reverse("scenario_review", args=[proj_id, scen_id]))

Expand Down
4 changes: 2 additions & 2 deletions app/templates/scenario/scenario_step2.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h5 class="modal-title" id="guiModalLabel">New message</h5>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% translate "Close" %}</button>
{% if scenario.project.user == request.user %}
{% if user_has_right_to_save %}
<button class="btn btn--medium" onclick="submitForm(event)">{% translate "Save" %}</button>
{% endif %}

Expand Down Expand Up @@ -278,7 +278,7 @@ <h3>{{ group_names|get_item:group_name|title }}</h3>
<div class="step-footer__left"></div>
<div class="step-footer__center">
<a class="btn btn--medium btn--hollow btn--previous" href="{% url 'scenario_steps_edit' proj_id scen_id step_id|add:'-1' %}" aria-disabled="true">{% translate "Previous" %}</a>
{% if scenario.project.user == request.user %}
{% if user_has_right_to_save %}
<button class="btn btn--medium btn--transparent" onclick="javascript:save_topology()">{% translate "Save" %}</button>
<button onclick="javascript:save_topology(move_step=true)" id="next_btn" class="btn btn--medium" >{% translate "Next" %} </button>
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/scenario/scenario_step3.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<div class="step-footer__center">
<a class="btn btn--medium btn--hollow btn--previous" href="{% url 'scenario_steps_edit' proj_id scen_id step_id|add:'-1' %}" aria-disabled="true">{% translate "Previous" %}</a>

{% if scenario.project.user == request.user %}
{% if user_has_right_to_save %}
<button onclick="next_btn_clicked()" id="next_btn" class="btn btn--medium" >{% translate "Next" %} </button>

{% else %}
Expand Down

0 comments on commit d5375ea

Please sign in to comment.