import time from netforce.model import Model, fields, get_model from netforce.utils import get_file_path from . import utils class ImportPatient(Model): _name="clinic.import.patient" _transient=True _fields={ 'date': fields.DateTime("Date"), 'file': fields.File("File"), 'result': fields.Text("Success"), 'hcode': fields.Char("Hospital Code"), } def get_hcode(self,context={}): settings=get_model("settings").browse(1) hcode=settings.hospital_code or "" return hcode _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'hcode': get_hcode, } def import_patient(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) lines=utils.read_excel(fpath,show_datetime=True) if not lines: raise Exception("Wrong File") for line in lines: hcode=line.get('hcode18','0') if not hcode: hcode='0' hcode=int(hcode) hcode=str(hcode) if obj.hcode==hcode: name=line.get("name14") hn=line.get('hn',"") patient_ids=get_model("clinic.patient").search([['name','=',name]]) if not patient_ids: vals={ 'name': name, 'hn': hn, 'type': 'sc', } patient_id=get_model('clinic.patient').create(vals) print("create patient ", patient_id) ImportPatient.register()