Skip to content

Commit

Permalink
refactor: [FC-0047] review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NiedielnitsevIvan committed Jun 24, 2024
1 parent 0368b98 commit 37d002c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
11 changes: 5 additions & 6 deletions edx_ace/ace.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
)
ace.send(msg)
"""
import logging
from django.template import TemplateDoesNotExist

from edx_ace import delivery, policy, presentation
from edx_ace.channel import get_channel_for_message
from edx_ace.errors import ChannelError, UnsupportedChannelError

log = logging.getLogger(__name__)


def send(msg, limit_to_channels=None):
"""
Expand All @@ -48,11 +51,7 @@ def send(msg, limit_to_channels=None):

for channel_type in channels_for_message:
if limit_to_channels and channel_type not in limit_to_channels:
msg.report(
'channel_skipped',
f'Skipping channel {channel_type}'
)
continue
log.info(f'Skipping channel {channel_type}')

try:
channel = get_channel_for_message(channel_type, msg)
Expand All @@ -64,7 +63,7 @@ def send(msg, limit_to_channels=None):
except TemplateDoesNotExist as error:
msg.report(
'template_error',
str(error)
'Unable to send message because template not found\n' + str(error)
)
continue

Expand Down
11 changes: 7 additions & 4 deletions edx_ace/channel/push_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def deliver(self, message: Message, rendered_message: RenderedPushNotification)
"""
device_tokens = self.get_user_device_tokens(message.recipient.lms_user_id)
if not device_tokens:
LOG.info('Recipient %s has no push token. Skipping push notification.', message.recipient.email_address)
LOG.info(
'Recipient with ID %s has no push token. Skipping push notification.',
message.recipient.lms_user_id
)
return

for token in device_tokens:
Expand All @@ -56,8 +59,8 @@ def send_message(self, message: Message, token: str, rendered_message: RenderedP
Send a push notification to a device by token.
"""
notification_data = {
'title': self.sanitize_html(rendered_message.subject),
'body': self.sanitize_html(rendered_message.body),
'title': self.compress_spaces(rendered_message.title),
'body': self.compress_spaces(rendered_message.body),
'notification_key': token,
**message.context.get('push_notification_extra_context', {}),
}
Expand Down Expand Up @@ -100,7 +103,7 @@ def get_user_device_tokens(user_id: int) -> list:
).values_list('registration_id', flat=True))

@staticmethod
def sanitize_html(html_str: str) -> str:
def compress_spaces(html_str: str) -> str:
"""
Compress spaces and remove newlines to make it easier to author templates.
"""
Expand Down
2 changes: 1 addition & 1 deletion edx_ace/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class RenderedPushNotification:
over an :attr:`.ChannelType.PUSH`.
"""

subject = attr.ib()
title = attr.ib()
body = attr.ib()


Expand Down

0 comments on commit 37d002c

Please sign in to comment.