Skip to content

Commit

Permalink
#717 SMS text ni vredu formatiran (#139)
Browse files Browse the repository at this point in the history
* better formatting of sms notification

* pr fix
  • Loading branch information
KlemenSpruk authored Oct 9, 2023
1 parent 4624e44 commit aab2624
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re
from abc import ABC, abstractmethod
from html import unescape
from typing import Union

import swapper
from django.urls import reverse
from django.utils.html import strip_tags

from django_project_base.constants import SEND_NOTIFICATION_SMS
Expand Down Expand Up @@ -58,17 +60,20 @@ def get_message(self, notification: DjangoProjectBaseNotification) -> Union[dict

def get_send_notification_sms_text(self, notification: DjangoProjectBaseNotification, host_url: str) -> str:
if notification.send_notification_sms:
template: str = swapper.load_model("django_project_base", "ProjectSettings").objects.get(
name=SEND_NOTIFICATION_SMS, project__slug=notification.project_slug).python_value
template: str = (
swapper.load_model("django_project_base", "ProjectSettings")
.objects.get(name=SEND_NOTIFICATION_SMS, project__slug=notification.project_slug)
.python_value
)
return template.replace(
"__LINK__",
f"{host_url}notification/{str(notification.pk)}/info"
f"{'/' if getattr(self.settings, 'APPEND_SLASH', False) else ''}")
f"{host_url.rstrip('/')}"
f"{reverse('notification-notification-login', kwargs=dict(pk=str(notification.pk)))}",
)
return ""

@abstractmethod
def _get_sms_message(self, notification: DjangoProjectBaseNotification) -> Union[dict, str]:

if notification.send_notification_sms:
return notification.send_notification_sms_text

Expand All @@ -77,9 +82,14 @@ def _get_sms_message(self, notification: DjangoProjectBaseNotification) -> Union
if notification.message.subject:
message += "\n\n"

message += notification.message.body

message += (
notification.message.body.replace("&nbsp;</p>", "\n")
.replace("</p>", "\n")
.replace("\n&nbsp;", "\n")
.replace("&nbsp;", "\n")
)
text_only = re.sub("[ \t]+", " ", strip_tags(message))
# Strip single spaces in the beginning of each line
message = text_only.replace("\n ", "\n").replace("\n", "\r\n").strip()
return message
message = text_only.replace("\n ", "\n")
message = message.replace("\n", "\r\n")
return unescape(message)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Union
from typing import List

from django.conf import settings

Expand All @@ -16,9 +16,6 @@ class SmsChannel(Channel):

provider_setting_name = "SMS_PROVIDER"

def get_message(self, notification: DjangoProjectBaseNotification) -> Union[dict, str]:
return self._get_sms_message(notification)

def get_recipients(self, notification: DjangoProjectBaseNotification, unique_identifier=""):
return list(set(super().get_recipients(notification, unique_identifier="phone_number")))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def forwards_func(apps, schema_editor):
project=project,
name=SEND_NOTIFICATION_SMS,
defaults=dict(
description="Text for notification detail view. __LINK__ is url placeholder.",
description=gettext("Text for notification detail view. __LINK__ is url placeholder."),
value=gettext("You received a notification. Clik link to view it: __LINK__"),
value_type="char",
),
Expand Down
4 changes: 2 additions & 2 deletions django_project_base/notifications/rest/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import pytz
import swapper
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.db.models import ForeignKey, QuerySet
from django.http import Http404, HttpResponse
from django.shortcuts import render
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema
from dynamicforms import fields
Expand Down Expand Up @@ -350,7 +350,7 @@ def notification_login(self, request, pk=None) -> HttpResponse:
"notification_login.html",
dict(
identifier=pk,
url=f"/notification/info-view{'/' if getattr(settings, 'APPEND_SLASH', False) else ''}",
url=reverse("notification-notification-view"),
title=swapper.load_model("django_project_base", "Project")
.objects.get(slug=DjangoProjectBaseNotification.objects.get(pk=pk).project_slug)
.name,
Expand Down

0 comments on commit aab2624

Please sign in to comment.