Skip to content

Commit

Permalink
Still working on import/export tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzbrand committed Jan 15, 2024
1 parent 2a0a84d commit 42dac3c
Show file tree
Hide file tree
Showing 26 changed files with 204 additions and 129 deletions.
15 changes: 13 additions & 2 deletions home/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@
class ContentPagesViewSet(PagesAPIViewSet):
base_serializer_class = ContentPageSerializer
known_query_parameters = PagesAPIViewSet.known_query_parameters.union(
["tag", "trigger", "page", "qa", "whatsapp", "viber", "messenger", "web", "s"]
[
"tag",
"trigger",
"page",
"qa",
"whatsapp",
"viber",
"messenger",
"web",
"s",
"sms",
]
)
pagination_class = PageNumberPagination

Expand Down Expand Up @@ -80,7 +91,7 @@ def get_queryset(self):
elif "whatsapp" in self.request.query_params:
queryset = queryset.filter(enable_whatsapp=True)
elif "sms" in self.request.query_params:
queryset = queryset.filter(enable_sms=True)
queryset = queryset.filter(enable_sms=True)
elif "messenger" in self.request.query_params:
queryset = queryset.filter(enable_messenger=True)
elif "viber" in self.request.query_params:
Expand Down
13 changes: 6 additions & 7 deletions home/content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ def add_sms(row, sms_messages, page=None):
slug=row["slug"],
enable_sms=True,
sms_title=row["sms_title"],
sms_body=get_body(sms_messages, "sms_block"),
sms_body=get_body(sms_messages, "SMS_Message"),
locale=home_page.locale,
)
else:
page.enable_sms = True
page.sms_title = row["sms_title"]
page.sms_body = get_body(sms_messages, "sms_block")
page.sms_body = get_body(sms_messages, "SMS_Message")
return page

def add_messenger(row, messenger_messages, page=None):
Expand Down Expand Up @@ -426,6 +426,7 @@ def get_side_panel_width(ws, column_heading):
and row["web_body"] in ["", None]
and row["whatsapp_body"] in ["", None]
and row["messenger_body"] in ["", None]
and row["sms_body"] in ["", None]
):
cpi = ContentPageIndex.objects.filter(slug=slug).first()
if cpi:
Expand Down Expand Up @@ -473,9 +474,7 @@ def get_side_panel_width(ws, column_heading):
page=contentpage,
variation_messages=variation_messages,
)
contentpage = add_sms(
row=row, sms_messages=sms_messages, page=contentpage
)
contentpage = add_sms(row=row, sms_messages=sms_messages, page=contentpage)
contentpage = add_messenger(
row=row, messenger_messages=messenger_messages, page=contentpage
)
Expand Down Expand Up @@ -917,7 +916,7 @@ class MessageContainer:
def from_platform_body(cls, whatsapp_body, sms_body, messenger_body, viber_body):
whatsapp = []
whatsapp_variation_messages = []
sms=[]
sms = []
messenger = []
viber = []
for whatsapp_msg in whatsapp_body:
Expand Down Expand Up @@ -1247,7 +1246,7 @@ def _set_messages(
if message_index < len(message_container.sms):
new_content_sheet_row.sms_body = message_container.sms[
message_index
].body
].body
if message_index < len(message_container.viber):
new_content_sheet_row.viber_body = message_container.viber[
message_index
Expand Down
10 changes: 7 additions & 3 deletions home/export_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
CPI_CTYPE = ContentPageIndex._meta.verbose_name


MsgBlocks = tuple[WhatsappBlock | None, SMSBlock | None, MessengerBlock | None, ViberBlock | None]
MsgBlocks = tuple[
WhatsappBlock | None, SMSBlock | None, MessengerBlock | None, ViberBlock | None
]


@dataclass
Expand Down Expand Up @@ -106,7 +108,7 @@ def add_message_fields(self, msg_blocks: MsgBlocks) -> None:
if "image" in messenger.value:
self.image_link = messenger.value["image"]
if sms:
self.sms_body = sms.value["message"].strip()
self.sms_body = sms.value["message"].strip()
if whatsapp:
self.whatsapp_body = whatsapp.value["message"].strip()
if "image" in whatsapp.value:
Expand Down Expand Up @@ -201,7 +203,9 @@ def _export_content_page(self, page: ContentPage, structure: str) -> None:
)
self.rows.append(row)
message_bodies = list(
zip_longest(page.whatsapp_body, page.sms_body, page.messenger_body, page.viber_body)
zip_longest(
page.whatsapp_body, page.sms_body, page.messenger_body, page.viber_body
)
)
for msg_blocks in message_bodies:
self._export_row_message(row, msg_blocks)
Expand Down
14 changes: 7 additions & 7 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def create_shadow_content_page_from_row(
# Currently media is exported as a URL, which just has an ID. This doesn't
# actually help us much, as IDs can differ between instances, so we need
# a better way of exporting and importing media here

if row.is_sms_message:
page.sms_title = row.sms_title

Expand Down Expand Up @@ -322,7 +322,7 @@ def add_message_to_shadow_content_page_from_row(
)
if row.is_sms_message:
page.enable_sms = True
page.sms_body.append(ShadowSMSBlock(message=row.sms_body))
page.sms_body.append(ShadowSMSBlock(message=row.sms_body))

if row.is_messenger_message:
page.enable_messenger = True
Expand Down Expand Up @@ -478,9 +478,7 @@ def formatted_whatsapp_body(self) -> list[StructValue]:

@property
def formatted_sms_body(self) -> list[StructValue]:
return [
SMSBlock().to_python(m.wagtail_format) for m in self.sms_body
]
return [SMSBlock().to_python(m.wagtail_format) for m in self.sms_body]

@property
def formatted_messenger_body(self) -> list[StructValue]:
Expand Down Expand Up @@ -528,6 +526,7 @@ def wagtail_format(self) -> dict[str, str | list[dict[str, str]]]:
],
}


@dataclass(slots=True)
class ShadowSMSBlock:
message: str = ""
Expand All @@ -536,6 +535,7 @@ class ShadowSMSBlock:
def wagtail_format(self) -> dict[str, str]:
return {"message": self.message}


@dataclass(slots=True)
class ShadowMessengerBlock:
message: str = ""
Expand Down Expand Up @@ -570,7 +570,7 @@ class ContentRow:
variation_title: dict[str, str] = field(default_factory=dict)
variation_body: str = ""
sms_title: str = ""
sms_body: str = ""
sms_body: str = ""
messenger_title: str = ""
messenger_body: str = ""
viber_title: str = ""
Expand Down Expand Up @@ -632,7 +632,7 @@ def is_whatsapp_message(self) -> bool:
@property
def is_whatsapp_template_message(self) -> bool:
return bool(self.whatsapp_template_name)

@property
def is_sms_message(self) -> bool:
return bool(self.sms_body)
Expand Down
17 changes: 13 additions & 4 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ def clean(self, value):
raise StructBlockValidationError(errors)
return result


class SMSBlock(blocks.StructBlock):
message = blocks.TextBlock(
help_text="each message cannot exceed 160 characters.",
Expand All @@ -309,6 +310,7 @@ class Meta:
icon = "user"
form_classname = "whatsapp-message-block struct-block"


class ViberBlock(blocks.StructBlock):
image = ImageChooserBlock(required=False)
message = blocks.TextBlock(
Expand Down Expand Up @@ -504,16 +506,14 @@ class WhatsAppTemplateCategory(models.TextChoices):
[
(
"SMS_Message",
SMSBlock(
help_text="Each message will be sent with the text"
),
SMSBlock(help_text="Each message will be sent with the text"),
),
],
blank=True,
null=True,
use_json_field=True,
)
# sms panels
# sms panels
sms_panels = [
MultiFieldPanel(
[
Expand Down Expand Up @@ -683,6 +683,8 @@ def save_page_view(self, query_params, platform=None):
if not platform and query_params:
if "whatsapp" in query_params:
platform = "whatsapp"
elif "sms" in query_params:
platform = "sms"
elif "messenger" in query_params:
platform = "messenger"
elif "viber" in query_params:
Expand Down Expand Up @@ -813,6 +815,12 @@ def update_embedding(sender, instance, *args, **kwargs):
content.append(block.value["message"])
body = preprocess_content_for_embedding("/n/n".join(content))
embedding["whatsapp"] = {"values": [float(i) for i in model.encode(body)]}
if instance.enable_sms:
content = []
for block in instance.sms_body:
content.append(block.value["message"])
body = preprocess_content_for_embedding("/n/n".join(content))
embedding["sms"] = {"values": [float(i) for i in model.encode(body)]}
if instance.enable_messenger:
content = []
for block in instance.messenger_body:
Expand Down Expand Up @@ -954,6 +962,7 @@ class PageView(models.Model):
platform = models.CharField(
choices=[
("WHATSAPP", "whatsapp"),
("SMS", "sms"),
("VIBER", "viber"),
("MESSENGER", "messenger"),
("WEB", "web"),
Expand Down
12 changes: 6 additions & 6 deletions home/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def to_representation(self, page):
return page.whatsapp_title
if "sms" in request.GET and page.enable_sms is True:
if page.sms_title:
return page.sms_title
return page.sms_title
elif "messenger" in request.GET and page.enable_messenger is True:
if page.messenger_title:
return page.messenger_title
Expand Down Expand Up @@ -77,7 +77,7 @@ def has_previous_message(message_index, content_page, platform):
if platform == "whatsapp":
messages_length = len(content_page.whatsapp_body._raw_data) - 1
elif platform == "sms":
messages_length = len(content_page.sms_body._raw_data) - 1
messages_length = len(content_page.sms_body._raw_data) - 1
elif platform == "viber":
messages_length = len(content_page.viber_body._raw_data) - 1
elif platform == "messenger":
Expand Down Expand Up @@ -189,7 +189,7 @@ def to_representation(self, page):
]
)
except IndexError:
raise ValidationError("The requested message does not exist")
raise ValidationError("The requested message does not exist")
elif "messenger" in request.GET and (
page.enable_messenger is True
or ("qa" in request.GET and request.GET["qa"] == "True")
Expand Down Expand Up @@ -259,7 +259,7 @@ def to_representation(self, page):
title = related_page.whatsapp_title
elif "sms" in request.GET and related_page.enable_sms is True:
if related_page.sms_title:
title = related_page.sms_title
title = related_page.sms_title
elif "messenger" in request.GET and related_page.enable_messenger is True:
if related_page.messenger_title:
title = related_page.messenger_title
Expand Down Expand Up @@ -330,7 +330,7 @@ def to_representation(self, instance):
title = page.whatsapp_title
elif "sms" in request.GET and page.enable_sms is True:
if page.sms_title:
title = page.sms_title
title = page.sms_title
elif "messenger" in request.GET and page.enable_messenger is True:
if page.messenger_title:
title = page.messenger_title
Expand Down Expand Up @@ -369,7 +369,7 @@ def to_representation(self, instance):
title = page.whatsapp_title
elif "sms" in request.GET and page.enable_sms is True:
if page.sms_title:
title = page.sms_title
title = page.sms_title
elif "messenger" in request.GET and page.enable_messenger is True:
if page.messenger_title:
title = page.messenger_title
Expand Down
71 changes: 36 additions & 35 deletions home/tests/content2.csv
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,variation_title,variation_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,locale,next_prompt,buttons,image_link,doc_link,media_link,related_pages,example_values
Menu 1,0,,main-menu,,Main Menu,,,,,,,,,,,a0b85075-d01b-46bf-8997-8591e87ba171,,,,English,,,,,,,[]
Sub 1.1,1,,main-menu-first-time-user,Main Menu,main menu first time user,,,main menu first time user," *Welcome to HealthAlert* 🌍

This is a messaging service created by the _*World Health Organization*_ *(WHO)* that provides information on COVID-19 as well as emergency resources for disease outbreaks, natural, and man-made disasters.

_You can return to this main menu at any time by replying *0*_ 🏠

Choose what you'd like to know more about by tapping a button below ⬇️",,,main menu first time user,"Welcome to HealthAlert 🌍

This is a messaging service created by the World Health Organization (WHO) that provides information on COVID-19 as well as emergency resources for disease outbreaks, natural, and man-made disasters.

You can return to this main menu at any time by replying 🏠

Choose what you'd like to know more about by tapping a button below ⬇️",,,5892bccd-8025-419d-9a8e-a6a37b755dbf,menu,"Self-help🌬️, Settings⚙️, Health Info🏥",Main menu🏠,English,,[],,,,,[]
Sub 1.1.1,1,,health-info,main menu first time user,health info,,,health info,"*Health information* 🏥

Get information and advice from WHO by tapping on a button below ⬇️",,,health info,"*Health information* 🏥

Get information and advice from WHO by tapping on a button below ⬇️",,,c9d6309e-173f-4c1d-bbaf-440b1fd4415f,health_info,,,English,,[],,,,,[]
Sub 1.1.2,1,,self-help,main menu first time user,self-help,,,self-help,"*Self-help programs* 🌬️

Reply with a number to take part in a *free* self-help program created by WHO.

1. Quit tobacco 🚭
_Stop smoking with the help of a guided, 42-day program._
2. Manage your stress 🧘🏽‍♀️
_Learn how to cope with stress and improve your wellbeing._",,,self-help,"*Self-help programs* 🌬️

Reply with a number to take part in a *free* self-help program created by WHO.

1. Quit tobacco 🚭
_Stop smoking with the help of a guided, 42-day program._
2. Manage your stress 🧘🏽‍♀️
_Learn how to cope with stress and improve your wellbeing._",,,3e5d77f7-4d34-430d-aad7-d9ca01f79732,self_help,,,English,,[],,,,,[]
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,variation_title,variation_body,sms_title,sms_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,locale,next_prompt,buttons,image_link,doc_link,media_link,related_pages,example_values
Menu 1,0,,main-menu,,Main Menu,,,,,,,,,,,,,a0b85075-d01b-46bf-8997-8591e87ba171,,,,English,,,,,,,[]
Sub 1.1,1,,main-menu-first-time-user,Main Menu,main menu first time user,,,main menu first time user," *Welcome to HealthAlert* 🌍

This is a messaging service created by the _*World Health Organization*_ *(WHO)* that provides information on COVID-19 as well as emergency resources for disease outbreaks, natural, and man-made disasters.

_You can return to this main menu at any time by replying *0*_ 🏠

Choose what you'd like to know more about by tapping a button below ⬇️",,,,,main menu first time user,"Welcome to HealthAlert 🌍

This is a messaging service created by the World Health Organization (WHO) that provides information on COVID-19 as well as emergency resources for disease outbreaks, natural, and man-made disasters.

You can return to this main menu at any time by replying 🏠

Choose what you'd like to know more about by tapping a button below ⬇️",,,5892bccd-8025-419d-9a8e-a6a37b755dbf,menu,"Self-help🌬️, Settings⚙️, Health Info🏥",Main menu🏠,English,,[],,,,,[]
Sub 1.1.1,1,,health-info,main menu first time user,health info,,,health info,"*Health information* 🏥

Get information and advice from WHO by tapping on a button below ⬇️",,,,,health info,"*Health information* 🏥

Get information and advice from WHO by tapping on a button below ⬇️",,,c9d6309e-173f-4c1d-bbaf-440b1fd4415f,health_info,,,English,,[],,,,,[]
Sub 1.1.2,1,,self-help,main menu first time user,self-help,,,self-help,"*Self-help programs* 🌬️

Reply with a number to take part in a *free* self-help program created by WHO.

1. Quit tobacco 🚭
_Stop smoking with the help of a guided, 42-day program._
2. Manage your stress 🧘🏽‍♀️
_Learn how to cope with stress and improve your wellbeing._",,,,,self-help,"*Self-help programs* 🌬️

Reply with a number to take part in a *free* self-help program created by WHO.

1. Quit tobacco 🚭
_Stop smoking with the help of a guided, 42-day program._
2. Manage your stress 🧘🏽‍♀️
_Learn how to cope with stress and improve your wellbeing._",,,3e5d77f7-4d34-430d-aad7-d9ca01f79732,self_help,,,English,,[],,,,,[]

Loading

0 comments on commit 42dac3c

Please sign in to comment.