main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import werkzeug
  2. from openerp import http, SUPERUSER_ID
  3. from openerp.http import request
  4. class MassMailController(http.Controller):
  5. @http.route('/mail/track/<int:mail_id>/blank.gif', type='http', auth='none')
  6. def track_mail_open(self, mail_id, **post):
  7. """ Email tracking. """
  8. mail_mail_stats = request.registry.get('mail.mail.statistics')
  9. mail_mail_stats.set_opened(request.cr, SUPERUSER_ID, mail_mail_ids=[mail_id])
  10. response = werkzeug.wrappers.Response()
  11. response.mimetype = 'image/gif'
  12. response.data = 'R0lGODlhAQABAIAAANvf7wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='.decode('base64')
  13. return response
  14. @http.route(['/mail/mailing/<int:mailing_id>/unsubscribe'], type='http', auth='none')
  15. def mailing(self, mailing_id, email=None, res_id=None, **post):
  16. cr, uid, context = request.cr, request.uid, request.context
  17. MassMailing = request.registry['mail.mass_mailing']
  18. mailing_ids = MassMailing.exists(cr, SUPERUSER_ID, [mailing_id], context=context)
  19. if not mailing_ids:
  20. return 'KO'
  21. mailing = MassMailing.browse(cr, SUPERUSER_ID, mailing_ids[0], context=context)
  22. if mailing.mailing_model == 'mail.mass_mailing.contact':
  23. list_ids = [l.id for l in mailing.contact_list_ids]
  24. record_ids = request.registry[mailing.mailing_model].search(cr, SUPERUSER_ID, [('list_id', 'in', list_ids), ('id', '=', res_id), ('email', 'ilike', email)], context=context)
  25. request.registry[mailing.mailing_model].write(cr, SUPERUSER_ID, record_ids, {'opt_out': True}, context=context)
  26. else:
  27. email_fname = None
  28. model = request.registry[mailing.mailing_model]
  29. if 'email_from' in model._fields:
  30. email_fname = 'email_from'
  31. elif 'email' in model._fields:
  32. email_fname = 'email'
  33. if email_fname:
  34. record_ids = model.search(cr, SUPERUSER_ID, [('id', '=', res_id), (email_fname, 'ilike', email)], context=context)
  35. if 'opt_out' in model._fields:
  36. model.write(cr, SUPERUSER_ID, record_ids, {'opt_out': True}, context=context)
  37. return 'OK'
  38. @http.route(['/website_mass_mailing/is_subscriber'], type='json', auth="public", website=True)
  39. def is_subscriber(self, list_id, **post):
  40. cr, uid, context = request.cr, request.uid, request.context
  41. Contacts = request.registry['mail.mass_mailing.contact']
  42. Users = request.registry['res.users']
  43. is_subscriber = False
  44. email = None
  45. if uid != request.website.user_id.id:
  46. email = Users.browse(cr, SUPERUSER_ID, uid, context).email
  47. elif request.session.get('mass_mailing_email'):
  48. email = request.session['mass_mailing_email']
  49. if email:
  50. contact_ids = Contacts.search(cr, SUPERUSER_ID, [('list_id', '=', int(list_id)), ('email', '=', email), ('opt_out', '=', False)], context=context)
  51. is_subscriber = len(contact_ids) > 0
  52. return {'is_subscriber': is_subscriber, 'email': email}
  53. @http.route(['/website_mass_mailing/subscribe'], type='json', auth="public", website=True)
  54. def subscribe(self, list_id, email, **post):
  55. cr, uid, context = request.cr, request.uid, request.context
  56. Contacts = request.registry['mail.mass_mailing.contact']
  57. parsed_email = Contacts.get_name_email(email, context=context)[1]
  58. contact_ids = Contacts.search_read(
  59. cr, SUPERUSER_ID,
  60. [('list_id', '=', int(list_id)), ('email', '=', parsed_email)],
  61. ['opt_out'], context=context)
  62. if not contact_ids:
  63. Contacts.add_to_list(cr, SUPERUSER_ID, email, int(list_id), context=context)
  64. else:
  65. if contact_ids[0]['opt_out']:
  66. Contacts.write(cr, SUPERUSER_ID, [contact_ids[0]['id']], {'opt_out': False}, context=context)
  67. # add email to session
  68. request.session['mass_mailing_email'] = email
  69. return True