~homeworkprod/byceps

1cdb0af44cb0d150869fcffa538240b44be740aa — Jochen Kupperschmidt 2 years ago f8c663d
Obtain website contact e-mail address for user message template from site's e-mail config rather than from brand

This makes the user message feature independent of (the existance of) a brand.
M byceps/blueprints/user_message/views.py => byceps/blueprints/user_message/views.py +1 -1
@@ 56,7 56,7 @@ def create(recipient_id):
                                 _external=True)

    user_message_service.send_message(sender.id, recipient.id, body,
                                      sender_contact_url, g.brand_id, g.site_id)
                                      sender_contact_url, g.site_id)

    flash_success(
        'Deine Nachricht an {} wurde versendet.'.format(recipient.screen_name))

M byceps/services/user_message/service.py => byceps/services/user_message/service.py +12 -29
@@ 16,12 16,9 @@ from attr import attrs
from flask import current_app
from jinja2 import Environment, FileSystemLoader, Template

from ...typing import BrandID, UserID
from ...typing import UserID
from ...util import templating

from ..brand import service as brand_service, \
    settings_service as brand_settings_service
from ..brand.transfer.models import Brand
from ..email import service as email_service
from ..email.transfer.models import Message
from ..site import service as site_service


@@ 37,26 34,22 @@ class MessageTemplateRenderResult:


def send_message(sender_id: UserID, recipient_id: UserID, text: str,
                 sender_contact_url: str, brand_id: BrandID, site_id: SiteID
                ) -> None:
                 sender_contact_url: str, site_id: SiteID) -> None:
    """Create a message and send it."""
    message = create_message(sender_id, recipient_id, text, sender_contact_url,
                             brand_id, site_id)
                             site_id)

    email_service.enqueue_message(message)


def create_message(sender_id: UserID, recipient_id: UserID, text: str,
                   sender_contact_url: str, brand_id: BrandID, site_id: SiteID
                  ) -> Message:
                   sender_contact_url: str, site_id: SiteID) -> Message:
    """Create a message."""
    sender = _get_user(sender_id)
    recipient = _get_user(recipient_id)
    brand = _get_brand(brand_id)
    site = site_service.get_site(site_id)

    return _assemble_message(sender, recipient, text, sender_contact_url, brand,
                             site)
    return _assemble_message(sender, recipient, text, sender_contact_url, site)


def _get_user(user_id: UserID) -> User:


@@ 69,27 62,17 @@ def _get_user(user_id: UserID) -> User:
    return user


def _get_brand(brand_id: BrandID) -> Brand:
    brand = brand_service.find_brand(brand_id)

    if brand is None:
        raise ValueError("Unknown brand ID '{}'".format(brand_id))

    return brand


def _assemble_message(sender_user: User, recipient: User, text: str,
                      sender_contact_url: str, brand: Brand, site: Site
                     ) -> Message:
                      sender_contact_url: str, site: Site) -> Message:
    """Assemble an email message with the rendered template as its body."""
    brand_contact_address = brand_settings_service \
        .find_setting_value(brand.id, 'contact_email_address')
    email_config = email_service.get_config(site.email_config_id)

    website_contact_address = email_config.contact_address

    message_template_render_result = _render_message_template(
        sender_user, recipient, text, sender_contact_url, site,
        brand_contact_address)
        website_contact_address)

    email_config = email_service.get_config(site.email_config_id)
    sender = email_config.sender

    recipient_address = user_service.get_email_address(recipient.id)


@@ 109,7 92,7 @@ def _to_name_and_address_string(name: str, address: str) -> str:

def _render_message_template(sender: User, recipient: User, text: str,
                             sender_contact_url: str, site: Site,
                             brand_contact_address: Optional[str]
                             website_contact_address: Optional[str]
                            ) -> MessageTemplateRenderResult:
    template = _get_template('message.txt')



@@ 119,7 102,7 @@ def _render_message_template(sender: User, recipient: User, text: str,
        'text': text.strip(),
        'sender_contact_url': sender_contact_url,
        'website_server_name': site.server_name,
        'brand_contact_address': brand_contact_address,
        'website_contact_address': website_contact_address,
    }

    module = template.make_module(context)

M byceps/services/user_message/templates/message.txt => byceps/services/user_message/templates/message.txt +2 -2
@@ 17,6 17,6 @@ ACHTUNG: Antworte *nicht* auf diese E-Mail, sondern folge dem Link.

-- 
Diese Mitteilung wurde über die Website {{ website_server_name }} gesendet.
{%- if brand_contact_address %}
Bei Fragen kontaktiere uns bitte per E-Mail an: {{ brand_contact_address }}
{%- if website_contact_address %}
Bei Fragen kontaktiere uns bitte per E-Mail an: {{ website_contact_address }}
{%- endif %}

M tests/blueprints/user_message/test_address_formatting.py => tests/blueprints/user_message/test_address_formatting.py +1 -1
@@ 31,7 31,7 @@ def test_recipient_formatting(application, params):
    user = create_user(screen_name, email_address=email_address)

    message = user_message_service.create_message(user.id, user.id, '', '',
                                                  brand.id, site.id)
                                                  site.id)

    assert message.recipients == [expected]


M tests/blueprints/user_message/test_send.py => tests/blueprints/user_message/test_send.py +5 -5
@@ 20,8 20,8 @@ class SendUserMessageTest(AbstractAppTestCase):
        self.brand = create_brand()
        party = create_party(self.brand.id)

        email_config_id = self.brand.id
        create_email_config(email_config_id, 'noreply@example.com',
        self.email_config_id = self.brand.id
        create_email_config(self.email_config_id, 'noreply@example.com',
                            'ACME Entertainment Convention')

        create_site(party.id, server_name='acme.example.com')


@@ 88,8 88,8 @@ Diese Mitteilung wurde über die Website acme.example.com gesendet.\
    @patch('byceps.email.send')
    def test_send_when_logged_in_with_brand_contact_address(self,
                                                            send_email_mock):
        brand_settings_service.create_setting(
            self.brand.id, 'contact_email_address', 'info@example.com')
        create_email_config(self.email_config_id, 'noreply@example.com',
                            'ACME Entertainment Convention', 'help@example.com')

        sender = create_user('Bob',
            user_id='11d72bab-3646-4199-b96c-e5e4c6f972bc',


@@ 133,7 133,7 @@ Bob

-- 
Diese Mitteilung wurde über die Website acme.example.com gesendet.
Bei Fragen kontaktiere uns bitte per E-Mail an: info@example.com\
Bei Fragen kontaktiere uns bitte per E-Mail an: help@example.com\
'''

        response = self.send_request(recipient.id, text,

M tests/helpers.py => tests/helpers.py +3 -2
@@ 112,8 112,9 @@ def create_site(party_id, *, site_id='acmecon-2014-website', title='Website',


def create_email_config(config_id='acmecon', sender_address='info@example.com',
                        sender_name='ACMECon'):
    email_service.set_config(config_id, sender_address, sender_name=sender_name)
                        sender_name='ACMECon', contact_address=None):
    email_service.set_config(config_id, sender_address, sender_name=sender_name,
                             contact_address=contact_address)


@contextmanager