Commit c3ffc8bb authored by Michal Čihař's avatar Michal Čihař

Move suggestion notifications to suggestion adding code

parent 9e967a50
......@@ -879,20 +879,22 @@ class Unit(models.Model):
position__lte=self.position + appsettings.NEARBY_MESSAGES,
)
def add_suggestion(self, target, user):
def add_suggestion(self, target, user, profile):
'''
Creates new suggestion for this unit.
'''
from trans.models.unitdata import Suggestion, Change
from accounts.models import Profile
# Create the suggestion
Suggestion.objects.create(
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,
......@@ -900,6 +902,25 @@ class Unit(models.Model):
translation=self.translation,
user=user
)
# Update suggestion count
self.translation.have_suggestion += 1
self.translation.save()
# Notify subscribed users
subscriptions = Profile.objects.subscribed_new_suggestion(
self.translation.subproject.project,
self.translation.language,
user
)
for subscription in subscriptions:
subscription.notify_new_suggestion(
self.translation,
suggestion,
self
)
# Update suggestion stats
if profile is not None:
profile.suggested += 1
profile.save()
......@@ -111,7 +111,8 @@ def translate(request, project, subproject, lang):
# Create the suggestion
unit.add_suggestion(
join_plural(form.cleaned_data['target']),
user
user,
profile
)
# Invite user to become translator if there is nobody else
recent_changes = Change.objects.content().filter(
......@@ -124,18 +125,6 @@ def translate(request, project, subproject, lang):
request,
_('There is currently no active translator for this translation, please consider becoming a translator as your suggestion might otherwise remain unreviewed.')
)
# Notify subscribed users
subscriptions = Profile.objects.subscribed_new_suggestion(
obj.subproject.project,
obj.language,
request.user
)
for subscription in subscriptions:
subscription.notify_new_suggestion(obj, sug, unit)
# Update suggestion stats
if profile is not None:
profile.suggested += 1
profile.save()
elif not request.user.is_authenticated():
# We accept translations only from authenticated
messages.error(
......
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