Batch processing

2018-06-26  本文已影响0人  御前九品带刀刺猬
Directory structure
+---models
|       models.py
|       __init__.py
+---static     
|   \---src
|       +---img
|       |       default_image.png
|       +---js
|       |       batch.js
|       \---xml
|               batch.xml          
+---views
        views.xml
        templates.xml    

batch.xml
<?xml version="1.0" encoding="utf-8"?>
<templates>

    <div t-name="batch_buttons" class="batch_buttons">
        <button t-if='widget.model=="batch.processing"' type="button"
                class="btn btn-sm  btn-primary batch_search">
            Batch Search
        </button>

        <button t-if='widget.model=="batch.processing"' type="button"
                class="btn btn-sm  btn-primary batch_download">
            Batch Download
        </button>

    </div>

    <t t-extend="ListView.buttons">
        <t t-jquery="div.o_list_buttons" t-operation="append">
            <t t-call="batch_buttons"/>
        </t>
    </t>
    <t t-name="NumHideField" t-extend="FieldChar">

    </t>
</templates>
batch.js
odoo.define('module.batch', function (require) {
    'use strict';
    var ListView = require('web.ListView');
    var Model = require('web.Model');

    ListView.include({
        render_buttons: function () {
            this._super.apply(this, arguments);
            var self = this;
            if (self.model === 'batch.processing') {
                var model = new Model('batch.processing');
                // Batch  duration
                this.$buttons.on('click', 'button.batch_search', function () {
                    var record_ids = self.groups.get_selection().ids;
                    if (!record_ids .length) return;
                    model.call("records_search", [record_ids]).then(function (result) {
                        if (result) {
                            self.reload();
                        }
                    });
                });
                // Batch download call records
                this.$buttons.on('click', 'button.batch_download', function () {
                    var record_ids = self.groups.get_selection().ids;
                    if (!record_ids.length) return;
                    model.call("records_download", [record_ids]).then(function (result) {
                        if (result) {
                            for (var i = 0, len = result.length; i < len; i++) {
                                window.open(result[i])
                            }
                        }
                    });
                });
            }
        }
    });

});
models.py
# -*- coding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.addons.phone_callapi.models import call_http
from odoo.exceptions import AccessDenied, AccessError, UserError, ValidationError

class BatchProcessing(models.Model):
    _name = 'batch.processing'

    customer_id = fields.Many2one(,'l.customer', ondelete='restrict', string='Customer')
    callid = fields.Char('ID')
    duration = fields.Float('Duration')
    call_url = fields.Char('URL')

    # def search_download(self, call_url):
    #     return {
    #         'name': 'Download Call',
    #         'res_model': 'ir.actions.act_url',
    #         'type': 'ir.actions.act_url',
    #         'target': 'self',
    #         'url': '%s' % (str(call_url))
    #     }

    def records_download(self, *args):
        url_list = []
        for res in self:
            call_data = self.env['res.users'].sudo().call_config()
            sign = call_http.sigin(call_data['accountSid'], call_data['authToken'])
            url = str('https://' + call_data['address'] + '/' + call_data['version'] + '/Accounts/' + call_data[
                'accountSid'] + '/Applications/callRecordUrl?sig=' + sign)
            data = {
                "callRecordUrl": {
                    "appId": str(call_data['appId']),
                    "callId": str(res.callid)
                }
            }
            headers = {
                'Host': 'apiusertest.emic.com.cn',
                'Accept': 'application/json',
                'Content-Type': 'application/json;charset=utf-8',
                'Content-Length': '256',
                'Authorization': call_http.Authorization(call_data['accountSid'])
            }
            result, res_data = call_http.post_json(url=url, data=data, headers=headers)
            if result['code'] == 'succes':
                call_url = res_data['resp']['callRecordUrl']['url']
                res.duration = res_data['resp']['callRecordUrl']['duration']
                # filename = self.customer_id.name + str(int(time.time()))
                # return self.search_download(call_url)
                url_list.append(call_url)
            else:
                raise UserError(_(result['code']))

        return url_list

    @api.model
    def records_search(self, record_ids):
        callout_lines = self.env['callout.lines'].sudo().search([('id', 'in', list_ids)])
        for res in callout_lines:
            call_data = self.env['res.users'].sudo().call_config()
            sign = call_http.sigin(call_data['accountSid'], call_data['authToken'])
            url = str('https://' + call_data['address'] + '/' + call_data['version'] + '/Accounts/' + call_data[
                'accountSid'] + '/Applications/callRecordUrl?sig=' + sign)
            data = {
                "callRecordUrl": {
                    "appId": str(call_data['appId']),
                    "callId": str(res.callid)
                }
            }
            headers = {
                'Host': 'apiusertest.emic.com.cn',
                'Accept': 'application/json',
                'Content-Type': 'application/json;charset=utf-8',
                'Content-Length': '256',
                'Authorization': call_http.Authorization(call_data['accountSid'])
            }
            result, res_data = call_http.post_json(url=url, data=data, headers=headers)
            if result['code'] == 'succes':
                res.duration = res_data['resp']['callRecordUrl']['duration']
                return True

            else:
                raise UserError(_(result['code']))
上一篇下一篇

猜你喜欢

热点阅读