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.

226 lines
8.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Eficent Business and IT Consulting Services S.L.
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from odoo import api, fields, models, _
  5. from odoo.exceptions import UserError
  6. class PrivacyPartnerReport(models.TransientModel):
  7. _name = "privacy.partner.report"
  8. _description = "Privacy Partner Report"
  9. company_id = fields.Many2one(
  10. comodel_name='res.company',
  11. string='Company',
  12. required=True,
  13. default=lambda self: self.env.user.company_id,
  14. )
  15. partner_id = fields.Many2one(
  16. comodel_name='res.partner',
  17. string='Partner',
  18. required=True,
  19. )
  20. table_ids = fields.Many2many(
  21. comodel_name='privacy.partner.data',
  22. string='Models with related partner data',
  23. )
  24. @api.onchange('partner_id')
  25. def _onchange_partner_id(self):
  26. if self.partner_id:
  27. data = self._get_tables_from_partner(self.partner_id)
  28. names = self._get_table_names(data)
  29. tables = self.env['privacy.partner.data']
  30. for name in sorted(names):
  31. vals = self._get_default_table(
  32. name=name,
  33. data=[t for t in data if t[0] == name and not t[5]],
  34. )
  35. if vals:
  36. tables |= self.env['privacy.partner.data'].create(vals)
  37. self.table_ids = tables
  38. else:
  39. self.table_ids = self.env['privacy.partner.data']
  40. return {
  41. 'domain': {
  42. 'table_ids': [
  43. ('id', 'in', self.table_ids.ids)],
  44. },
  45. }
  46. @api.onchange('company_id')
  47. def _onchange_company_id(self):
  48. if not self.company_id:
  49. self.company_id = self.env.user.company_id
  50. return {
  51. 'domain': {
  52. 'partner_id': [
  53. ('company_id', 'in', [self.company_id.id, False])],
  54. },
  55. }
  56. @api.multi
  57. def button_export_xlsx(self):
  58. self.ensure_one()
  59. return self.check_report(xlsx_report=True)
  60. def _build_contexts(self, data):
  61. result = {}
  62. result['partner_id'] = data['form']['partner_id'][0] or False
  63. result['company_id'] = data['form']['company_id'][0] or False
  64. result['table_ids'] = 'table_ids' in data['form'] and \
  65. data['form']['table_ids'] or False
  66. return result
  67. def _clean_data(self, model, rows):
  68. cleaned_rows = []
  69. for i, row in enumerate(rows):
  70. cleaned_rows.append({})
  71. for key, value in row.items():
  72. label = self.env[model]._fields[key].string or key
  73. if self.env[model]._fields[key].store:
  74. if 'many2one' == self.env[model]._fields[key].type:
  75. comodel = self.env[model]._fields[key].comodel_name
  76. if value:
  77. record = self.env[comodel].sudo().browse(value)
  78. cleaned_rows[i][label] = \
  79. record.display_name.encode('utf8')
  80. else:
  81. cleaned_rows[i][label] = rows[i][key]
  82. elif '2many' not in self.env[model]._fields[key].type:
  83. cleaned_rows[i][label] = rows[i][key]
  84. return cleaned_rows
  85. @api.multi
  86. def check_report(self, xlsx_report=False):
  87. self.ensure_one()
  88. data = {}
  89. data['ids'] = self.env.context.get('active_ids', [])
  90. data['model'] = self.env.context.get('active_model', 'ir.ui.menu')
  91. data['form'] = self.read(['partner_id', 'company_id', 'table_ids'])[0]
  92. used_context = self._build_contexts(data)
  93. data['form']['used_context'] = dict(
  94. used_context, lang=self.env.context.get('lang', 'en_US'))
  95. return self._print_report(data=data, xlsx_report=xlsx_report)
  96. @api.multi
  97. def compute_data_for_report(self, data):
  98. if not data.get('form'):
  99. raise UserError(
  100. _("Form content is missing, this report cannot be printed."))
  101. partner = data['form'].get('partner_id', False)
  102. if not partner:
  103. raise UserError(
  104. _("No provided partner."))
  105. partner = self.env['res.partner'].sudo().browse(partner[0])
  106. tables = data['form'].get('table_ids', False)
  107. if tables:
  108. tables = self.env['privacy.partner.data'].browse(tables)
  109. tables = self._get_rows_from_tables(tables, partner)
  110. data.update({'tables': tables, })
  111. return data
  112. def _exclude_column(self, model, column):
  113. # https://github.com/odoo/odoo/issues/24927
  114. if model in ('mail.compose.message', 'survey.mail.compose.message'):
  115. if column in ('needaction_partner_ids', 'starred_partner_ids'):
  116. return True
  117. # feel free to add more specific cases meanwhile the issue is not fixed
  118. return False
  119. def _get_default_table(self, name, data):
  120. if data:
  121. data_type = data[0][4]
  122. res = self.env[data[0][1]]
  123. for t in data:
  124. res |= self.env[t[1]].sudo().browse(t[3])
  125. if res:
  126. values = {
  127. 'name': name,
  128. 'model_id': self.env['ir.model'].sudo().search(
  129. [('model', '=', res._name)]).id,
  130. 'count_rows': len(res.ids),
  131. 'type': data_type,
  132. }
  133. return values
  134. return {}
  135. def _get_model_from_table(self, table, partner):
  136. new_tables = {}
  137. for model in table.model_id:
  138. rows = self._get_rows_from_model(model, partner)
  139. new_tables[model.display_name.encode('utf8')] = rows
  140. return new_tables
  141. def _get_rows_from_model(self, model, partner):
  142. lines = self.env[model.model]
  143. columns = [k for k, v in self.env[model.model]._fields.items()
  144. if v.comodel_name == 'res.partner' and
  145. v.store and not self._exclude_column(model.model, k)]
  146. for column in columns:
  147. lines |= self.env[model.model].sudo().search(
  148. [(column, '=', partner.id)])
  149. rows = lines.sudo().read(load=False)
  150. rows = self._clean_data(model.model, rows)
  151. return rows
  152. def _get_rows_from_tables(self, tables, partner):
  153. new_tables = {}
  154. for table in tables:
  155. data_table = self._get_model_from_table(table, partner)
  156. new_tables[str(table.name)] = data_table
  157. return new_tables
  158. def _get_table_names(self, data):
  159. names = []
  160. for t in data:
  161. if t[3] and not t[5] and t[0] not in names:
  162. names.append(t[0])
  163. return names
  164. def _get_tables_from_partner(self, partner):
  165. tables = [t[0] for t in [
  166. [[self.env[m]._table, m, k, self.env[m].sudo().search(
  167. [(k, '=', partner.id)]).ids, v.type, self.env[m]._transient]
  168. for k, v in self.env[m]._fields.items()
  169. if v.comodel_name == 'res.partner' and self.env[m]._auto and
  170. v.store and not self._exclude_column(m, k)]
  171. for m in [x for x in self.env.registry.keys()]] if t]
  172. for i, t in enumerate(tables):
  173. if t[4] == 'many2many':
  174. if t[3]:
  175. relation = self.env[t[1]]._fields[t[2]].relation
  176. if relation:
  177. tables[i][0] = relation
  178. return tables
  179. def _print_report(self, data, xlsx_report=False):
  180. records = self.env[data['model']].sudo().browse(data.get('ids', []))
  181. processed_data = self.compute_data_for_report(data)
  182. if xlsx_report:
  183. return self.env['report'].with_context(landscape=True).get_action(
  184. records=records, report_name='privacy.report_partner_xlsx',
  185. data=processed_data)
  186. class PrivacyPartnerData(models.TransientModel):
  187. _name = "privacy.partner.data"
  188. _description = "Privacy Partner Data"
  189. name = fields.Char(
  190. string='Database Table',
  191. )
  192. model_id = fields.Many2one(
  193. comodel_name='ir.model',
  194. ondelete='cascade',
  195. string='Models',
  196. )
  197. type = fields.Char(
  198. string="Type",
  199. )
  200. count_rows = fields.Integer(
  201. default=0,
  202. string='Number of lines',
  203. )