Skip to content

Commit a2cf44b

Browse files
lordmauveagjohnson
authored andcommitted
Fix lint issues for notifications app (readthedocs#2881)
1 parent beeb82c commit a2cf44b

File tree

9 files changed

+58
-16
lines changed

9 files changed

+58
-16
lines changed

prospector-more.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ ignore-paths:
66
- core/
77
- doc_builder/
88
- donate/
9-
- notifications/
109
- privacy/
1110
- projects/
1211
- redirects/

readthedocs/notifications/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1+
"""Extensions to Django messages to support notifications to users.
2+
3+
Notifications are important communications to users that need to be as visible
4+
as possible. We support different backends to make notifications visible in
5+
different ways. For example, they might be e-mailed to users as well as
6+
displayed on the site.
7+
8+
This app builds on `django-messages-extends`_ to provide persistent messages
9+
on the site.
10+
11+
.. _`django-messages-extends`: https://github.com
12+
/AliLozano/django-messages-extends/
13+
14+
"""
115
from .notification import Notification
216
from .backends import send_notification
317

18+
__all__ = (
19+
'Notification',
20+
'send_notification'
21+
)
22+
23+
424
default_app_config = 'readthedocs.notifications.apps.NotificationsAppConfig'

readthedocs/notifications/apps.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Django app configuration for the notifications app."""
12
from django.apps import AppConfig
23

34

readthedocs/notifications/backends.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
"""Notification to message backends"""
1+
"""Pluggable backends for the delivery of notifications.
2+
3+
Delivery of notifications to users depends on a list of backends configured in
4+
Django settings. For example, they might be e-mailed to users as well as
5+
displayed on the site.
6+
7+
"""
28

39
from django.conf import settings
410
from django.http import HttpRequest
511
from django.utils.module_loading import import_string
612
from messages_extends.constants import INFO_PERSISTENT
7-
from messages_extends import add_message
813

914
from readthedocs.core.utils import send_email
1015

readthedocs/notifications/forms.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""HTML forms for sending notifications."""
12
from django import forms
23
from django.utils.translation import ugettext_lazy as _
34

readthedocs/notifications/notification.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1+
"""Support for templating of notifications."""
2+
13
from django.conf import settings
24
from django.template import Template, Context
35
from django.template.loader import render_to_string
46
from django.db import models
57

68
from .backends import send_notification
7-
from .constants import INFO, HTML, TEXT
9+
from . import constants
810

911

1012
class Notification(object):
1113

14+
"""An unsent notification linked to an object.
15+
16+
This class provides an interface to construct notification messages by
17+
rendering Django templates. The ``Notification`` itself is not expected
18+
to be persisted by the backends.
19+
20+
Call .send() to send the notification.
21+
22+
"""
23+
1224
name = None
1325
context_object_name = 'object'
14-
level = INFO
26+
level = constants.INFO
1527
subject = None
1628
user = None
1729

18-
def __init__(self, object, request, user=None):
19-
self.object = object
30+
def __init__(self, context_object, request, user=None):
31+
self.object = context_object
2032
self.request = request
2133
self.user = user
2234
if self.user is None:
@@ -35,22 +47,23 @@ def get_context_data(self):
3547
)
3648
}
3749

38-
def get_template_names(self, backend_name, source_format=HTML):
50+
def get_template_names(self, backend_name, source_format=constants.HTML):
3951
names = []
4052
if self.object and isinstance(self.object, models.Model):
53+
meta = self.object._meta # pylint: disable=protected-access
4154
names.append(
4255
'{app}/notifications/{name}_{backend}.{source_format}'
4356
.format(
44-
app=self.object._meta.app_label,
45-
name=self.name or self.object._meta.model_name,
57+
app=meta.app_label,
58+
name=self.name or meta.model_name,
4659
backend=backend_name,
4760
source_format=source_format,
4861
))
4962
return names
5063
else:
5164
raise AttributeError()
5265

53-
def render(self, backend_name, source_format=HTML):
66+
def render(self, backend_name, source_format=constants.HTML):
5467
return render_to_string(
5568
template_name=self.get_template_names(
5669
backend_name=backend_name,

readthedocs/notifications/storages.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Customised storage for notifications."""
2+
13
from django.contrib.messages.storage.base import Message
24
from django.utils.safestring import mark_safe
35
from messages_extends.storages import FallbackStorage

readthedocs/notifications/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Django views for the notifications app."""
12
from django.views.generic import FormView
23
from django.contrib import admin, messages
34
from django.http import HttpResponseRedirect
@@ -51,7 +52,7 @@ def form_valid(self, form):
5152
notification_cls = form.cleaned_data['source']
5253
for obj in self.get_queryset().all():
5354
for recipient in self.get_object_recipients(obj):
54-
notification = notification_cls(object=obj,
55+
notification = notification_cls(context_object=obj,
5556
request=self.request,
5657
user=recipient)
5758
notification.send()

readthedocs/rtd_tests/tests/test_notifications.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TestNotification(Notification):
3333

3434
build = fixture.get(Build)
3535
req = mock.MagicMock()
36-
notify = TestNotification(object=build, request=req)
36+
notify = TestNotification(context_object=build, request=req)
3737

3838
self.assertEqual(notify.get_template_names('email'),
3939
['builds/notifications/foo_email.html'])
@@ -69,7 +69,7 @@ class TestNotification(Notification):
6969
build = fixture.get(Build)
7070
req = mock.MagicMock()
7171
user = fixture.get(User)
72-
notify = TestNotification(object=build, request=req, user=user)
72+
notify = TestNotification(context_object=build, request=req, user=user)
7373
backend = EmailBackend(request=req)
7474
backend.send(notify)
7575

@@ -96,7 +96,7 @@ class TestNotification(Notification):
9696
build = fixture.get(Build)
9797
user = fixture.get(User)
9898
req = mock.MagicMock()
99-
notify = TestNotification(object=build, request=req, user=user)
99+
notify = TestNotification(context_object=build, request=req, user=user)
100100
backend = SiteBackend(request=req)
101101
backend.send(notify)
102102

@@ -118,7 +118,7 @@ class TestNotification(Notification):
118118
build = fixture.get(Build)
119119
user = AnonymousUser()
120120
req = mock.MagicMock()
121-
notify = TestNotification(object=build, request=req, user=user)
121+
notify = TestNotification(context_object=build, request=req, user=user)
122122
backend = SiteBackend(request=req)
123123

124124
self.assertEqual(PersistentMessage.objects.count(), 0)

0 commit comments

Comments
 (0)