Commit 77b45c05 authored by Michal Čihař's avatar Michal Čihař

Added option to import components with duplicate name to import_project.

Fixes #999
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 78ed42fa
......@@ -184,6 +184,13 @@ against file name (as matched by `<filemask>`) and has to contain named group
`name`. This can be also used for excluding files in case they do not match
this expression. For example: ``.*/(?P<name>[^-]*)\.po``
By default the import does skip already existing projects. This is to allow
repeated importing of same repository. However if you want to force importing
additional components even if name or slug matches existing one, you can do it
by passing ``--no-skip-duplicates``. This is generally useful for components
with long names, which will get truncated on import and many of them will get
same name or slug.
To give you some examples, let's try importing two projects.
As first we import The Debian Handbook translations, where each language has
......
......@@ -40,6 +40,7 @@ Released on ? 2015.
* Dropped dependency on msginit for creating new Gettext po files.
* Added configurable dashboard views.
* Improved notifications on parse erorrs.
* Added option to import components with duplicate name to import_project.
weblate 2.4
-----------
......
......@@ -81,6 +81,16 @@ class Command(BaseCommand):
' components'
),
),
make_option(
'--no-skip-duplicates',
action='store_true',
default=False,
dest='duplicates',
help=(
'Avoid skipping duplicate component names/slugs. '
'Use this if importing project with long names '
)
),
make_option(
'--license',
default=None,
......@@ -192,6 +202,21 @@ class Command(BaseCommand):
self.logger.info('Found %d subprojects', len(names))
return sorted(names)
def find_usable_slug(self, name, slug_len, project):
base = name[:slug_len - 4]
for i in range(1, 1000):
newname = '{0} {1:03d}'.format(base, i)
slug = slugify(newname)
subprojects = SubProject.objects.filter(
Q(name=newname) | Q(slug=slug),
project=project
)
if not subprojects.exists():
return newname, slug
raise CommandError(
'Failed to find suitable name for {0}'.format(name)
)
def handle(self, *args, **options):
'''
Automatic import of project.
......@@ -284,11 +309,16 @@ class Command(BaseCommand):
project=project
)
if subprojects.exists():
self.logger.warning(
'Component %s already exists, skipping',
name
)
continue
if not options['duplicates']:
self.logger.warning(
'Component %s already exists, skipping',
name
)
continue
else:
name, slug = self.find_usable_slug(
name, slug_len, project
)
self.logger.info('Creating component %s', name)
SubProject.objects.create(
......
......@@ -42,18 +42,36 @@ class RunnerTest(TestCase):
class ImportProjectTest(RepoTestCase):
def test_import(self):
project = self.create_project()
def do_import(self, path=None, **kwargs):
call_command(
'import_project',
'test',
self.git_repo_path,
self.git_repo_path if path is None else path,
'master',
'**/*.po',
**kwargs
)
def test_import(self):
project = self.create_project()
self.do_import()
# We should have loaded four subprojects
self.assertEqual(project.subproject_set.count(), 4)
def test_import_ignore(self):
project = self.create_project()
self.do_import()
self.do_import()
# We should have loaded four subprojects
self.assertEqual(project.subproject_set.count(), 4)
def test_import_duplicate(self):
project = self.create_project()
self.do_import()
self.do_import(path='weblate://test/po', duplicates=True)
# We should have loaded eight subprojects
self.assertEqual(project.subproject_set.count(), 8)
def test_import_main_1(self, name='po-mono'):
project = self.create_project()
call_command(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment