Commit 9c12d63a authored by Michal Čihař's avatar Michal Čihař

Correctly copy suggestions on component move

Fixes #896
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 41a3f7e9
......@@ -1359,6 +1359,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
# Detect if VCS config has changed (so that we have to pull the repo)
changed_git = True
changed_setup = False
changed_project = False
if self.id:
old = SubProject.objects.get(pk=self.id)
changed_git = (
......@@ -1371,6 +1372,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
(old.edit_template != self.edit_template) or
(old.template != self.template)
)
changed_project = (old.project_id != self.project_id)
# Detect slug changes and rename git repo
self.check_rename(old)
......@@ -1396,6 +1398,11 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
elif changed_git:
self.create_translations()
# Copy suggestions to new project
if changed_project:
print 'CHANGED!'
old.project.suggestion_set.copy(self.project)
def _get_percents(self):
'''
Returns percentages of translation status.
......
......@@ -76,6 +76,23 @@ class SuggestionManager(models.Manager):
user.profile.suggested += 1
user.profile.save()
def copy(self, project):
"""Copies suggestions to new project
This is used on moving component to other project and ensures nothing
is lost. We don't actually look where the suggestion belongs as it
would make the operation really expensive and it should be done in the
cleanup cron job.
"""
for suggestion in self.all():
Suggestion.objects.create(
project=project,
target=suggestion.target,
contentsum=suggestion.contentsum,
user=suggestion.user,
language=suggestion.language,
)
class Suggestion(models.Model):
contentsum = models.CharField(max_length=40, db_index=True)
......
......@@ -30,7 +30,7 @@ from django.core.exceptions import ValidationError
import shutil
import os
from weblate.trans.models import (
Project, SubProject, Source, Unit, WhiteboardMessage, Check,
Project, SubProject, Source, Unit, WhiteboardMessage, Check, Suggestion,
get_related_units,
)
from weblate import appsettings
......@@ -778,6 +778,42 @@ class SubProjectTest(RepoTestCase):
'en'
)
def test_change_project(self):
subproject = self.create_subproject()
# Create and verify suggestion
Suggestion.objects.create(
project=subproject.project,
contentsum='x',
language=subproject.translation_set.all()[0].language,
)
self.assertEqual(subproject.project.suggestion_set.count(), 1)
# Check current path exists
old_path = subproject.get_path()
self.assertTrue(os.path.exists(old_path))
# Crete target project
second = Project.objects.create(
name='Test2',
slug='test2',
web='https://weblate.org/'
)
# Move component
subproject.project = second
subproject.save()
# Check new path exists
new_path = subproject.get_path()
self.assertTrue(os.path.exists(new_path))
# Check paths differ
self.assertNotEqual(old_path, new_path)
# Check suggestion has been copied
self.assertEqual(subproject.project.suggestion_set.count(), 1)
class TranslationTest(RepoTestCase):
"""
......
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