Skip to content

Commit

Permalink
Accounts: Added option to watch a component
Browse files Browse the repository at this point in the history
It watches project, but disables all project level notifications and
enables them at component level.

Fixes #3510
  • Loading branch information
nijel committed Jul 23, 2020
1 parent a8f9805 commit f155e67
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Not yet released.
* Allow to avoid overwriting approved translations on file upload.
* Dropped support for some compatibility URL redirects.
* Added check for ECMAScript template literals.
* Added option to watch a component.

Weblate 4.1.1
-------------
Expand Down
16 changes: 16 additions & 0 deletions weblate/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,22 @@ def test_watch(self):
)
self.assertEqual(self.user.subscription_set.count(), 8)

def test_watch_component(self):
self.assertEqual(self.user.profile.watched.count(), 0)
self.assertEqual(self.user.subscription_set.count(), 8)

# Watch component
self.client.post(reverse("watch", kwargs=self.kw_component))
self.assertEqual(self.user.profile.watched.count(), 1)
# All project notifications should be muted
self.assertEqual(
self.user.subscription_set.filter(project=self.project).count(), 18
)
# Only default notifications should be enabled
self.assertEqual(
self.user.subscription_set.filter(component=self.component).count(), 3
)

def test_unsubscribe(self):
response = self.client.get(reverse("unsubscribe"), follow=True)
self.assertRedirects(response, reverse("profile") + "#notifications")
Expand Down
5 changes: 5 additions & 0 deletions weblate/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
path("unsubscribe/", weblate.accounts.views.unsubscribe, name="unsubscribe"),
path("subscribe/", weblate.accounts.views.subscribe, name="subscribe"),
path("watch/<name:project>/", weblate.accounts.views.watch, name="watch"),
path(
"watch/<name:project>/<name:component>/",
weblate.accounts.views.watch,
name="watch",
),
path("unwatch/<name:project>/", weblate.accounts.views.unwatch, name="unwatch"),
path(
"mute/<name:project>/<name:component>/",
Expand Down
27 changes: 23 additions & 4 deletions weblate/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,29 @@ def userdata(request):

@require_POST
@login_required
def watch(request, project):
obj = get_project(request, project)
request.user.profile.watched.add(obj)
return redirect(obj)
def watch(request, project, component=None):
user = request.user
if component:
redirect_obj = component_obj = get_component(request, project, component)
obj = component_obj.project
# Mute project level subscriptions
mute_real(user, scope=SCOPE_PROJECT, component=None, project=obj)
# Manually enable component level subscriptions
for default_subscription in user.subscription_set.filter(scope=SCOPE_DEFAULT):
subscription, created = user.subscription_set.get_or_create(
notification=default_subscription.notification,
scope=SCOPE_COMPONENT,
component=component_obj,
project=None,
defaults={"frequency": default_subscription.frequency},
)
if not created and subscription.frequency != default_subscription.frequency:
subscription.frequency = default_subscription.frequency
subscription.save(update_fields=["frequency"])
else:
redirect_obj = obj = get_project(request, project)
user.profile.watched.add(obj)
return redirect(redirect_obj)


@require_POST
Expand Down
11 changes: 10 additions & 1 deletion weblate/templates/snippets/watch-dropdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
{% endif %}
</ul>
{% else %}
<a href="{% url 'watch' project=project.slug %}" class="btn btn-link link-post" title="{% trans "You can receive notifications for watched projects and they are shown on the dashboard by default." %}">{% icon "eye.svg" %} {% trans "Watch" %}</a>
<a class="btn btn-link dropdown-toggle" type="button" id="notwatch_menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" href="#">
{% icon "eye.svg" %} {% trans "Not watching" %}
<span class="caret"></span>
</a>
<ul class="dropdown-menu dropdown-menu-right flip" aria-labelledby="notwatch_menu">
<li><a href="{% url 'watch' project=project.slug %}" class="btn btn-link link-post" title="{% trans "You can receive notifications for watched projects and they are shown on the dashboard by default." %}">{% blocktrans %}Watch project {{ project }}{% endblocktrans %}</a></li>
{% if component %}
<li><a href="{% url 'watch' project=project.slug component=component.slug %}" class="btn btn-link link-post" title="{% trans "You can receive notifications for watched projects and they are shown on the dashboard by default." %}">{% blocktrans %}Watch component {{ component }}{% endblocktrans %}</a></li>
{% endif %}
</ul>
{% endif %}

</div>
Expand Down

0 comments on commit f155e67

Please sign in to comment.