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.

84 line
2.9KB

  1. from odoo import api, fields, models
  2. from odoo.exceptions import Warning
  3. class Book(models.Model):
  4. _name = 'library.book'
  5. _description = 'Book'
  6. # String fields
  7. name = fields.Char('Title', required=True)
  8. isbn = fields.Char('ISBN')
  9. book_type = fields.Selection(
  10. [('paper', 'Paperback'),
  11. ('hard', 'Hardcover'),
  12. ('electronic', 'Electronic'),
  13. ('other', 'Other')],
  14. 'Type')
  15. notes = fields.Text('Internal Notes')
  16. descr = fields.Html('Description')
  17. # Numeric fields:
  18. copies = fields.Integer(default=1)
  19. avg_rating = fields.Float('Average Rating', (3,2))
  20. price = fields.Monetary('Price', 'currency_id')
  21. currency_id = fields.Many2one('res.currency') # price helper
  22. # Date adn time fields
  23. date_published = fields.Date()
  24. last_borrow_date = fields.Datetime(
  25. 'Last Borrowed On',
  26. default=lambda self: fields.Datetime.now()
  27. )
  28. # Other fields
  29. active = fields.Boolean('Active?', default=True)
  30. image = fields.Binary('Cover')
  31. # Relational Fields
  32. active = fields.Boolean('Active?', default=True)
  33. date_published = fields.Date()
  34. image = fields.Binary('Cover')
  35. publisher_id = fields.Many2one('res.partner', string='Publisher')
  36. author_ids = fields.Many2many('res.partner', string='Authors')
  37. # 添加业务逻辑
  38. def _check_isbn(self):
  39. self.ensure_one()
  40. isbn = self.isbn.replace('-', '') # 为保持兼容性Alan 自动添加
  41. digits = [int(x) for x in isbn if x.isdigit()]
  42. if len(digits) == 13:
  43. ponderations = [1, 3] * 6
  44. terms = [a * b for a,b in zip(digits[:13], ponderations)]
  45. remain = sum(terms) % 10
  46. check = 10 - remain if remain !=0 else 0
  47. return digits[-1] == check
  48. def button_check_isbn(self):
  49. for book in self:
  50. if not book.isbn:
  51. raise Warning('Please provide an ISBN for %s' % book.name)
  52. if book.isbn and not book._check_isbn():
  53. raise Warning('%s is an invalid ISBN' % book.isbn)
  54. return True
  55. publisher_country_id = fields.Many2one(
  56. 'res.country', string='Publisher Country',
  57. related='publisher_id.country_id',
  58. compute='_compute_publisher_country',
  59. # store = False, # 默认不在数据库中存储
  60. inverse='_inverse_publisher_country',
  61. search='_search_publisher_country',
  62. )
  63. @api.depends('publisher_id.country_id')
  64. def _compute_publisher_country(self):
  65. for book in self:
  66. book.publisher_country_id = book.publisher_country_id
  67. def _search_publisher_country(self, operator, value):
  68. return [('publisher_id.country_id', operator, value)]
  69. @api.constrains('isbn')
  70. def _constrain_isbn_valid(self):
  71. for book in self:
  72. if book.isbn and not book._check_isbn():
  73. raise ValidationError('%s is an invalid ISBN' % book.isbn)
上海开阖软件有限公司 沪ICP备12045867号-1