Skip to content

Commit

Permalink
SS-1112 Enforce unique project names by owner (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
anondo1969 authored Nov 20, 2024
1 parent 32dc57f commit 7e859e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 28 additions & 1 deletion projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests as r
from django.apps import apps
from django.conf import settings as django_settings
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.core.exceptions import FieldDoesNotExist
Expand Down Expand Up @@ -387,7 +388,9 @@ def get(self, request):

template = arr[0] if len(arr) > 0 else None

context = {"template": template}
context = {
"template": template,
}

return render(
request=request,
Expand All @@ -407,6 +410,30 @@ def post(self, request, *args, **kwargs):
name = request.POST.get("name", "default")[:200]
description = request.POST.get("description", "")

# Ensure no duplicate project name for the common user

project_name_already_exists = (
Project.objects.filter(
owner=request.user,
name=name,
)
.exclude(status="deleted")
.exists()
)

if project_name_already_exists and not request.user.is_superuser:
pre_selected_template = request.GET.get("template")
template = ProjectTemplate.objects.filter(name=pre_selected_template).first()
context = {"template": template}
logger.error("A project with name '" + name + "' already exists.")

messages.error(
request,
"Project cannot be created because a project with name '" + name + "' already exists.",
)

return render(request, self.template_name, context)

# Try to create database project object.
try:
project = Project.objects.create_project(
Expand Down
5 changes: 2 additions & 3 deletions templates/projects/project_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
{% block content %}

{% include "breadcrumbs/bc_project_create.html" %}
{% include 'common/flash_messages.html' %}

<div class="row">

<div class="col">
<h3>New project</h3>
<p>Provide details about your new project.</p>
</div>
</div>




<div class="row pt-3">
<div class="col-12 col-xl-6">
{% if template %}
Expand Down

0 comments on commit 7e859e3

Please sign in to comment.