File: common.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (135 lines) | stat: -rw-r--r-- 5,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import base64
from contextlib import contextmanager
from unittest.mock import patch

from odoo.tests import BaseCase, TransactionCase
from odoo.addons.base.models.ir_actions_report import IrActionsReport
from odoo.addons.mail.tests.common import mail_new_test_user


VALID_JPEG = base64.b64decode('/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=')


def mock_image_render(func):
    def patched(self, *args, **kwargs):
        with self.mock_image_renderer(collect_params=False):
            return func(self, *args, **kwargs)
    return patched


class MockImageRender(BaseCase):
    @contextmanager
    def mock_image_renderer(self, collect_params=True):
        self._wkhtmltoimage_bodies = []

        def _ir_actions_report_build_run_wkhtmltoimage(model, bodies, width, height, image_format="jpg"):
            if collect_params:
                self._wkhtmltoimage_bodies.extend(bodies)
            return [VALID_JPEG] * len(bodies)

        with patch.object(IrActionsReport, '_run_wkhtmltoimage', _ir_actions_report_build_run_wkhtmltoimage):
            yield


class MarketingCardCommon(TransactionCase, MockImageRender):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        cls.company = cls.env['res.company'].create({
            'country_id': cls.env.ref("base.be").id,
            'email': 'your.company@example.',
            'name': 'YourTestCompany',
        })
        cls.marketing_card_manager = mail_new_test_user(
            cls.env,
            company_id=cls.company.id,
            email='manager.marketing.card@example.com',
            login='marketing_card_manager',
            groups='marketing_card.marketing_card_group_manager',
            name='Marketing Card Manager',
        )
        cls.marketing_card_user = mail_new_test_user(
            cls.env,
            company_id=cls.company.id,
            email='user.marketing.card@example.com',
            login='marketing_card_user',
            groups='marketing_card.marketing_card_group_user',
            name='Marketing Card User',
        )
        cls.marketing_card_user_2 = cls.marketing_card_user.copy({
            'email': 'user2.marketing.card@example.com',
            'login': 'marketing_card_user_2',
            'name': 'Marketing Card User 2',
        })
        cls.system_admin = mail_new_test_user(
            cls.env,
            company_id=cls.company.id,
            email='system.marketing.card@example.com',
            login='marketing_card_system_admin',
            groups='base.group_system,marketing_card.marketing_card_group_manager',
            name='System Admin',
        )

        cls.partners = cls.env['res.partner'].create([
            {'name': 'John', 'email': 'john93@trombino.scope'},
            {'name': 'Bob', 'email': 'bob@justbob.me',
             'phone': '+32 123 446 789', 'image_1920': base64.b64encode(VALID_JPEG),
             },
        ])

        cls.card_template = cls.env['card.template'].create({
            'name': 'Test Template',
            'body': """
<html>
    <t t-set="values" t-value="card_campaign._get_card_element_values(object)"/>
    <head>
        <style>
            p { margin: 1px };
            body { width: 100%; height: 100%; };
        </style>
    </head>
    <body>
    <div id="body" t-attf-style="background-image: url('data:image/png;base64,{{card_campaign.content_background or card_campaign.card_template_id.default_background}}');">
                <span id="header" t-out="values['header']" t-att-style="'color: %s;' % card_campaign.content_header_color"/>
                <span id="subheader" t-out="values['sub_header']" t-att-style="'color: %s;' % card_campaign.content_sub_header_color"/>
                <span id="button" t-out="card_campaign.content_button">Button</span>
                <span id="section" t-out="values['section']"/>
                <span id="sub_section1" t-out="values['sub_section1']"/>
                <span id="sub_section2" t-out="values['sub_section2']"/>
                <img id="image1" t-if="values['image1']" t-attf-src="data:image/png;base64,{{values['image1']}}"/>
                <img id="image2" t-if="values['image2']" t-attf-src="data:image/png;base64,{{values['image2']}}"/>
    </div>
    </body>
</html>
            """,
        })

        cls.campaign = cls.env['card.campaign'].with_user(cls.marketing_card_user).create({
            'name': 'Test Campaign',
            'card_template_id': cls.card_template.id,
            'post_suggestion': 'Come see my show!',
            'preview_record_ref': f'{cls.partners._name},{cls.partners[0].id}',
            'reward_message': """<p>Thanks for sharing!</p>""",
            'reward_target_url': f"{cls.env['card.campaign'].get_base_url()}/share-rewards/2039-sharer-badge/",
            'target_url': cls.env['card.campaign'].get_base_url(),
            'content_section': 'Contact',
            'content_sub_header_dyn': True,
            'content_sub_header_path': 'name',
            'content_sub_section1_dyn': True,
            'content_sub_section1_path': 'email',
            'content_sub_section2_dyn': True,
            'content_sub_section2_path': 'phone',
            'content_image1_path': 'user_ids.image_256',
            'content_image2_path': 'image_256',
        })
        cls.static_campaign = cls.env['card.campaign'].with_user(cls.marketing_card_user).create({
            'name': 'Simple Campaign',
            'card_template_id': cls.card_template.id,
            'post_suggestion': 'Come see my show!',
            'preview_record_ref': f'{cls.partners._name},{cls.partners[0].id}',
            'reward_message': """<p>Thanks for sharing!</p>""",
            'reward_target_url': f"{cls.env['card.campaign'].get_base_url()}/share-rewards/2039-sharer-badge/",
            'target_url': cls.env['card.campaign'].get_base_url(),
        })