odoo_dev 开发培训作业:图书管理系统
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

103 lines
3.7KB

  1. from odoo import api, fields, models
  2. from odoo.exceptions import Warning
  3. from odoo.exceptions import ValidationError
  4. class Book(models.Model):
  5. _name = 'library.book'
  6. _description = 'Book'
  7. _order = 'name, date_published desc' # 默认以图书名排序,然后按出版日期倒序排
  8. # String fields:
  9. name = fields.Char('Title',
  10. default=None,
  11. index=True,
  12. help='Book Cover Title',
  13. readonly=False,
  14. required=True,
  15. Translate=False,)
  16. isbn = fields.Char('ISBN')
  17. book_type = fields.Selection(
  18. [('paper','Paperback'),
  19. ('hard','Hardcover'),
  20. ('electronic','Electronic'),
  21. ('other','Other')],
  22. 'Type')
  23. notes = fields.Text('Internal Notes')
  24. descr = fields.Html('Description')
  25. # Numberic fields:
  26. copies = fields.Integer(default=1)
  27. avg_rating = fields.Float('Average Rating', (3,2))
  28. price = fields.Monetary('Price', 'currency_id')
  29. currency_id = fields.Many2one('res.currency') # price helper
  30. # Date and time fields
  31. date_published = fields.Date()
  32. last_borrow_date = fields.Datetime('Last Borrowed On',default=lambda self: fields.Datetime.now(),)
  33. # Other fields
  34. active = fields.Boolean('Active?', default=True)
  35. image = fields.Binary('Cover')
  36. # Relational fields
  37. publisher_id = fields.Many2one('res.partner', string='Publisher', context={'default_is_company':True})
  38. author_ids = fields.Many2many('res.partner', string='Authors')
  39. publisher_country_id = fields.Many2one(
  40. 'res.country', string='Publisher Country',
  41. compute='_compute_publisher_country',
  42. # store = False, # 默认不在数据库中存储
  43. inverse='_inverse_publisher_country',
  44. search='_search_publisher_country',
  45. )
  46. # publisher_country_id = fields.Many2one(
  47. # 'res.country', string='Publisher Country',
  48. # related='publisher_id.country_id',
  49. # )
  50. _sql_constraints = [
  51. ('library_book_name_date_uq', # 约束唯一标识符
  52. 'UNIQUE (name, date_published)', # 约束 SQL 语法
  53. 'Book title and publication date must be unique'), # 消息
  54. ('library_book_check_date',
  55. 'CHECK (date_published <= current_date)',
  56. 'Publication date must not be in the future.'),
  57. ]
  58. @api.depends('publisher_id.country_id')
  59. def _compute_publisher_country(self):
  60. for book in self:
  61. book.publisher_country_id = book.publisher_id.country_id
  62. def _inverse_publisher_country(self):
  63. for book in self:
  64. book.publisher_id.country_id = book.publisher_country_id
  65. def _search_publisher_country(self, operator, value):
  66. return [('publisher_id.country_id', operator, value)]
  67. def _check_isbn(self):
  68. self.ensure_one()
  69. isbn = self.isbn.replace('-', '') # 为保持兼容性 自行添加
  70. digits = [int(x) for x in isbn if x.isdigit()]
  71. if len(digits) == 13:
  72. ponderations = [1, 3] * 6
  73. terms = [a * b for a,b in zip(digits[:13], ponderations)]
  74. remain = sum(terms) % 10
  75. check = 10 - remain if remain !=0 else 0
  76. return digits[-1] == check
  77. def button_check_isbn(self):
  78. for book in self:
  79. if not book.isbn:
  80. raise Warning('Please provide an ISBN for %s' % book.name)
  81. if book.isbn and not book._check_isbn():
  82. raise Warning('%s is an invalid ISBN' % book.isbn)
  83. return True
  84. @api.constrains('isbn')
  85. def _constrain_isbn_valid(self):
  86. for book in self:
  87. if book.isbn and not book._check_isbn():
  88. raise ValidationError('%s is an invalid ISBN' % book.isbn)
上海开阖软件有限公司 沪ICP备12045867号-1