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.

139 lines
4.6 KiB

  1. # Copyright 2018 Simone Orsi <simone.orsi@camptocamp.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import http
  4. from odoo.http import request
  5. import random
  6. FAKE_NAMES = [
  7. 'Madison Castillo',
  8. 'Destiny Frost',
  9. 'Dennis Parrish',
  10. 'Christy Moore',
  11. 'Larry James',
  12. 'David Simmons',
  13. 'Dr. Francis Ramos',
  14. 'Michelle Williams',
  15. 'Allison Montgomery',
  16. 'Michelle Rodriguez',
  17. 'Gina Patel',
  18. 'Corey Ray',
  19. 'Brent Myers',
  20. 'Sydney Hicks',
  21. 'Austin Buckley',
  22. 'Patricia Jones DDS',
  23. 'Dylan Davila',
  24. 'Christopher Bolton',
  25. 'James Cline',
  26. 'Gary Johnson',
  27. 'Jennifer Reese',
  28. 'Kevin Davis',
  29. 'Sandra Robinson',
  30. 'Sara Warner',
  31. 'Jaime Dunn',
  32. 'Mark Austin',
  33. 'Kendra Nelson',
  34. 'Matthew White',
  35. 'Rebecca Berger',
  36. 'Amanda Thornton',
  37. 'Lorraine Schultz',
  38. 'Chelsea Daniel',
  39. 'Kayla Jackson',
  40. 'Melanie Grant',
  41. 'Oscar Jones',
  42. 'Jon Sanchez',
  43. 'Kevin Anderson',
  44. 'Yvonne Mullen',
  45. 'Jonathan King',
  46. 'Wendy Hernandez'
  47. ]
  48. FAKE_NUMBERS = range(1, 30)
  49. class DigestPreview(http.Controller):
  50. digest_test_template = 'mail_digest.digest_layout_preview'
  51. @http.route([
  52. '/digest/layout-preview',
  53. ], type='http', auth='user')
  54. def digest_test(self):
  55. digest = self._fake_digest()
  56. mail_values = digest._get_email_values()
  57. values = {
  58. 'env': request.env,
  59. 'digest_html': mail_values['body_html'],
  60. }
  61. return request.render(self.digest_test_template, values)
  62. def _fake_digest(self):
  63. user = request.env.user
  64. digest_model = request.env['mail.digest'].sudo()
  65. digest = digest_model.new()
  66. digest.partner_id = user.partner_id
  67. digest.digest_template_id = digest._default_digest_template_id()
  68. digest.message_ids = self._fake_messages()
  69. digest.sanitize_msg_body = True
  70. return digest
  71. def _fake_messages(self):
  72. messages = request.env['mail.message'].sudo()
  73. subtype_model = request.env['mail.message.subtype'].sudo()
  74. subtypes = subtype_model.search([])
  75. records = request.env['res.partner'].sudo().search([])
  76. # TODO: filter subtypes?
  77. for i, subtype in enumerate(subtypes):
  78. # generate a couple of messages for each type
  79. for x in range(1, 3):
  80. msg = messages.new()
  81. msg.subtype_id = subtype
  82. subject, body = self._fake_content(subtype, i, x)
  83. msg.subject = subject
  84. msg.message_type = random.choice(
  85. ('email', 'comment', 'notification'))
  86. msg.email_from = 'random@user%d.com' % i
  87. msg.partner_ids = [(6, 0, request.env.user.partner_id.ids)]
  88. if i + x % 2 == 0:
  89. # relate a document
  90. msg.model = records._name
  91. msg.res_id = random.choice(records.ids)
  92. # simulate messages w/ no body but tracking values
  93. if x == random.choice([1, 2]):
  94. msg.tracking_value_ids = self._fake_tracking_vals()
  95. else:
  96. msg.body = body
  97. messages += msg
  98. return messages
  99. def _fake_content(self, subtype, i, x):
  100. subject = 'Lorem ipsum %d / %d' % (i, x)
  101. body = 'Random text here lorem ipsum %d / %d' % (i, x)
  102. if i % 2 == 0 and x > 1:
  103. # simulate also random styles that are goin to be stripped
  104. body = """
  105. <p style="font-size: 13px; font-family: &quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif; margin: 0px 0px 9px 0px">Lorem ipsum dolor sit amet, cetero menandri mel id.</p>
  106. <p>Ad modus tantas qui, quo choro facete delicata te.
  107. Epicurei accusata vix eu, prima erant graeci sit te,
  108. vivendum molestiae an mel.</p>
  109. <p>Sed apeirian atomorum id, no ius possit antiopam molestiae.</p>
  110. """ # noqa
  111. return subject, body.strip()
  112. def _fake_tracking_vals(self):
  113. tracking_model = request.env['mail.tracking.value'].sudo()
  114. track_vals1 = tracking_model.create_tracking_values(
  115. random.choice(FAKE_NAMES), random.choice(FAKE_NAMES),
  116. 'name', {'type': 'char', 'string': 'Name'},
  117. )
  118. track_vals2 = tracking_model.create_tracking_values(
  119. random.choice(FAKE_NUMBERS), random.choice(FAKE_NUMBERS),
  120. 'count', {'type': 'integer', 'string': 'Count'},
  121. )
  122. return [
  123. (0, 0, track_vals1),
  124. (0, 0, track_vals2),
  125. ]