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.

140 lines
4.6 KiB

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