Auto generate slug for projects
parent
e79f0380dd
commit
33fac4b43a
|
@ -23,3 +23,9 @@ def first(iterable):
|
|||
def next(data:list):
|
||||
return data[1:]
|
||||
|
||||
|
||||
def arithmetic_progression(step=1, start=1):
|
||||
i = start
|
||||
while True:
|
||||
yield i
|
||||
i += step
|
||||
|
|
|
@ -35,6 +35,7 @@ from taiga.base.tags import TaggedMixin
|
|||
from taiga.users.models import Role
|
||||
from taiga.base.utils.slug import slugify_uniquely
|
||||
from taiga.base.utils.dicts import dict_sum
|
||||
from taiga.base.utils.sequence import arithmetic_progression
|
||||
|
||||
from . import choices
|
||||
|
||||
|
@ -181,7 +182,14 @@ class Project(ProjectDefaults, TaggedMixin, models.Model):
|
|||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug:
|
||||
self.slug = slugify_uniquely(self.name, self.__class__)
|
||||
base_slug = slugify_uniquely(self.name, self.__class__)
|
||||
slug = base_slug
|
||||
for i in arithmetic_progression():
|
||||
if not type(self).objects.filter(slug=slug).exists() or i > 100:
|
||||
break
|
||||
slug = "{}-{}".format(base_slug, i)
|
||||
self.slug = slug
|
||||
|
||||
if not self.videoconferences:
|
||||
self.videoconferences_salt = None
|
||||
|
||||
|
|
|
@ -145,17 +145,6 @@ class ProjectSerializer(serializers.ModelSerializer):
|
|||
# The "stars_count" attribute is attached in the get_queryset of the viewset.
|
||||
return getattr(obj, "stars_count", 0)
|
||||
|
||||
def validate_slug(self, attrs, source):
|
||||
if self.object:
|
||||
project_with_slug = models.Project.objects.filter(slug=attrs[source]).exclude(pk=self.object.pk)
|
||||
else:
|
||||
project_with_slug = models.Project.objects.filter(slug=attrs[source])
|
||||
|
||||
if source == "slug" and project_with_slug.exists():
|
||||
raise serializers.ValidationError(_("Slug duplicated for the project"))
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class ProjectDetailSerializer(ProjectSerializer):
|
||||
roles = serializers.SerializerMethodField("get_list_of_roles")
|
||||
|
|
|
@ -12,7 +12,7 @@ def test_api_create_project(client):
|
|||
f.ProjectTemplateFactory.create(slug=settings.DEFAULT_PROJECT_TEMPLATE)
|
||||
user = f.UserFactory.create()
|
||||
url = reverse("projects-list")
|
||||
data = {"name": "project name", "slug": "project-slug", "description": "project description"}
|
||||
data = {"name": "project name", "description": "project description"}
|
||||
|
||||
client.login(user)
|
||||
response = client.json.post(url, data)
|
||||
|
|
Loading…
Reference in New Issue