def _convert_visitor_to_lead(self, partner, channel_partners, key): description = ''.join( '%s: %s\n' % (message.author_id.name or self.anonymous_name, message.body) for message in self.channel_message_ids.sorted('id')) utm_source = self.env.ref('crm_livechat.utm_source_livechat', raise_if_not_found=False) lead = self.env['crm.lead'].create({ 'name': html2plaintext(key[5:]), 'partner_id': channel_partners.partner_id.id, 'user_id': None, 'team_id': None, 'description': html2plaintext(description), 'referred': partner.name, 'source_id': utm_source and utm_source.id, }) lead._onchange_partner_id() return lead
def _compute_teaser(self): for blog_post in self: if blog_post.teaser_manual: blog_post.teaser = blog_post.teaser_manual else: content = html2plaintext(blog_post.content).replace('\n', ' ') blog_post.teaser = content[:200] + '...'
def assertSMSLogged(self, records, body): for record in records: message = record.message_ids[-1] self.assertEqual(message.subtype_id, self.env.ref('mail.mt_note')) self.assertEqual(message.message_type, 'sms') self.assertEqual( tools.html2plaintext(message.body).rstrip('\n'), body)
def setHtmlFallbackText(self, padID, html): try: # Prevents malformed HTML errors html_wellformed = '<html><body>' + html + '</body></html>' return self.setHtml(padID, html_wellformed) except Exception: _logger.exception('Falling back to setText. SetHtml failed with message:') return self.setText(padID, html2plaintext(html).encode('UTF-8'))
def _compute_description(self): for message in self: if message.subject: message.description = message.subject else: plaintext_ct = '' if not message.body else html2plaintext( message.body) message.description = plaintext_ct[:30] + '%s' % ( ' [...]' if len(plaintext_ct) >= 30 else '')
def action_create_calendar_event(self): self.ensure_one() action = self.env.ref('calendar.action_calendar_event').read()[0] action['context'] = { 'default_activity_type_id': self.activity_type_id.id, 'default_res_id': self.env.context.get('default_res_id'), 'default_res_model': self.env.context.get('default_res_model'), 'default_name': self.summary or self.res_name, 'default_description': self.note and tools.html2plaintext(self.note).strip() or '', 'default_activity_ids': [(6, 0, self.ids)], } return action
def _mark_done(self): """ Will add certification to employee's resumé if - The survey is a certification - The user is linked to an employee - The user succeeded the test """ super(SurveyUserInput, self)._mark_done() certificate_user_inputs = self.filtered( lambda user_input: user_input.survey_id.certificate and user_input. quizz_passed) partner_has_completed = { user_input.partner_id.id: user_input.survey_id for user_input in certificate_user_inputs } employees = self.env['hr.employee'].sudo().search([ ('user_id.partner_id', 'in', certificate_user_inputs.mapped('partner_id').ids) ]) for employee in employees: line_type = self.env.ref( 'hr_skills_survey.resume_type_certification', raise_if_not_found=False) survey = partner_has_completed.get(employee.user_id.partner_id.id) self.env['hr.resume.line'].create({ 'employee_id': employee.id, 'name': survey.title, 'date_start': fields.Date.today(), 'date_end': fields.Date.today(), 'description': html2plaintext(survey.description), 'line_type_id': line_type and line_type.id, 'display_type': 'certification', 'survey_id': survey.id })
def _send_prepare_values(self, partner=None): """Return a dictionary for specific email values, depending on a partner, or generic to the whole recipients given by mail.email_to. :param Model partner: specific recipient partner """ self.ensure_one() body = self._send_prepare_body() body_alternative = tools.html2plaintext(body) if partner: email_to = [ tools.formataddr((partner.name or 'False', partner.email or 'False')) ] else: email_to = tools.email_split_and_format(self.email_to) res = { 'body': body, 'body_alternative': body_alternative, 'email_to': email_to, } return res
def _message_sms(self, body, subtype_id=False, partner_ids=False, number_field=False, sms_numbers=None, sms_pid_to_number=None, **kwargs): """ Main method to post a message on a record using SMS-based notification method. :param body: content of SMS; :param subtype_id: mail.message.subtype used in mail.message associated to the sms notification process; :param partner_ids: if set is a record set of partners to notify; :param number_field: if set is a name of field to use on current record to compute a number to notify; :param sms_numbers: see ``_notify_record_by_sms``; :param sms_pid_to_number: see ``_notify_record_by_sms``; """ self.ensure_one() sms_pid_to_number = sms_pid_to_number if sms_pid_to_number is not None else {} if number_field or (partner_ids is False and sms_numbers is None): info = self._sms_get_recipients_info(force_field=number_field)[self.id] info_partner_ids = info['partner'].ids if info['partner'] else False info_number = info['sanitized'] if info['sanitized'] else info['number'] if info_partner_ids and info_number: sms_pid_to_number[info_partner_ids[0]] = info_number if info_partner_ids: partner_ids = info_partner_ids + (partner_ids or []) if info_number and not info_partner_ids: sms_numbers = [info_number] + (sms_numbers or []) if subtype_id is False: subtype_id = self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note') return self.message_post( body=plaintext2html(html2plaintext(body)), partner_ids=partner_ids or [], # TDE FIXME: temp fix otherwise crash mail_thread.py message_type='sms', subtype_id=subtype_id, sms_numbers=sms_numbers, sms_pid_to_number=sms_pid_to_number, **kwargs )
def _prepare_log_body_values(self, sms_records_values): result = {} for record_id, sms_values in sms_records_values.items(): result[record_id] = html2plaintext(sms_values['body']) return result
def _get_plain_content(self): for post in self: post.plain_content = tools.html2plaintext(post.content)[0:500] if post.content else False
def assertSMSNotification(self, recipients_info, content, messages=None, check_sms=True): """ Check content of notifications. :param recipients_info: list[{ 'partner': res.partner record (may be empty), 'number': number used for notification (may be empty, computed based on partner), 'state': ready / sent / exception / canceled (sent by default), 'failure_type': optional: sms_number_missing / sms_number_format / sms_credit / sms_server }, { ... }] """ partners = self.env['res.partner'].concat(*list( p['partner'] for p in recipients_info if p.get('partner'))) numbers = [p['number'] for p in recipients_info if p.get('number')] base_domain = [ '|', ('res_partner_id', 'in', partners.ids), '&', ('res_partner_id', '=', False), ('sms_number', 'in', numbers), ('notification_type', '=', 'sms') ] if messages is not None: base_domain += [('mail_message_id', 'in', messages.ids)] notifications = self.env['mail.notification'].search(base_domain) self.assertEqual(notifications.mapped('res_partner_id'), partners) for recipient_info in recipients_info: partner = recipient_info.get('partner', self.env['res.partner']) number = recipient_info.get('number') state = recipient_info.get('state', 'sent') if number is None and partner: number = partner.phone_get_sanitized_number() notif = notifications.filtered( lambda n: n.res_partner_id == partner and n.sms_number == number and n.notification_status == state) self.assertTrue( notif, 'SMS: not found notification for %s (number: %s, state: %s)' % (partner, number, state)) if state not in ('sent', 'ready', 'canceled'): self.assertEqual(notif.failure_type, recipient_info['failure_type']) if check_sms: if state == 'sent': self.assertSMSSent([number], content) elif state == 'ready': self.assertSMSOutgoing(partner, number, content) elif state == 'exception': self.assertSMSFailed(partner, number, recipient_info['failure_type'], content) elif state == 'canceled': self.assertSMSCanceled( partner, number, recipient_info.get('failure_type', False), content) else: raise NotImplementedError('Not implemented') if messages is not None: for message in messages: self.assertEqual( content, tools.html2plaintext(message.body).rstrip('\n'))
def _notify_record_by_sms(self, message, recipients_data, msg_vals=False, sms_numbers=None, sms_pid_to_number=None, check_existing=False, put_in_queue=False, **kwargs): """ Notification method: by SMS. :param message: mail.message record to notify; :param recipients_data: see ``_notify_thread``; :param msg_vals: see ``_notify_thread``; :param sms_numbers: additional numbers to notify in addition to partners and classic recipients; :param pid_to_number: force a number to notify for a given partner ID instead of taking its mobile / phone number; :param check_existing: check for existing notifications to update based on mailed recipient, otherwise create new notifications; :param put_in_queue: use cron to send queued SMS instead of sending them directly; """ sms_pid_to_number = sms_pid_to_number if sms_pid_to_number is not None else {} sms_numbers = sms_numbers if sms_numbers is not None else [] sms_create_vals = [] sms_all = self.env['sms.sms'].sudo() # pre-compute SMS data body = msg_vals['body'] if msg_vals and msg_vals.get('body') else message.body sms_base_vals = { 'body': html2plaintext(body), 'mail_message_id': message.id, 'state': 'outgoing', } # notify from computed recipients_data (followers, specific recipients) partners_data = [r for r in recipients_data['partners'] if r['notif'] == 'sms'] partner_ids = [r['id'] for r in partners_data] if partner_ids: for partner in self.env['res.partner'].sudo().browse(partner_ids): number = sms_pid_to_number.get(partner.id) or partner.mobile or partner.phone sanitize_res = phone_validation.phone_sanitize_numbers_w_record([number], partner)[number] number = sanitize_res['sanitized'] or number sms_create_vals.append(dict( sms_base_vals, partner_id=partner.id, number=number )) # notify from additional numbers if sms_numbers: sanitized = phone_validation.phone_sanitize_numbers_w_record(sms_numbers, self) tocreate_numbers = [ value['sanitized'] or original for original, value in sanitized.items() if value['code'] != 'empty' ] sms_create_vals += [dict(sms_base_vals, partner_id=False, number=n) for n in tocreate_numbers] # create sms and notification existing_pids, existing_numbers = [], [] if sms_create_vals: sms_all |= self.env['sms.sms'].sudo().create(sms_create_vals) if check_existing: existing = self.env['mail.notification'].sudo().search([ '|', ('res_partner_id', 'in', partner_ids), '&', ('res_partner_id', '=', False), ('sms_number', 'in', sms_numbers), ('notification_type', '=', 'sms'), ('mail_message_id', '=', message.id) ]) for n in existing: if n.res_partner_id.id in partner_ids and n.mail_message_id == message: existing_pids.append(n.res_partner_id.id) if not n.res_partner_id and n.sms_number in sms_numbers and n.mail_message_id == message: existing_numbers.append(n.sms_number) notif_create_values = [{ 'mail_message_id': message.id, 'res_partner_id': sms.partner_id.id, 'sms_number': sms.number, 'notification_type': 'sms', 'sms_id': sms.id, 'is_read': True, # discard Inbox notification 'notification_status': 'ready', } for sms in sms_all if (sms.partner_id and sms.partner_id.id not in existing_pids) or (not sms.partner_id and sms.number not in existing_numbers)] if notif_create_values: self.env['mail.notification'].sudo().create(notif_create_values) if existing_pids or existing_numbers: for sms in sms_all: notif = next((n for n in existing if (n.res_partner_id.id in existing_pids and n.res_partner_id.id == sms.partner_id.id) or (not n.res_partner_id and n.sms_number in existing_numbers and n.sms_number == sms.number)), False) if notif: notif.write({ 'notification_type': 'sms', 'notification_status': 'ready', 'sms_id': sms.id, 'sms_number': sms.number, }) if sms_all and not put_in_queue: sms_all.send(auto_commit=False, raise_exception=False) return True
def _compute_name(self): """ Read the first line of the memo to determine the note name """ for note in self: text = html2plaintext(note.memo) if note.memo else '' note.name = text.strip().replace('*', '').split("\n")[0]
def mail_update_message(self, res_model, res_id, message, message_id, redirect=None, attachment_ids='', attachment_tokens='', **post): # keep this mecanism intern to slide currently (saas 12.5) as it is # considered experimental if res_model != 'slide.channel': raise Forbidden() res_id = int(res_id) attachment_ids = [ int(attachment_id) for attachment_id in attachment_ids.split(',') if attachment_id ] attachment_tokens = [ attachment_token for attachment_token in attachment_tokens.split(',') if attachment_token ] self._portal_post_check_attachments(attachment_ids, attachment_tokens) pid = int(post['pid']) if post.get('pid') else False if not _check_special_access(res_model, res_id, token=post.get('token'), _hash=post.get('hash'), pid=pid): raise Forbidden() # fetch and update mail.message message_id = int(message_id) message_body = plaintext2html(message) domain = [('model', '=', res_model), ('res_id', '=', res_id), ('website_published', '=', True), ('author_id', '=', request.env.user.partner_id.id), ('message_type', '=', 'comment'), ('id', '=', message_id)] # restrict to the given message_id message = request.env['mail.message'].search(domain, limit=1) if not message: raise NotFound() message.write({ 'body': message_body, 'attachment_ids': [(4, aid) for aid in attachment_ids], }) # update rating if post.get('rating_value'): domain = [('res_model', '=', res_model), ('res_id', '=', res_id), ('website_published', '=', True), ('message_id', '=', message.id)] rating = request.env['rating.rating'].search( domain, order='write_date DESC', limit=1) rating.write({ 'rating': float(post['rating_value']), 'feedback': html2plaintext(message.body), }) # redirect to specified or referrer or simply channel page as fallback redirect_url = redirect or (request.httprequest.referrer and request.httprequest.referrer + '#review') or '/slides/%s' % res_id return werkzeug.utils.redirect(redirect_url, 302)