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.

91 lignes
3.1KB

  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. @api.depends('publisher_id.country_id')
  6. def _compute_publisher_country(self):
  7. for book in self:
  8. book.publisher_country_id = book.publisher_id.country_id
  9. def _inverse_publisher_country(self):
  10. for book in self:
  11. book.publisher_id.country_id = book.publisher_country_id
  12. def _search_publisher_country(self, operator, value):
  13. return [('publisher_id.country_id', operator, value)]
  14. @api.constrains('isbn')
  15. def _constrain_isbn_valid(self):
  16. for book in self:
  17. if book.isbn and not book._check_isbn():
  18. raise ValidationError('%s is an invalid ISBN' % book.isbn)
  19. _name = 'library.book'
  20. _description = 'Book'
  21. _sql_constraints = [
  22. ('library_book_name_date_uq','UNIQUE (name, date_published)', 'Book title and publication date must be unique'),
  23. ('library_book_check_date','CHECK (date_published <= current_date)', 'Publication date must not be in the future.')
  24. ]
  25. name = fields.Char('Title', required=True)
  26. isbn = fields.Char('ISBN')
  27. book_type = fields.Selection([
  28. ('paper', 'Paperback'),
  29. ('hard', 'Hardcover'),
  30. ('electronic', 'Electronic'),
  31. ('other', 'Other')
  32. ],'Type')
  33. notes = fields.Text()
  34. descr = fields.Html('Description')
  35. # Numeric fields:
  36. copies = fields.Integer(default=1)
  37. avg_rating = fields.Float('Average Rating', (3,2))
  38. price = fields.Monetary('Price', 'currency_id')
  39. currency_id = fields.Many2one('res.currency')
  40. # Date and time fields:
  41. date_published = fields.Date()
  42. last_borrow_date = fields.Datetime(
  43. 'Last Borrowed On',
  44. default = lambda self: fields.Datetime.now()
  45. )
  46. # Other fields:
  47. active = fields.Boolean('Active?', default=True)
  48. image = fields.Binary('Cover')
  49. # Relational fields:
  50. publisher_id = fields.Many2one('res.partner', string='Publisher')
  51. author_ids = fields.Many2many('res.partner', string='Authors')
  52. # Compute fields:
  53. publisher_country_id = fields.Many2one(
  54. 'res.country',
  55. string='Publisher Country',
  56. compute='_compute_publisher_country',
  57. inverse='_inverse_publisher_country',
  58. search='_search_publisher_country'
  59. )
  60. def _check_isbn(self):
  61. self.ensure_one()
  62. isbn=self.isbn.replace('-','')
  63. digits=[int(x) for x in isbn if x.isdigit()]
  64. if len(digits) == 13:
  65. ponderations = [1 , 3] * 6
  66. terms = [a * b for a,b in zip(digits[:13], ponderations)]
  67. remain = sum(terms) % 10
  68. check = 10 - remain if remain != 0 else 0
  69. return digits[-1] == check
  70. def button_check_isbn(self):
  71. for book in self:
  72. if not book.isbn:
  73. raise Warning('Please provide an ISBN for %s' % book.name)
  74. if book.isbn and not book._check_isbn():
  75. raise Warning('%s is an invalid ISBN' % book.isbn)
  76. return True
上海开阖软件有限公司 沪ICP备12045867号-1