Skip to content

Commit

Permalink
prohibit flavor and environment editing for regular users
Browse files Browse the repository at this point in the history
  • Loading branch information
akochari committed Nov 26, 2024
1 parent 0618247 commit d228984
Showing 1 changed file with 53 additions and 46 deletions.
99 changes: 53 additions & 46 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,26 @@ def can_model_instance_be_deleted(field_name, instance):
@login_required
@permission_required_or_403("can_view_project", (Project, "slug", "project_slug"))
def create_environment(request, project_slug):
# TODO: Ensure that user is allowed to create environment in this project.
if request.method == "POST":
project = Project.objects.get(slug=project_slug)
name = request.POST.get("environment_name")
repo = request.POST.get("environment_repository")
image = request.POST.get("environment_image")
app_pk = request.POST.get("environment_app")
app = Apps.objects.get(pk=app_pk)
environment = Environment(
name=name,
slug=name,
project=project,
repository=repo,
image=image,
app=app,
)
environment.save()
project = Project.objects.get(slug=project_slug)
if not request.user.is_superuser:
return HttpResponseForbidden()
else:
if request.method == "POST":
# TODO: check input data
name = request.POST.get("environment_name")
repo = request.POST.get("environment_repository")
image = request.POST.get("environment_image")
app_pk = request.POST.get("environment_app")
app = Apps.objects.get(pk=app_pk)
environment = Environment(
name=name,
slug=name,
project=project,
repository=repo,
image=image,
app=app,
)
environment.save()
return HttpResponseRedirect(
reverse(
"projects:settings",
Expand All @@ -230,23 +233,24 @@ def create_environment(request, project_slug):
@login_required
@permission_required_or_403("can_view_project", (Project, "slug", "project_slug"))
def delete_environment(request, project_slug):
if request.method == "POST":
project = Project.objects.get(slug=project_slug)
pk = request.POST.get("environment_pk")
# TODO: Check that the user has permission to delete this environment.
environment = Environment.objects.get(pk=pk, project=project)

can_environment_be_deleted = can_model_instance_be_deleted("environment", pk)
project = Project.objects.get(slug=project_slug)
if not request.user.is_superuser:
return HttpResponseForbidden()
else:
if request.method == "POST":
pk = request.POST.get("environment_pk")
environment = Environment.objects.get(pk=pk, project=project)

if can_environment_be_deleted:
environment.delete()
else:
messages.error(
request,
"Environment cannot be deleted because it is currently used by at least one app \
(can also be a deleted app).",
)
can_environment_be_deleted = can_model_instance_be_deleted("environment", pk)

if can_environment_be_deleted:
environment.delete()
else:
messages.error(
request,
"Environment cannot be deleted because it is currently used by at least one app \
(can also be a deleted app).",
)
return HttpResponseRedirect(
reverse(
"projects:settings",
Expand Down Expand Up @@ -294,22 +298,25 @@ def create_flavor(request, project_slug):
@login_required
@permission_required_or_403("can_view_project", (Project, "slug", "project_slug"))
def delete_flavor(request, project_slug):
if request.method == "POST":
project = Project.objects.get(slug=project_slug)
pk = request.POST.get("flavor_pk")
# TODO: Check that the user has permission to delete this flavor.
flavor = Flavor.objects.get(pk=pk, project=project)
project = Project.objects.get(slug=project_slug)
if not request.user.is_superuser:
return HttpResponseForbidden()
else:
if request.method == "POST":
project = Project.objects.get(slug=project_slug)
pk = request.POST.get("flavor_pk")
flavor = Flavor.objects.get(pk=pk, project=project)

can_flavor_be_deleted = can_model_instance_be_deleted("flavor", pk)
can_flavor_be_deleted = can_model_instance_be_deleted("flavor", pk)

if can_flavor_be_deleted:
flavor.delete()
else:
messages.error(
request,
"Flavor cannot be deleted because it is currently used by at least one app \
(can also be a deleted app).",
)
if can_flavor_be_deleted:
flavor.delete()
else:
messages.error(
request,
"Flavor cannot be deleted because it is currently used by at least one app \
(can also be a deleted app).",
)

return HttpResponseRedirect(
reverse(
Expand Down

0 comments on commit d228984

Please sign in to comment.