Odoo中同步更新模板文件的文件名

效果:

 

 

 

PY文件代码:

class RhwlDataUploadWizard(osv.osv_memory):
    _name = 'rhwl.data.upload.wizard'

    _columns = {
        "excel_name": fields.char(u"EXCEL文件名"),
        "excel_file_templet": fields.binary(u"模板下载"),
        "setting_id": fields.many2one("rhwl.lims.base.setting", string=u"检测项目", domain="[('is_product','=',True)]"),
        "operation_type": fields.selection([("1", u"样本信息导入"),
                                            ("2", u"实验结果上传"),
                                            ("3", u"更新样本信息")], u"操作类型", required=True),
        "file_bin": fields.binary(string=u"文件"),
    }


    _defaults = {
        "operation_type": "1",
    }

    def _get_data_templet(self, setting_code, operation_type):
        project_path = os.path.dirname(os.path.dirname(__file__))
        excel_name = ""
        if operation_type == "1":
            excel_name = "Virus_Sample_Templet_HPV"

        if operation_type == "2":
            excel_name = "Virus_Result_Templet_Acid"

        if operation_type == "3":
            excel_name = "Virus_Update_Sample_Templet"

        if not excel_name:
            return False

        templet_path = os.path.join(project_path, "templet%s%s.xls" % (os.sep, excel_name))
        with open(templet_path, 'rb') as fp:
            excel_templet = base64.encodestring(fp.read())

        return excel_templet

    @api.onchange("setting_id", "operation_type")
    def onchange_setting_operation(self):
        if self.operation_type and self.setting_id:
            self.excel_file_templet = self._get_data_templet(self.setting_id, self.operation_type)
            if self.operation_type == "1":
                self.excel_name = "Create_Sample_Templet.xls"
            elif self.operation_type == "2":
                self.excel_name = "Sample_Result_Templet.xls"
            elif self.operation_type == "3":
                self.excel_name = "Update_Sample_Templet.xls"

 

XML文件:

<record id="rhwl_data_upload_wizard_view_form" model="ir.ui.view">
            <field name="name">rhwl data upload wizard</field>
            <field name="model">rhwl.data.upload.wizard</field>
            <field name="arch" type="xml">
                <form string="Parameters">
                    <group>
                        <field name="excel_name" class="excel_name"   invisible="1"/>
                        <field name="excel_file_templet" class="file_templet" filename="excel_name" readonly="1" />
                        <field name="operation_type"  style='width:60%' />
                        <field name="setting_id"  style='width:60%' />
                        <field name="file_bin" />
                    </group>
                    <footer>
                        <button name="action_data_upload" string="上传" type="object"  class="oe_highlight"/>
                        or
                        <button string="Cancel" class="oe_link" special="cancel" />
                    </footer>
                </form>

            </field>
        </record>

        <record id="action_rhwl_data_upload" model="ir.actions.act_window">
            <field name="name">数据上传</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">rhwl.data.upload.wizard</field>
            <field name="view_type">form</field>
            <field name="view_mode">form_data_upload_wizard</field>
            <field name="context">{'data_update':'1'}</field>
            <field name="target">new</field>
        </record>

 

JS文件:

    instance.web.views.add('form_data_upload_wizard', 'instance.web.rhwl_virus.FormView');
    instance.web.rhwl_virus.FormView = instance.web.FormView.extend({
        init: function () {
            this._super.apply(this, arguments);
            var self = this;
        },
        on_form_changed: function() {
            this._super.apply(this, arguments);
            var self = this;
            var excel_name = self.$el.find(".excel_name").children("input").val();
            if(excel_name){
                self.$el.find(".file_templet").children(".oe_form_uri").html(excel_name);
            }
        }
    });

 

posted @ 2021-04-01 16:02  553490191  阅读(129)  评论(0编辑  收藏  举报