Exemplo n.º 1
0
    def _snailmail_print(self):
        """
        get response
        {
            'request_code': RESPONSE_OK, # because we receive 200 if good or fail
            'total_cost': total_cost,
            'credit_error': credit_error,
            'request': {
                'documents': documents,
                'options': options
                }
            }
        }
        """
        self.write({'state': 'pending'})
        endpoint = self.env['ir.config_parameter'].sudo().get_param('snailmail.endpoint', DEFAULT_ENDPOINT)
        params = self._snailmail_create('print')
        response = jsonrpc(endpoint + PRINT_ENDPOINT, params=params)
        for doc in response['request']['documents']:
            letter = self.browse(doc['letter_id'])
            record = self.env[doc['res_model']].browse(doc['res_id'])
            if doc.get('sent') and response['request_code'] == 200:
                if hasattr(record, '_message_log'):
                    message = _('The document was correctly sent by post.<br>The tracking id is %s' % doc['send_id'])
                    record._message_log(body=message)
                    letter.write({'info_msg': message, 'state': 'sent'})
            else:
                # look for existing activities related to snailmail to update or create a new one.
                # TODO: in following versions, Add a link to a specifc activity on the letter
                note = _('An error occured when sending the document by post.<br>Error: %s' % \
                    self._get_error_message(doc['error'] if response['request_code'] == 200 else response['reason']))

                domain = [
                    ('summary', 'ilike', '[SNAILMAIL]'),
                    ('res_id', '=', letter.res_id),
                    ('res_model_id', '=', self.env['ir.model']._get(letter.model).id),
                    ('activity_type_id', '=', self.env.ref('mail.mail_activity_data_warning').id),
                ]
                MailActivity = self.env['mail.activity']
                activity = MailActivity.search(domain, limit=1)

                activity_data = {
                    'activity_type_id': self.env.ref('mail.mail_activity_data_warning').id,
                    'summary': '[SNAILMAIL] ' + _('Post letter: an error occured.'),
                    'note': note,
                    'date_deadline': fields.Date.today()
                }
                if activity:
                    activity.update(activity_data)
                else:
                    activity_data.update({
                        'user_id': letter.user_id.id,
                        'res_id': letter.res_id,
                        'res_model_id': self.env['ir.model']._get(letter.model).id,
                    })
                    MailActivity.create(activity_data)

                letter.write({'info_msg': note, 'state': 'error'})

        self.env.cr.commit()
Exemplo n.º 2
0
 def _rpc_remote_api(self, action, params, timeout=15):
     if self.env.registry.in_test_mode():
         return False, 'Insufficient Credit'
     url = '%s/%s' % (self.get_endpoint(), action)
     account = self.env['iap.account'].get('partner_autocomplete')
     if not account.account_token:
         return False, 'No Account Token'
     params.update({
         'db_uuid':
         self.env['ir.config_parameter'].sudo().get_param('database.uuid'),
         'account_token':
         account.account_token,
         'country_code':
         self.env.company.country_id.code,
         'zip':
         self.env.company.zip,
     })
     try:
         return jsonrpc(url=url, params=params, timeout=timeout), False
     except (ConnectionError, HTTPError, exceptions.AccessError,
             exceptions.UserError) as exception:
         _logger.error('Autocomplete API error: %s' % str(exception))
         return False, str(exception)
     except InsufficientCreditError as exception:
         _logger.warning(
             'Insufficient Credits for Autocomplete Service: %s' %
             str(exception))
         return False, 'Insufficient Credit'
Exemplo n.º 3
0
 def _perform_reveal_service(self, server_payload):
     result = False
     account_token = self.env['iap.account'].get('reveal')
     endpoint = self.env['ir.config_parameter'].sudo().get_param(
         'reveal.endpoint', DEFAULT_ENDPOINT) + '/iap/clearbit/1/reveal'
     params = {
         'account_token': account_token.account_token,
         'data': server_payload
     }
     result = jsonrpc(endpoint, params=params, timeout=300)
     for res in result.get('reveal_data', []):
         if not res.get('not_found'):
             lead = self._create_lead_from_response(res)
             self.env['crm.reveal.view'].search([('reveal_ip', '=',
                                                  res['ip'])]).unlink()
         else:
             self.env['crm.reveal.view'].search([
                 ('reveal_ip', '=', res['ip'])
             ]).write({'reveal_state': 'not_found'})
     if result.get('credit_error'):
         self.env['crm.iap.lead.helpers'].notify_no_more_credit(
             'reveal', self._name, 'reveal.already_notified')
         return False
     else:
         self.env['ir.config_parameter'].sudo().set_param(
             'reveal.already_notified', False)
     return True
 def _perform_request(self):
     """
     This will perform the request and create the corresponding leads.
     The user will be notified if he hasn't enough credits.
     """
     server_payload = self._prepare_iap_payload()
     reveal_account = self.env['iap.account'].get('reveal')
     dbuuid = self.env['ir.config_parameter'].sudo().get_param(
         'database.uuid')
     endpoint = self.env['ir.config_parameter'].sudo().get_param(
         'reveal.endpoint',
         DEFAULT_ENDPOINT) + '/iap/clearbit/1/lead_mining_request'
     params = {
         'account_token': reveal_account.account_token,
         'dbuuid': dbuuid,
         'data': server_payload
     }
     try:
         response = jsonrpc(endpoint, params=params, timeout=300)
         return response['data']
     except InsufficientCreditError as e:
         self.error = 'Insufficient credits. Recharge your account and retry.'
         self.state = 'error'
         self._cr.commit()
         raise e
Exemplo n.º 5
0
 def _contact_iap(self, local_endpoint, params):
     account = self.env['iap.account'].get('reveal')
     dbuuid = self.env['ir.config_parameter'].sudo().get_param(
         'database.uuid')
     params['account_token'] = account.account_token
     params['dbuuid'] = dbuuid
     return iap.jsonrpc(self._DEFAULT_ENDPOINT + local_endpoint,
                        params=params,
                        timeout=300)
Exemplo n.º 6
0
    def _snailmail_print_valid_address(self):
        """
        get response
        {
            'request_code': RESPONSE_OK, # because we receive 200 if good or fail
            'total_cost': total_cost,
            'credit_error': credit_error,
            'request': {
                'documents': documents,
                'options': options
                }
            }
        }
        """
        endpoint = self.env['ir.config_parameter'].sudo().get_param(
            'snailmail.endpoint', DEFAULT_ENDPOINT)
        params = self._snailmail_create('print')
        response = jsonrpc(endpoint + PRINT_ENDPOINT, params=params)
        for doc in response['request']['documents']:
            if doc.get('sent') and response['request_code'] == 200:
                note = _(
                    'The document was correctly sent by post.<br>The tracking id is %s'
                    % doc['send_id'])
                letter_data = {
                    'info_msg': note,
                    'state': 'sent',
                    'error_code': False
                }
            else:
                error = doc['error'] if response[
                    'request_code'] == 200 else response['reason']

                note = _(
                    'An error occured when sending the document by post.<br>Error: %s'
                    % self._get_error_message(error))
                letter_data = {
                    'info_msg':
                    note,
                    'state':
                    'error',
                    'error_code':
                    error if error in ERROR_CODES else 'UNKNOWN_ERROR'
                }

            letter = self.browse(doc['letter_id'])
            letter.write(letter_data)
        self.send_snailmail_update()