odoo_dev 开发培训作业:图书管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.5KB

  1. from odoo import fields, models
  2. class Book(models.Model):
  3. _name = 'library.book'
  4. _description = 'Book'
  5. name = fields.Char('Title', required=True)
  6. isbn = fields.Char('ISBN')
  7. active = fields.Boolean('Active?', default=True)
  8. date_published = fields.Date()
  9. image = fields.Binary('Cover')
  10. publisher_id = fields.Many2one('res.partner', string='Publisher')
  11. author_ids = fields.Many2many('res.partner', string='Authors')
  12. <form string="Book">
  13. <header>
  14. <button name="button_check_isbn" type="object"
  15. string="Check ISBN" />
  16. </header>
  17. <sheet>
  18. <group name="group_top">
  19. <group name="group_left">
  20. <field name="name" />
  21. <field name="author_ids" widget="many2many_tags" />
  22. <field name="publisher_id" />
  23. <field name="date_published" />
  24. </group>
  25. <group name="group_right">
  26. <field name="isbn" />
  27. <field name="active" />
  28. <field name="image" widget="image" />
  29. </group>
  30. </group>
  31. </sheet>
  32. </form>
  33. def _check_isbn(self):
  34. self.ensure_one()
  35. isbn = self.isbn.replace('-', '') # 为保持兼容性 Alan 自行添加
  36. digits = [int(x) for x in isbn if x.isdigit()]
  37. if len(digits) == 13:
  38. ponderations = [1, 3] * 6
  39. terms = [a * b for a,b in zip(digits[:13], ponderations)]
  40. remain = sum(terms) % 10
  41. check = 10 - remain if remain !=0 else 0
  42. return digits[-1] == check
上海开阖软件有限公司 沪ICP备12045867号-1