odoo_dev 开发培训作业:图书管理系统
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

128 行
4.7KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models
  3. from odoo.exceptions import Warning
  4. from odoo.exceptions import ValidationError
  5. # name和active为特殊字段名
  6. class Book(models.Model):
  7. _name = 'library.book'
  8. _description = "Book"
  9. # String fields
  10. name = fields.Char('Title',
  11. default=None,
  12. index=True,
  13. help='Book cover title',
  14. readonly=False,
  15. required=True,
  16. translate=False,
  17. )
  18. isbn = fields.Char('ISBN')
  19. # active = fields.Boolean('Active?', default=True)
  20. # date_published = fields.Date()
  21. # image = fields.Binary('Cover')
  22. publisher_id = fields.Many2one('res.partner', string='publisher') # 对partner记录的一个引用
  23. author_ids = fields.Many2many('res.partner', string='Authors') # 一本书可以有多个作者,一个作者可以有多本书
  24. _order = 'name, date_published desc'
  25. book_type = fields.Selection(
  26. [('paper', 'Paperback'),
  27. ('hard', 'Hardcover'),
  28. ('electronic', 'Electronic'),
  29. ('other', 'Other')],
  30. 'Type')
  31. notes = fields.Text('Internal Notes')
  32. descr = fields.Html('Description')
  33. # Numeric fields
  34. copies = fields.Integer(default=1)
  35. avg_rating = fields.Float('Average Rating', (3, 2))
  36. price = fields.Monetary('Price', 'currency_id')
  37. currency_id = fields.Many2one('res.currency') # price helper
  38. # Date and time fields
  39. date_published = fields.Date()
  40. # last_borrow_date = fields.Datetime(
  41. # 'Last Borrowed On',
  42. # default=lambda self: fields.Datetime.now()) # 计算当前时间
  43. # 默认值也可以是一个函数引用,或待定义函数名字符串
  44. def _default_last_borrow_date(self):
  45. return fields.Datetime.now()
  46. last_borrow_date = fields.Datetime('Last Borrowed On', default=_default_last_borrow_date)
  47. # Other fields
  48. active = fields.Boolean('Active?', default=True)
  49. image = fields.Binary('Cover')
  50. def _check_isbn(self):
  51. self.ensure_one()
  52. isbn = self.isbn.replace('-', '') # 为保持兼容性 Alan 自行添加
  53. digits = [int(x) for x in isbn if x.isdigit()]
  54. if len(digits) == 13:
  55. ponderations = [1, 3] * 6
  56. terms = [a * b for a, b in zip(digits[:13], ponderations)]
  57. remain = sum(terms) % 10
  58. check = 10 - remain if remain != 0 else 0
  59. return digits[-1] == check
  60. def button_check_isbn(self):
  61. for book in self:
  62. if not book.isbn:
  63. raise Warning('Please provide an ISBN for %s' % book.name)
  64. if book.isbn and not book._check_isbn():
  65. raise Warning('%s is an invalid ISBN' % book.isbn)
  66. return True
  67. # publisher_country_id = fields.Many2one(
  68. # 'res.country', string='Publisher Country',
  69. # compute='_compute_publisher_country'
  70. # )
  71. publisher_country_id = fields.Many2one(
  72. 'res.country', string='Publisher Country',
  73. compute='_compute_publisher_country',
  74. inverse = '_inverse_publisher_country',
  75. search = '_search_publisher_country',
  76. )
  77. # store = False, # 默认不在数据库中存储
  78. #反向(inverse)逻辑
  79. @api.depends('publisher_id.country_id')
  80. def _compute_publisher_country(self):
  81. for book in self:
  82. book.publisher_country_id = book.publisher_id.country_id
  83. # related属性也能达到关联效果
  84. # publisher_country_id = fields.Many2one(
  85. # 'res.country', string='Publisher Country',
  86. # related='publisher_id.country_id',
  87. # )
  88. def _inverse_publisher_country(self):
  89. for book in self:
  90. book.publisher_id.country_id = book.publisher_country_id
  91. # 要为计算字段开启搜索操作,需要实现search方法
  92. def _search_publisher_country(self, operator, value):
  93. return [('publisher_id.country_id', operator, value)]
  94. # 通过标题和出版日期是否相同来确保没有重复的图书;另一条是检查出版日期是否为未出版
  95. _sql_constraints = [
  96. ('library_book_name_date_uq', # 约束唯一标识符
  97. 'UNIQUE (name, date_published)', # 约束 SQL 语法
  98. 'Book title and publication date must be unique'), # 消息
  99. ('library_book_check_date',
  100. 'CHECK (date_published <= current_date)',
  101. 'Publication date must not be in the future.'),
  102. ]
  103. # 防止插入不正确的ISBN号
  104. @api.constrains('isbn')
  105. def _constrain_isbn_valid(self):
  106. for book in self:
  107. if book.isbn and not book._check_isbn():
  108. raise ValidationError('%s is an invalid ISBN' % book.isbn)
上海开阖软件有限公司 沪ICP备12045867号-1