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.

82 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from openerp import api, fields, models
  5. from openerp.exceptions import MissingError
  6. class DarkroomModal(models.TransientModel):
  7. _name = 'darkroom.modal'
  8. _description = 'Darkroom Modal - Wizard Model'
  9. @api.model
  10. def _default_res_model_id(self):
  11. res_model_name = self.env.context.get('active_model')
  12. return self.env['ir.model'].search([('model', '=', res_model_name)])
  13. @api.model
  14. def _default_res_record_id(self):
  15. return self.env.context.get('active_record_id', 0)
  16. @api.model
  17. def _default_res_record(self):
  18. res_model_name = self._default_res_model_id().model
  19. try:
  20. res_model_model = self.env[res_model_name]
  21. except KeyError:
  22. return None
  23. return res_model_model.browse(self._default_res_record_id())
  24. @api.model
  25. def _default_res_field_id(self):
  26. res_model_id = self._default_res_model_id()
  27. res_field_name = self.env.context.get('active_field')
  28. return self.env['ir.model.fields'].search([
  29. ('model_id', '=', res_model_id.id),
  30. ('name', '=', res_field_name),
  31. ])
  32. @api.model
  33. def _default_image(self):
  34. res_record = self._default_res_record()
  35. res_field_name = self._default_res_field_id().name
  36. try:
  37. return getattr(res_record, res_field_name, None)
  38. except (TypeError, MissingError):
  39. return None
  40. res_model_id = fields.Many2one(
  41. comodel_name='ir.model',
  42. string='Source Model',
  43. required=True,
  44. default=lambda s: s._default_res_model_id(),
  45. )
  46. res_record_id = fields.Integer(
  47. string='Source Record ID',
  48. required=True,
  49. default=lambda s: s._default_res_record_id(),
  50. )
  51. res_field_id = fields.Many2one(
  52. comodel_name='ir.model.fields',
  53. string='Source Field',
  54. required=True,
  55. default=lambda s: s._default_res_field_id(),
  56. )
  57. image = fields.Binary(
  58. string='Darkroom Image',
  59. required=True,
  60. default=lambda s: s._default_image(),
  61. )
  62. @api.multi
  63. def action_save(self):
  64. self.ensure_one()
  65. res_record = self._default_res_record()
  66. res_field_name = self._default_res_field_id().name
  67. setattr(res_record, res_field_name, self.image)
  68. return {'type': 'ir.actions.act_window_close'}