|
- from odoo import fields, models
-
- class Book(models.Model):
- _name = 'library.book'
- _description = 'Book'
- name = fields.Char('Title', required=True)
- isbn = fields.Char('ISBN')
- active = fields.Boolean('Active?', default=True)
- date_published = fields.Date()
- image = fields.Binary('Cover')
- publisher_id = fields.Many2one('res.partner', string='Publisher')
- author_ids = fields.Many2many('res.partner', string='Authors')
- <form string="Book">
- <header>
- <button name="button_check_isbn" type="object"
- string="Check ISBN" />
- </header>
- <sheet>
- <group name="group_top">
- <group name="group_left">
- <field name="name" />
- <field name="author_ids" widget="many2many_tags" />
- <field name="publisher_id" />
- <field name="date_published" />
- </group>
- <group name="group_right">
- <field name="isbn" />
- <field name="active" />
- <field name="image" widget="image" />
- </group>
- </group>
- </sheet>
- </form>
- def _check_isbn(self):
- self.ensure_one()
- isbn = self.isbn.replace('-', '') # 为保持兼容性 Alan 自行添加
- digits = [int(x) for x in isbn if x.isdigit()]
- if len(digits) == 13:
- ponderations = [1, 3] * 6
- terms = [a * b for a,b in zip(digits[:13], ponderations)]
- remain = sum(terms) % 10
- check = 10 - remain if remain !=0 else 0
- return digits[-1] == check
|