Clean duplicate code in project slug generation process

remotes/origin/issue/4795/notification_even_they_are_disabled
David Barragán Merino 2016-10-17 19:17:21 +02:00 committed by Alejandro Alonso
parent 67669eb817
commit f7918abddb
3 changed files with 3 additions and 45 deletions

View File

@ -20,7 +20,6 @@ import abc
from functools import reduce
from taiga.base.utils import sequence as sq
from taiga.permissions.services import user_has_perm, is_project_admin
from django.apps import apps
@ -112,7 +111,7 @@ class Not(PermissionOperator):
super().__init__(component)
def check_permissions(self, *args, **kwargs):
component = sq.first(self.components)
component = self.components[0]
return (not component.check_permissions(*args, **kwargs))

View File

@ -1,34 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2014-2016 Andrey Antukh <niwi@niwi.nz>
# Copyright (C) 2014-2016 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014-2016 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2014-2016 Alejandro Alonso <alejandro.alonso@kaleidos.net>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def first(iterable):
if len(iterable) == 0:
return None
return iterable[0]
def next(data:list):
return data[1:]
def arithmetic_progression(step=1, start=1):
i = start
while True:
yield i
i += step

View File

@ -35,7 +35,6 @@ from taiga.base.utils.time import timestamp_ms
from taiga.projects.tagging.models import TaggedMixin
from taiga.projects.tagging.models import TagsColorsdMixin
from taiga.base.utils.files import get_file_path
from taiga.base.utils.sequence import arithmetic_progression
from taiga.base.utils.slug import slugify_uniquely
from taiga.base.utils.slug import slugify_uniquely_for_queryset
@ -287,14 +286,8 @@ class Project(ProjectDefaults, TaggedMixin, TagsColorsdMixin, models.Model):
if not self.slug:
with advisory_lock("project-creation"):
base_name = "{}-{}".format(self.owner.username, self.name)
base_slug = slugify_uniquely(base_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
base_slug = "{}-{}".format(self.owner.username, self.name)
self.slug = slugify_uniquely(base_slug, self.__class__)
super().save(*args, **kwargs)
else:
super().save(*args, **kwargs)