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.

84 lines
3.1 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  2. from odoo import api, fields, models, _
  3. class SearchLine(models.Model):
  4. _name = "search.line"
  5. name = fields.Char(string="Model Name")
  6. field_list = fields.Char(string="Fields Name")
  7. model_id = fields.Many2one('ir.model', string="Found in Model")
  8. record_id = fields.Integer(string="Record ID")
  9. search_id = fields.Many2one("dpo.view", string="Search Terms")
  10. record_name = fields.Char(string="Record Name",
  11. compute="_compute_record_name")
  12. @api.multi
  13. def open_record(self):
  14. return {
  15. 'name': _('Found record'),
  16. 'view_type': 'form',
  17. 'view_mode': 'form',
  18. 'res_model': self.model_id.model,
  19. 'type': 'ir.actions.act_window',
  20. 'res_id': self.record_id,
  21. 'target': 'new'
  22. }
  23. def _compute_record_name(self):
  24. for record in self:
  25. record_object = self.env[self.model_id.model].browse([
  26. self.record_id
  27. ])
  28. record.record_name = record_object.name_get()[0][1]
  29. class DpoView(models.Model):
  30. _name = "dpo.view"
  31. name = fields.Char(string="Search Term")
  32. model_ids = fields.Many2many('ir.model',
  33. 'dpo_view_ir_model_rel',
  34. string='Search in Model')
  35. search_lines = fields.One2many('search.line',
  36. 'search_id',
  37. string='Search Result')
  38. @api.multi
  39. def search_string(self):
  40. search_line_ids = self.env['search.line'].search([
  41. ('search_id', '=', self.id)
  42. ])
  43. search_line_ids.unlink()
  44. final_list = []
  45. for model_id in self.model_ids:
  46. field_list = []
  47. found_match = {}
  48. for field_id in model_id.field_id:
  49. if field_id.ttype in ['char', 'html', 'text'] \
  50. and field_id.store:
  51. field_list.append(field_id.name)
  52. for field in field_list:
  53. records = self.env[model_id.model].search([
  54. (field, 'ilike', self.name),
  55. (field, '!=', '')
  56. ])
  57. for rec in records:
  58. temp_list = found_match.get(rec.id, False)
  59. if temp_list:
  60. temp_list.append(field)
  61. found_match[rec.id] = temp_list
  62. else:
  63. found_match[rec.id] = [field]
  64. for key, value in found_match.items():
  65. founded_json = {}
  66. founded_json["field_list"] = str(list(set(value)))
  67. founded_json["name"] = str(model_id.name)
  68. founded_json["model_id"] = model_id.id
  69. founded_json["search_id"] = self.id
  70. founded_json["record_id"] = key
  71. final_list.append(founded_json)
  72. if final_list:
  73. for vals in final_list:
  74. self.env['search.line'].create(vals)