Skip to content

Commit

Permalink
Add message functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meomancer committed Oct 24, 2024
1 parent bee5805 commit a5978b5
Show file tree
Hide file tree
Showing 16 changed files with 542 additions and 37 deletions.
2 changes: 1 addition & 1 deletion django_project/core/settings/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
'gap',
'gap_api',
'spw',
'prise',
'message',
'prise',
)
INSTALLED_APPS = INSTALLED_APPS + PROJECT_APPS

Expand Down
27 changes: 27 additions & 0 deletions django_project/message/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding=utf-8
"""
Tomorrow Now GAP.
.. note:: Factory classes for Message
"""
import factory
from factory.django import DjangoModelFactory

from message.models import MessageTemplate
from prise.variables import PriceMessageGroup


class MessageTemplateFactory(DjangoModelFactory):
"""Factory class for MessageTemplate model."""

class Meta: # noqa
model = MessageTemplate

code = factory.Sequence(
lambda n: f'code-{n}'
)
name = factory.Sequence(
lambda n: f'name-{n}'
)
template = factory.Faker('text')
group = PriceMessageGroup.START_SEASON
50 changes: 25 additions & 25 deletions django_project/message/fixtures/1.messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"model": "message.messagetemplate",
"pk": 1,
"fields": {
"code": "beanfly_1",
"code": "prise_beanfly_1",
"name": "feedback",
"type": "Feedback",
"application": "PRISE",
Expand All @@ -18,7 +18,7 @@
"model": "message.messagetemplate",
"pk": 2,
"fields": {
"code": "beanfly_2",
"code": "prise_beanfly_2",
"name": "identify",
"type": "Introduction /Advice /Action",
"application": "PRISE",
Expand All @@ -33,7 +33,7 @@
"model": "message.messagetemplate",
"pk": 3,
"fields": {
"code": "beanfly_3",
"code": "prise_beanfly_3",
"name": "intro2",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -48,7 +48,7 @@
"model": "message.messagetemplate",
"pk": 4,
"fields": {
"code": "beanfly_4",
"code": "prise_beanfly_4",
"name": "intro3",
"type": "PRISE overview",
"application": "PRISE",
Expand All @@ -63,7 +63,7 @@
"model": "message.messagetemplate",
"pk": 5,
"fields": {
"code": "beanfly_5",
"code": "prise_beanfly_5",
"name": "feedback",
"type": "Feedback",
"application": "PRISE",
Expand All @@ -78,7 +78,7 @@
"model": "message.messagetemplate",
"pk": 6,
"fields": {
"code": "beanfly_6",
"code": "prise_beanfly_6",
"name": "monitor",
"type": "Introduction",
"application": "PRISE",
Expand All @@ -93,7 +93,7 @@
"model": "message.messagetemplate",
"pk": 7,
"fields": {
"code": "beanfly_7",
"code": "prise_beanfly_7",
"name": "control 1",
"type": "Context",
"application": "PRISE",
Expand All @@ -108,7 +108,7 @@
"model": "message.messagetemplate",
"pk": 8,
"fields": {
"code": "beanfly_8",
"code": "prise_beanfly_8",
"name": "bestdate",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -123,7 +123,7 @@
"model": "message.messagetemplate",
"pk": 9,
"fields": {
"code": "beanfly_9",
"code": "prise_beanfly_9",
"name": "bestdate",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -138,7 +138,7 @@
"model": "message.messagetemplate",
"pk": 10,
"fields": {
"code": "beanfly_10",
"code": "prise_beanfly_10",
"name": "feedback",
"type": "Warning /Description /Feedback",
"application": "PRISE",
Expand All @@ -153,7 +153,7 @@
"model": "message.messagetemplate",
"pk": 11,
"fields": {
"code": "beanfly_11",
"code": "prise_beanfly_11",
"name": "advice1",
"type": "Warning /Description /Action /Advice1",
"application": "PRISE",
Expand All @@ -168,7 +168,7 @@
"model": "message.messagetemplate",
"pk": 12,
"fields": {
"code": "beanfly_12",
"code": "prise_beanfly_12",
"name": "bestdate",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -183,7 +183,7 @@
"model": "message.messagetemplate",
"pk": 13,
"fields": {
"code": "fall_armyworm_1",
"code": "prise_fall_armyworm_1",
"name": "feedback",
"type": "Feedback",
"application": "PRISE",
Expand All @@ -198,7 +198,7 @@
"model": "message.messagetemplate",
"pk": 14,
"fields": {
"code": "fall_armyworm_2",
"code": "prise_fall_armyworm_2",
"name": "intro1",
"type": "Introduction /Advice /Action",
"application": "PRISE",
Expand All @@ -213,7 +213,7 @@
"model": "message.messagetemplate",
"pk": 15,
"fields": {
"code": "fall_armyworm_3",
"code": "prise_fall_armyworm_3",
"name": "intro2",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -228,7 +228,7 @@
"model": "message.messagetemplate",
"pk": 16,
"fields": {
"code": "fall_armyworm_4",
"code": "prise_fall_armyworm_4",
"name": "feedback",
"type": "Feedback",
"application": "PRISE",
Expand All @@ -243,7 +243,7 @@
"model": "message.messagetemplate",
"pk": 17,
"fields": {
"code": "fall_armyworm_5",
"code": "prise_fall_armyworm_5",
"name": "spray1",
"type": "Introduction",
"application": "PRISE",
Expand All @@ -258,7 +258,7 @@
"model": "message.messagetemplate",
"pk": 18,
"fields": {
"code": "fall_armyworm_6",
"code": "prise_fall_armyworm_6",
"name": "spray2",
"type": "Context",
"application": "PRISE",
Expand All @@ -273,7 +273,7 @@
"model": "message.messagetemplate",
"pk": 19,
"fields": {
"code": "fall_armyworm_7",
"code": "prise_fall_armyworm_7",
"name": "bestdate1",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -288,7 +288,7 @@
"model": "message.messagetemplate",
"pk": 20,
"fields": {
"code": "fall_armyworm_8",
"code": "prise_fall_armyworm_8",
"name": "bestdate2",
"type": "Warning /Description",
"application": "PRISE",
Expand All @@ -303,7 +303,7 @@
"model": "message.messagetemplate",
"pk": 21,
"fields": {
"code": "fall_armyworm_9",
"code": "prise_fall_armyworm_9",
"name": "feedback",
"type": "Feedback",
"application": "PRISE",
Expand All @@ -318,7 +318,7 @@
"model": "message.messagetemplate",
"pk": 22,
"fields": {
"code": "fall_armyworm_10",
"code": "prise_fall_armyworm_10",
"name": "spray1",
"type": "Introduction",
"application": "PRISE",
Expand All @@ -333,7 +333,7 @@
"model": "message.messagetemplate",
"pk": 23,
"fields": {
"code": "fall_armyworm_11",
"code": "prise_fall_armyworm_11",
"name": "spray2",
"type": "Context",
"application": "PRISE",
Expand All @@ -348,7 +348,7 @@
"model": "message.messagetemplate",
"pk": 24,
"fields": {
"code": "fall_armyworm_12",
"code": "prise_fall_armyworm_12",
"name": "advice1",
"type": "Warning /Description /Action /Advice1",
"application": "PRISE",
Expand All @@ -363,7 +363,7 @@
"model": "message.messagetemplate",
"pk": 25,
"fields": {
"code": "fall_armyworm_13",
"code": "prise_fall_armyworm_13",
"name": "bestdate",
"type": "Warning /Description",
"application": "PRISE",
Expand Down
4 changes: 3 additions & 1 deletion django_project/message/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2024-10-24 06:39
# Generated by Django 4.2.7 on 2024-10-24 08:20

from django.db import migrations, models

Expand Down Expand Up @@ -26,7 +26,9 @@ class Migration(migrations.Migration):
('template_sw', models.TextField(help_text='Field for storing messages in translation. Include {{ context_key }} as a placeholder to be replaced with the appropriate context.', null=True)),
],
options={
'db_table': 'message_template',
'ordering': ('code',),
'indexes': [models.Index(fields=['group'], name='message_tem_group_2a17ef_idx'), models.Index(fields=['application'], name='message_tem_applica_7f4863_idx'), models.Index(fields=['name'], name='message_tem_name_311d8e_idx')],
},
),
]
59 changes: 53 additions & 6 deletions django_project/message/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
.. note:: Message models.
"""

from django.conf import settings
from django.db import models
from django.template import Template, Context
from django.utils.translation import gettext_lazy as _

from prise.variables import MessageType
from prise.variables import PriceMessageGroup


class MessageLanguageNotSupportedException(Exception):
"""Message language not supported exception."""

def __init__(self): # noqa
self.message = (
f'The language is not supported. '
f'Choices: {[lang[0] for lang in settings.LANGUAGES]}'' '
)
super().__init__(self.message)


class MessageApplication:
Expand Down Expand Up @@ -37,12 +50,24 @@ class MessageTemplate(models.Model):
max_length=512
)
group = models.CharField(
default=MessageType.START_SEASON,
default=PriceMessageGroup.START_SEASON,
choices=(
(MessageType.START_SEASON, _(MessageType.START_SEASON)),
(MessageType.TIME_TO_ACTION_1, _(MessageType.TIME_TO_ACTION_1)),
(MessageType.TIME_TO_ACTION_2, _(MessageType.TIME_TO_ACTION_2)),
(MessageType.END_SEASON, _(MessageType.END_SEASON)),
(
PriceMessageGroup.START_SEASON,
_(PriceMessageGroup.START_SEASON)
),
(
PriceMessageGroup.TIME_TO_ACTION_1,
_(PriceMessageGroup.TIME_TO_ACTION_1)
),
(
PriceMessageGroup.TIME_TO_ACTION_2,
_(PriceMessageGroup.TIME_TO_ACTION_2)
),
(
PriceMessageGroup.END_SEASON,
_(PriceMessageGroup.END_SEASON)
),
),
max_length=512
)
Expand All @@ -62,7 +87,29 @@ class MessageTemplate(models.Model):

class Meta: # noqa
ordering = ('code',)
db_table = 'message_template'
indexes = [
models.Index(fields=['group']),
models.Index(fields=['application']),
models.Index(fields=['name']),
]

def __str__(self):
"""Return string representation of MessageTemplate."""
return self.code

def get_message(self, context=dict, language_code: str = None):
"""Return template by language code.
Also auto assign the data from context to template.
"""
if not language_code:
language_code = settings.LANGUAGES[0][0]
try:
template = Template(
getattr(self, f'template_{language_code}')
)
context_obj = Context(context)
return template.render(context_obj)
except AttributeError:
raise MessageLanguageNotSupportedException()
Empty file.
Loading

0 comments on commit a5978b5

Please sign in to comment.