Commit 1e468d6d authored by Michal Čihař's avatar Michal Čihař

Move suggestion addding code to Suggestion model where it belongs

parent 143be07c
......@@ -1100,7 +1100,7 @@ class Translation(models.Model, URLMixin):
dbunit = dbunit[0]
# Add suggestion
dbunit.add_suggestion(unit.get_target(), request.user)
Suggestion.objects.add(dbunit, unit.get_target(), request.user)
# Update suggestion count
if ret:
......
......@@ -977,44 +977,3 @@ class Unit(models.Model):
position__gte=self.position - appsettings.NEARBY_MESSAGES,
position__lte=self.position + appsettings.NEARBY_MESSAGES,
).select_related()
def add_suggestion(self, target, user):
'''
Creates new suggestion for this unit.
'''
from trans.models.unitdata import Suggestion
from trans.models.changes import Change
from accounts.models import notify_new_suggestion
if not user.is_authenticated():
user = None
# Create the suggestion
suggestion = Suggestion.objects.create(
target=target,
checksum=self.checksum,
language=self.translation.language,
project=self.translation.subproject.project,
user=user
)
# Record in change
Change.objects.create(
unit=self,
action=Change.ACTION_SUGGESTION,
translation=self.translation,
user=user
)
# Notify subscribed users
notify_new_suggestion(self, suggestion, user)
# Update suggestion stats
if user is not None:
profile = user.get_profile()
profile.suggested += 1
profile.save()
# Update unit flags
for unit in suggestion.get_related_units():
unit.update_has_suggestion()
......@@ -46,6 +46,49 @@ class RelatedUnitMixin(object):
return related_units
class SuggestionManager(models.Manager):
def add(self, unit, target, user):
'''
Creates new suggestion for this unit.
'''
from trans.models.unitdata import Suggestion
from trans.models.changes import Change
from accounts.models import notify_new_suggestion
if not user.is_authenticated():
user = None
# Create the suggestion
suggestion = Suggestion.objects.create(
target=target,
checksum=unit.checksum,
language=unit.translation.language,
project=unit.translation.subproject.project,
user=user
)
# Record in change
Change.objects.create(
unit=unit,
action=Change.ACTION_SUGGESTION,
translation=unit.translation,
user=user
)
# Notify subscribed users
notify_new_suggestion(unit, suggestion, user)
# Update suggestion stats
if user is not None:
profile = user.get_profile()
profile.suggested += 1
profile.save()
# Update unit flags
for relunit in suggestion.get_related_units():
relunit.update_has_suggestion()
class Suggestion(models.Model, RelatedUnitMixin):
checksum = models.CharField(max_length=40, db_index=True)
target = models.TextField()
......@@ -53,6 +96,8 @@ class Suggestion(models.Model, RelatedUnitMixin):
project = models.ForeignKey(Project)
language = models.ForeignKey(Language)
objects = SuggestionManager()
class Meta:
permissions = (
('accept_suggestion', "Can accept suggestion"),
......
......@@ -30,8 +30,8 @@ import uuid
import time
from urllib import urlencode
from trans.models import SubProject, Unit, Suggestion, Change
from trans.models.unitdata import Comment
from trans.models import SubProject, Unit, Change
from trans.models.unitdata import Comment, Suggestion
from trans.forms import (
TranslationForm, SearchForm,
MergeForm, AutoForm, ReviewForm,
......@@ -218,7 +218,8 @@ def handle_translate(obj, request, user_locked, this_unit_url, next_unit_url):
'as your suggestion might otherwise remain unreviewed.'
))
# Create the suggestion
unit.add_suggestion(
Suggestion.objects.add(
unit,
join_plural(form.cleaned_data['target']),
user,
)
......
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