odoo_dev 开发培训作业:图书管理系统
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

33 lignes
1.2KB

  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. def _check_isbn(self):
  13. self.ensure_one()
  14. isbn = self.isbn.replace('-', '') # 为保持兼容性 Alan 自行添加
  15. digits = [int(x) for x in isbn if x.isdigit()]
  16. if len(digits) == 13:
  17. ponderations = [1, 3] * 6
  18. terms = [a * b for a,b in zip(digits[:13], ponderations)]
  19. remain = sum(terms) % 10
  20. check = 10 - remain if remain !=0 else 0
  21. return digits[-1] == check
  22. def button_check_isbn(self):
  23. for book in self:
  24. if not book.isbn:
  25. raise Warning('Please provide an ISBN for %s' % book.name)
  26. if book.isbn and not book._check_isbn():
  27. raise Warning('%s is an invalid ISBN' % book.isbn)
  28. return True
上海开阖软件有限公司 沪ICP备12045867号-1