Commit d5397e57 authored by Weblate's avatar Weblate

Merge remote-tracking branch 'origin/master'

parents 5638c889 0e993dda
......@@ -42,6 +42,114 @@ from trans.util import get_user_display, get_site_url
import weblate
def notify_merge_failure(subproject, error, status):
'''
Notification on merge failure.
'''
subscriptions = Profile.objects.subscribed_merge_failure(
subproject.project,
)
for subscription in subscriptions:
subscription.notify_merge_failure(subproject, error, status)
# Notify admins
send_notification_email(
'en',
'ADMINS',
'merge_failure',
subproject,
{
'subproject': subproject,
'status': status,
'error': error,
}
)
def notify_new_string(translation):
'''
Notification on new string to translate.
'''
subscriptions = Profile.objects.subscribed_new_string(
translation.subproject.project, translation.language
)
for subscription in subscriptions:
subscription.notify_new_string(translation)
def notify_new_translation(unit, oldunit, user):
'''
Notify subscribed users about new translation
'''
subscriptions = Profile.objects.subscribed_any_translation(
unit.translation.subproject.project,
unit.translation.language,
user
)
for subscription in subscriptions:
subscription.notify_any_translation(unit, oldunit)
def notify_new_contributor(unit, user):
'''
Notify about new contributor.
'''
subscriptions = Profile.objects.subscribed_new_contributor(
unit.translation.subproject.project,
unit.translation.language,
user
)
for subscription in subscriptions:
subscription.notify_new_contributor(
unit.translation, user
)
def notify_new_suggestion(unit, suggestion, user):
'''
Notify about new suggestion.
'''
subscriptions = Profile.objects.subscribed_new_suggestion(
unit.translation.subproject.project,
unit.translation.language,
user
)
for subscription in subscriptions:
subscription.notify_new_suggestion(
unit.translation,
suggestion,
unit
)
def notify_new_comment(unit, comment, user, report_source_bugs):
'''
Notify about new comment.
'''
subscriptions = Profile.objects.subscribed_new_comment(
unit.translation.subproject.project,
comment.language,
user
)
for subscription in subscriptions:
subscription.notify_new_comment(unit, comment)
# Notify upstream
if comment.language is None and report_source_bugs != '':
send_notification_email(
'en',
report_source_bugs,
'new_comment',
unit.translation,
{
'unit': unit,
'comment': comment,
'subproject': unit.translation.subproject,
},
from_email=user.email,
)
def send_notification_email(language, email, notification, translation_obj,
context=None, headers=None, from_email=None):
'''
......
......@@ -28,7 +28,19 @@ from django.contrib.auth.models import User, Group
from django.core import mail
from django.conf import settings
from django.core.management import call_command
from accounts.models import Profile
from accounts.models import (
Profile,
notify_merge_failure,
notify_new_string,
notify_new_suggestion,
notify_new_comment,
notify_new_translation,
notify_new_contributor,
)
from trans.tests.views import ViewTestCase
from trans.models.unitdata import Suggestion, Comment
from lang.models import Language
class RegistrationTest(TestCase):
......@@ -129,6 +141,32 @@ class ViewTest(TestCase):
'[Weblate] Message from dark side'
)
def test_contact_subject(self):
# With set subject
response = self.client.get(
reverse('contact'),
{'subject': 'Weblate test message'}
)
self.assertContains(response, 'Weblate test message')
def test_contact_user(self):
user = User.objects.create_user(
username='testuser',
password='testpassword',
)
user.first_name = 'First'
user.last_name = 'Second'
user.email = 'noreply@weblate.org'
user.save()
Profile.objects.get_or_create(user=user)
# Login
self.client.login(username='testuser', password='testpassword')
response = self.client.get(
reverse('contact'),
)
self.assertContains(response, 'value="First Second"')
self.assertContains(response, 'noreply@weblate.org')
def test_user(self):
'''
Test user pages.
......@@ -149,6 +187,170 @@ class ViewTest(TestCase):
)
self.assertContains(response, 'src="/activity')
class ProfileTest(ViewTestCase):
def test_profile(self):
# Get profile page
response = self.client.get(reverse('profile'))
self.assertContains(response, 'class="tabs preferences"')
# Save profile
response = self.client.post(
reverse('profile'),
{
'language': 'cs',
'languages': Language.objects.get(code='cs').id,
'secondary_languages': Language.objects.get(code='cs').id,
'first_name': 'First',
'last_name': 'Last',
'email': 'noreply@weblate.org',
}
)
self.assertRedirects(response, reverse('profile'))
class NotificationTest(ViewTestCase):
def setUp(self):
super(NotificationTest, self).setUp()
self.user.email = 'noreply@weblate.org'
self.user.save()
profile = Profile.objects.get(user=self.user)
profile.subscribe_any_translation = True
profile.subscribe_new_string = True
profile.subscribe_new_suggestion = True
profile.subscribe_new_contributor = True
profile.subscribe_new_comment = True
profile.subscribe_merge_failure = True
profile.subscriptions.add(self.project)
profile.languages.add(
Language.objects.get(code='cs')
)
profile.save()
def second_user(self):
return User.objects.create_user(
username='seconduser',
password='secondpassword'
)
def test_notify_merge_failure(self):
notify_merge_failure(
self.subproject,
'Failed merge',
'Error\nstatus'
)
# Check mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] Merge failure in Test/Test'
)
def test_notify_new_string(self):
notify_new_string(self.get_translation())
# Check mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] New string to translate in Test/Test - Czech'
)
def test_notify_new_translation(self):
unit = self.get_unit()
unit2 = self.get_translation().unit_set.get(
source='Thank you for using Weblate.'
)
notify_new_translation(
unit,
unit2,
self.second_user()
)
# Check mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] New translation in Test/Test - Czech'
)
def test_notify_new_contributor(self):
unit = self.get_unit()
notify_new_contributor(
unit,
self.second_user()
)
# Check mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] New contributor in Test/Test - Czech'
)
def test_notify_new_suggestion(self):
unit = self.get_unit()
notify_new_suggestion(
unit,
Suggestion.objects.create(
checksum=unit.checksum,
project=unit.translation.subproject.project,
language=unit.translation.language,
target='Foo'
),
self.second_user()
)
# Check mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] New suggestion in Test/Test - Czech'
)
def test_notify_new_comment(self):
unit = self.get_unit()
notify_new_comment(
unit,
Comment.objects.create(
checksum=unit.checksum,
project=unit.translation.subproject.project,
language=unit.translation.language,
comment='Foo'
),
self.second_user(),
''
)
# Check mail
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] New comment in Test/Test'
)
def test_notify_new_comment_report(self):
unit = self.get_unit()
notify_new_comment(
unit,
Comment.objects.create(
checksum=unit.checksum,
project=unit.translation.subproject.project,
language=None,
comment='Foo'
),
self.second_user(),
'noreply@weblate.org'
)
# Check mail
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(
mail.outbox[0].subject,
'[Weblate] New comment in Test/Test'
)
self.assertEqual(
mail.outbox[1].subject,
'[Weblate] New comment in Test/Test'
)
......@@ -593,25 +593,8 @@ class SubProject(models.Model, PercentMixin, URLMixin):
Sends out notifications on merge failure.
'''
# Notify subscribed users about failure
from accounts.models import Profile, send_notification_email
subscriptions = Profile.objects.subscribed_merge_failure(
self.project,
)
for subscription in subscriptions:
subscription.notify_merge_failure(self, error, status)
# Notify admins
send_notification_email(
'en',
'ADMINS',
'merge_failure',
self,
{
'subproject': self,
'status': status,
'error': error,
}
)
from accounts.models import notify_merge_failure
notify_merge_failure(self, error, status)
def update_branch(self, request=None):
'''
......
......@@ -561,12 +561,8 @@ class Translation(models.Model, URLMixin):
# Notify subscribed users
if was_new:
from accounts.models import Profile
subscriptions = Profile.objects.subscribed_new_string(
self.subproject.project, self.language
)
for subscription in subscriptions:
subscription.notify_new_string(self)
from accounts.models import notify_new_string
notify_new_string(self)
@property
def git_repo(self):
......
......@@ -577,7 +577,9 @@ class Unit(models.Model):
'''
Stores unit to backend.
'''
from accounts.models import Profile
from accounts.models import (
notify_new_translation, notify_new_contributor
)
from trans.models.changes import Change
# Update lock timestamp
......@@ -636,13 +638,7 @@ class Unit(models.Model):
self.translation.update_stats()
# Notify subscribed users about new translation
subscriptions = Profile.objects.subscribed_any_translation(
self.translation.subproject.project,
self.translation.language,
request.user
)
for subscription in subscriptions:
subscription.notify_any_translation(self, oldunit)
notify_new_translation(self, oldunit, request.user)
# Update user stats
profile = request.user.get_profile()
......@@ -655,16 +651,7 @@ class Unit(models.Model):
user=request.user
)
if not user_changes.exists():
# Get list of subscribers for new contributor
subscriptions = Profile.objects.subscribed_new_contributor(
self.translation.subproject.project,
self.translation.language,
request.user
)
for subscription in subscriptions:
subscription.notify_new_contributor(
self.translation, request.user
)
notify_new_contributor(self, request.user)
# Generate Change object for this change
if gen_change:
......@@ -997,7 +984,7 @@ class Unit(models.Model):
'''
from trans.models.unitdata import Suggestion
from trans.models.changes import Change
from accounts.models import Profile
from accounts.models import notify_new_suggestion
if not user.is_authenticated():
user = None
......@@ -1020,17 +1007,7 @@ class Unit(models.Model):
)
# 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
)
notify_new_suggestion(self, suggestion, user)
# Update suggestion stats
if user is not None:
......@@ -1048,7 +1025,7 @@ class Unit(models.Model):
'''
from trans.models.unitdata import Comment
from trans.models.changes import Change
from accounts.models import Profile, send_notification_email
from accounts.models import notify_new_comment
new_comment = Comment.objects.create(
user=user,
......@@ -1075,26 +1052,9 @@ class Unit(models.Model):
unit.update_has_comment()
# Notify subscribed users
subscriptions = Profile.objects.subscribed_new_comment(
self.translation.subproject.project,
lang,
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=user.email,
notify_new_comment(
self,
new_comment,
user,
self.translation.subproject.report_source_bugs
)
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