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.

81 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. _inherit = 'mail.thread'
  10. name = fields.Char("Name", required=True)
  11. description = fields.Text("Description")
  12. document = fields.Binary('Document', attachment=True, required=True)
  13. filename = fields.Char("Document File Name")
  14. mimetype = fields.Char("Mime-Type", compute='_mimetype')
  15. file_size = fields.Integer("File Size", compute='_file_size')
  16. document_date = fields.Date("Document Date",
  17. default=fields.Date.today())
  18. category = fields.Many2one('easy_my_coop.document.category',
  19. string="Category")
  20. published = fields.Boolean("Published?")
  21. publication_date = fields.Datetime("Publication Date",
  22. compute='_publication_date',
  23. store=True)
  24. public = fields.Boolean("Public?")
  25. @api.depends('document')
  26. def _mimetype(self):
  27. for doc in self:
  28. attachment_mgr = self.env['ir.attachment'].sudo()
  29. attachment = attachment_mgr.search_read(
  30. [('res_model', '=', self._name),
  31. ('res_id', '=', doc.id),
  32. ('res_field', '=', 'document')],
  33. fields=['mimetype', 'file_size'],
  34. limit=1,
  35. )[0]
  36. doc.mimetype = attachment['mimetype']
  37. @api.depends('document')
  38. def _file_size(self):
  39. for doc in self:
  40. attachment_mgr = self.env['ir.attachment'].sudo()
  41. attachment = attachment_mgr.search_read(
  42. [('res_model', '=', self._name),
  43. ('res_id', '=', doc.id),
  44. ('res_field', '=', 'document')],
  45. fields=['mimetype', 'file_size'],
  46. limit=1,
  47. )[0]
  48. doc.file_size = attachment['file_size']
  49. @api.depends('published')
  50. def _publication_date(self):
  51. for doc in self:
  52. if doc.published and not doc.publication_date:
  53. doc.publication_date = fields.Datetime.now()
  54. if not doc.published:
  55. doc.publication_date = False
  56. class Category(models.Model):
  57. _name = 'easy_my_coop.document.category'
  58. _description = "Category"
  59. _order = 'name'
  60. name = fields.Char("Name", required=True)
  61. description = fields.Text("Description")
  62. parent_id = fields.Many2one('easy_my_coop.document.category',
  63. string="Parent Category")
  64. child_ids = fields.One2many('easy_my_coop.document.category',
  65. 'parent_id',
  66. string="Child Categories")
  67. document_ids = fields.One2many('easy_my_coop.document',
  68. 'category',
  69. string="Documents")