diff --git a/projects/views.py b/projects/views.py index 170a8a76..c7ac1321 100644 --- a/projects/views.py +++ b/projects/views.py @@ -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 @@ -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, @@ -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( diff --git a/templates/projects/project_create.html b/templates/projects/project_create.html index f02e01d1..6ba3369c 100644 --- a/templates/projects/project_create.html +++ b/templates/projects/project_create.html @@ -4,17 +4,16 @@ {% block content %} {% include "breadcrumbs/bc_project_create.html" %} +{% include 'common/flash_messages.html' %}
+

New project

Provide details about your new project.

- - -
{% if template %}