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.

80 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Rémy Taymans <remytaymans@gmail.com>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. class Document(models.Model):
  6. _name = 'easy_my_coop.document'
  7. _description = "Document"
  8. _order = 'document_date desc, name'
  9. name = fields.Char("Name", required=True)
  10. description = fields.Text("Description")
  11. document = fields.Binary('Document', attachment=True, required=True)
  12. filename = fields.Char("Document File Name")
  13. mimetype = fields.Char("Mime-Type", compute='_mimetype')
  14. file_size = fields.Integer("File Size", compute='_file_size')
  15. document_date = fields.Date("Document Date",
  16. default=fields.Date.today())
  17. category = fields.Many2one('easy_my_coop.document.category',
  18. string="Category")
  19. published = fields.Boolean("Published?")
  20. publication_date = fields.Datetime("Publication Date",
  21. compute='_publication_date',
  22. store=True)
  23. public = fields.Boolean("Public?")
  24. @api.depends('document')
  25. def _mimetype(self):
  26. for doc in self:
  27. attachment_mgr = self.env['ir.attachment'].sudo()
  28. attachment = attachment_mgr.search_read(
  29. [('res_model', '=', self._name),
  30. ('res_id', '=', doc.id),
  31. ('res_field', '=', 'document')],
  32. fields=['mimetype', 'file_size'],
  33. limit=1,
  34. )[0]
  35. doc.mimetype = attachment['mimetype']
  36. @api.depends('document')
  37. def _file_size(self):
  38. for doc in self:
  39. attachment_mgr = self.env['ir.attachment'].sudo()
  40. attachment = attachment_mgr.search_read(
  41. [('res_model', '=', self._name),
  42. ('res_id', '=', doc.id),
  43. ('res_field', '=', 'document')],
  44. fields=['mimetype', 'file_size'],
  45. limit=1,
  46. )[0]
  47. doc.file_size = attachment['file_size']
  48. @api.depends('published')
  49. def _publication_date(self):
  50. for doc in self:
  51. if doc.published and not doc.publication_date:
  52. doc.publication_date = fields.Datetime.now()
  53. if not doc.published:
  54. doc.publication_date = False
  55. class Category(models.Model):
  56. _name = 'easy_my_coop.document.category'
  57. _description = "Category"
  58. _order = 'name'
  59. name = fields.Char("Name", required=True)
  60. description = fields.Text("Description")
  61. parent_id = fields.Many2one('easy_my_coop.document.category',
  62. string="Parent Category")
  63. child_ids = fields.One2many('easy_my_coop.document.category',
  64. 'parent_id',
  65. string="Child Categories")
  66. document_ids = fields.One2many('easy_my_coop.document',
  67. 'category',
  68. string="Documents")