iT邦幫忙

2023 iThome 鐵人賽

DAY 21
0
Odoo

Odoo 魔法學院: 一步一腳印帶你成為客製化大師系列 第 21

[Day21] 實作催繳書籍功能 + 基礎功能驗證

  • 分享至 

  • xImage
  •  

今天先來將之前繼承mail的功能,附加到checkout model上面。
先到library_checkout/manifest.py,指定要的model

"depends": ["library_member", "mail"],

library_checkout/models/library_checkout.py

class Checkout(models.Model):
    _inherit = ["mail.thread", "mail.activity.mixin"]

library_checkout/views/checkout_view.xml

        <div class="oe_chatter">
          <field name="message_follower_ids" widget="mail_followers" />
          <field name="activity_ids" widget="mail_activity"/>
          <field name="message_ids" widget="mail_thread" />
        </div>      

這樣之後checkout model就有基本的傳訊息功能。
接下來要做wizard model,向借書的人通知,例如可以選擇過期尚未還書的人,向他們發送一條訊息,請求歸還書籍。
library_checkout/init.py

from . import wizard

wizard/init.py

from . import checkout_mass_message

wizard/checkout_mass_message.py

class CheckoutMassMessage(models.TransientModel):
    _name = "library.checkout.massmessage"
    _description = "Send Message to Borrowers"
    checkout_ids = fields.Many2many(
        "library.checkout",
        string="Checkouts",
    )
    message_subject = fields.Char()
    message_body = fields.Html()

還是要設置安全性,security/ir.model.access.csv

checkout_massmessage_user,Checkout Mass Message User,model_library_checkout_massmessage,library_app.library_group_user,1,1,1,1

再來就是介面,wizard/checkout_mass_message_wizard_view.xml

<odoo>

  <record id="view_form_checkout_message" model="ir.ui.view">
    <field name="name">Library Checkout Mass Message Wizard</field>
    <field name="model">library.checkout.massmessage</field>
    <field name="arch" type="xml">

      <form>
        <group>
          <field name="message_subject" />
          <field name="message_body" />
          <field name="checkout_ids" />
        </group>
        <footer>
          <button type="object"
            name="button_send"
            string="Send Messages" />
          <button special="cancel" 
            string="Cancel" 
            class="btn-secondary" />
        </footer>
      </form>

    </field>
  </record>

  <record id="action_checkout_message"
          model="ir.actions.act_window">
    <field name="name">Send Messages</field>
    <field name="res_model">library.checkout.massmessage</field>
    <field name="view_mode">form</field>
    <field name="binding_model_id"
           ref="model_library_checkout" />
    <field name="binding_view_types">form,list</field>
    <field name="target">new</field>
  </record>

</odoo>

也要記得到data添加library_checkout/manifest.py

"wizard/checkout_mass_message_wizard_view.xml",

到checkout裡選擇任意訂單,裡面Action會有一個選項Send Messages:
https://ithelp.ithome.com.tw/upload/images/20231006/201633263m57aMAO3B.png

不過按鈕還沒有作用,到wizard/checkout_mass_message.py

    def button_send(self):
        self.ensure_one()
        if not self.checkout_ids:
            raise exceptions.UserError(
                "No Checkouts were selected."
            )
        if not self.message_body:
            raise exceptions.UserError(
                "A message body is required"
            )
        for checkout in self.checkout_ids:
            checkout.message_post(
                body=self.message_body,
                subject=self.message_subject,
                subtype_xmlid='mail.mt_comment',
            )
         return True

這樣我們把checkout model的功能都寫完了,最後就來做unit tests:
測試的資料就不用附加到__init__.py,直接新增tests/init.py

from . import test_checkout_mass_message

再來也要有基本框架,tests/test_checkout_mass_message.py

from odoo import exceptions
from odoo.tests import common
class TestWizard(common.SingleTransactionCase):
    def setUp(self, *args, **kwargs):
        super(TestWizard, self).setUp(*args, **kwargs)
    def test_01_button_send(self):
     """Send button should create messages on Checkouts"""

接下來更新要測試的model,並把--test-enable新增至odoo

 (env15) $ odoo -c library.conf --test-enable -u library_checkout --stop-after-init

應該會找到類似的訊息

INFO library odoo.modules.module: odoo.addons.library_checkout.tests.test_checkout_mass_message running tests.

然後先設置一個測試資料,在tests/test_checkout_mass_message.py:

class TestWizard(common.SingleTransactionCase):

    def setUp(self, *args, **kwargs):
        super(TestWizard, self).setUp(*args, **kwargs)
        # Setup test data
        admin_user = self.env.ref('base.user_admin')
        self.Checkout = self.env["library.checkout"]\
            .with_user(admin_user)
        self.Wizard = self.env['library.checkout.massmessage']\
            .with_user(admin_user)
        a_member = self.env['library.member']\
            .create({'partner_id': admin_user.partner_id.id})
        self.checkout0 = self.Checkout\
            .create({'member_id': a_member.id})   

Button_send()將一則訊息加入到訊息歷史記錄中,並測試訊息是否多一條

 def test_01_button_send(self):
        """Send button should create messages on Checkouts"""
        count_before = len(self.checkout0.message_ids)
        Wizard0 = self.Wizard\
            .with_context(active_ids=self.checkout0.ids)
        wizard0 = Wizard0.create({
            "message_subject": "Hello",
            "message_body": "This is a message.",
        })
        wizard0.button_send()
        count_after = len(self.checkout0.message_ids)
        self.assertEqual(
            count_before + 1,
            count_after,
            "Expected one additional message in the Checkout.",
        )

這樣就能判斷程式是否正確。


上一篇
[Day20] 催繳書籍(前置工作)
下一篇
[Day22] 第一次用外部 API 就上手:最簡單的 Python 和 XML-RPC 結合
系列文
Odoo 魔法學院: 一步一腳印帶你成為客製化大師30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言