odoo_dev 开发培训作业:图书管理系统
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

71 lines
2.6KB

  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. name = fields.Char('Title', required=True)
  7. isbn = fields.Char('ISBN')
  8. book_type = fields.Selection(
  9. [('paper', 'Paperback'),
  10. ('hard', 'Hardcover'),
  11. ('electronic', 'Electronic'),
  12. ('other', 'Other')],
  13. 'Type')
  14. notes = fields.Text('Internal Notes')
  15. descr = fields.Html('Description')
  16. # Numeric fields:
  17. copies = fields.Integer(default=1)
  18. avg_rating = fields.Float('Average Rating', (3,2))
  19. price = fields.Monetary('Price', 'currency_id')
  20. currency_id = fields.Many2one('res.currency') # price helper
  21. # Date and time fields
  22. date_published = fields.Date()
  23. last_borrow_date = fields.Datetime(
  24. 'Last Borrowed On',
  25. default=lambda self: fields.Datetime.now())
  26. # Other fields
  27. active = fields.Boolean('Active?', default=True)
  28. image = fields.Binary('Image')
  29. publisher_id = fields.Many2one('res.partner', string='Publisher')
  30. author_ids = fields.Many2many('res.partner', string='Authors')
  31. def _check_isbn(self):
  32. self.ensure_one()
  33. isbn = self.isbn.replace('-', '') # 为保持兼容性 Alan 自行添加
  34. digits = [int(x) for x in isbn if x.isdigit()]
  35. if len(digits) == 13:
  36. ponderations = [1, 3] * 6
  37. terms = [a * b for a,b in zip(digits[:13], ponderations)]
  38. remain = sum(terms) % 10
  39. check = 10 - remain if remain !=0 else 0
  40. return digits[-1] == check
  41. def button_check_isbn(self):
  42. for book in self:
  43. if not book.isbn:
  44. book.isbn = 'this is hard coded'
  45. # raise Warning('Please provide an ISBN for %s' % book.name)
  46. # if book.isbn and not book._check_isbn():
  47. # raise Warning('%s is an invalid ISBN' % book.isbn)
  48. return True
  49. publisher_country_id = fields.Many2one(
  50. 'res.country', string='Publisher Country',
  51. compute='_compute_publisher_country',
  52. inverse='_inverse_publisher_country',
  53. search='_search_publisher_country',
  54. )
  55. @api.depends('publisher_id.country_id')
  56. def _compute_publisher_country(self):
  57. for book in self:
  58. book.publisher_country_id = book.publisher_id.country_id
  59. def _inverse_publisher_country(self):
  60. for book in self:
  61. book.publisher_id.country_id = book.publisher_country_id
  62. def _search_publisher_country(self, operator, value):
  63. return [('publisher_id.country_id', operator, value)]
上海开阖软件有限公司 沪ICP备12045867号-1