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

Update comment stats on runtime

parent 3e8959c5
......@@ -1054,3 +1054,59 @@ class Unit(models.Model):
# Update unit flags
for unit in suggestion.get_related_units():
unit.update_has_suggestion()
def add_comment(self, user, lang, text):
'''
Adds comment to this unit.
'''
from trans.models.unitdata import Comment, Change
from accounts.models import Profile
new_comment = Comment.objects.create(
user=user,
checksum=self.checksum,
project=self.translation.subproject.project,
comment=text
language=lang
)
Change.objects.create(
unit=self,
action=Change.ACTION_COMMENT,
translation=self.translation,
user=request.user
)
# Invalidate counts cache
if lang is None:
self.translation.invalidate_cache('sourcecomments')
else:
self.translation.invalidate_cache('targetcomments')
# Update unit stats
for unit in new_comment.get_related_units():
unit.update_has_comment()
# Notify subscribed users
subscriptions = Profile.objects.subscribed_new_comment(
self.translation.subproject.project,
lang,
request.user
)
for subscription in subscriptions:
subscription.notify_new_comment(self, new_comment)
# Notify upstream
report_source_bugs = self.translation.subproject.report_source_bugs
if lang is None and report_source_bugs != '':
send_notification_email(
'en',
report_source_bugs,
'new_comment',
self.translation,
{
'unit': self,
'comment': new_comment,
'subproject': self.translation.subproject,
},
from_email=request.user.email,
)
......@@ -117,6 +117,12 @@ class Comment(models.Model, RelatedUnitMixin):
def get_user_display(self):
return get_user_display(self.user, link=True)
def delete(self, *args, **kwargs):
super(Suggestion, self).delete(*args, **kwargs)
# Update unit flags
for unit in suggestion.get_related_units():
unit.update_has_comment()
CHECK_CHOICES = [(x, CHECKS[x].name) for x in CHECKS]
......
......@@ -28,7 +28,7 @@ from django.utils import formats
import uuid
import time
from trans.models import SubProject, Unit, Suggestion, Change, Comment
from trans.models import SubProject, Unit, Suggestion, Change
from trans.forms import (
TranslationForm, SearchForm,
MergeForm, AutoForm, ReviewForm,
......@@ -561,49 +561,8 @@ def comment(request, pk):
form = CommentForm(request.POST)
if form.is_valid():
new_comment = Comment.objects.create(
user=request.user,
checksum=obj.checksum,
project=obj.translation.subproject.project,
comment=form.cleaned_data['comment'],
language=lang
)
Change.objects.create(
unit=obj,
action=Change.ACTION_COMMENT,
translation=obj.translation,
user=request.user
)
# Invalidate counts cache
if lang is None:
obj.translation.invalidate_cache('sourcecomments')
else:
obj.translation.invalidate_cache('targetcomments')
unit.add_comment(request.user, lang, form.cleaned_data['comment'])
messages.info(request, _('Posted new comment'))
# Notify subscribed users
subscriptions = Profile.objects.subscribed_new_comment(
obj.translation.subproject.project,
lang,
request.user
)
for subscription in subscriptions:
subscription.notify_new_comment(obj, new_comment)
# Notify upstream
report_source_bugs = obj.translation.subproject.report_source_bugs
if lang is None and report_source_bugs != '':
send_notification_email(
'en',
report_source_bugs,
'new_comment',
obj.translation,
{
'unit': obj,
'comment': new_comment,
'subproject': obj.translation.subproject,
},
from_email=request.user.email,
)
else:
messages.error(request, _('Failed to add comment!'))
......
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