Openerp/histest

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา

การสร้าง OTP เพื่อผนวกเข้ากับ workflow ของโมเดล HISAppointment

ไฟล์ histest.py

ที่ตอนต้นของไฟล์ โหลดฟังก์ชัน send_sms จากมอดูล sms.py ที่สร้างขึ้นก่อนหน้านี้ และฟังก์ชัน randint จากมอดูล random เพื่อใช้สร้าง OTP (One-Time Password) แบบสุ่ม

# encoding=utf-8
from random import randint
from osv import osv, fields
from sms import send_sms

เพิ่มฟิลด์ phone_number ลงในโมเดล HISPatient เพื่อใช้รับ OTP ผ่าน SMS

class HISPatient(osv.Model):
    :
    _columns={
        :
        'phone_number': fields.char(size=20, string=u'โทรศัพท์'),
        :
    }

ในโมเดล HISAppointment เพิ่มฟิลด์ generated_otp และ entered_otp เพื่อใช้เก็บ OTP ที่ถูกสุ่มขึ้นและ OTP ที่ถูกป้อนโดยผู้ใช้ตามลำดับ รวมถึงเมท็อดที่ถูกเรียกใช้เมื่อ workflow ของการนัดหมายเข้าสู่สเตท arrived และเมท็อดสำหรับสร้าง OTP เพื่อส่งไปยังผู้ป่วยผ่าน SMS

class HISAppointment(osv.Model):
    :
    _columns = {
            :
            'generated_otp': fields.char(size=10, string=u'Generated OTP'),
            'entered_otp': fields.char(size=10, string=u'Enter OTP'),
            :
            }

    def action_arrived(self, cr, uid, ids, context=None):
        appointments = self.browse(cr, uid, ids)
        appointment = appointments[0]
        if appointment.generated_otp == appointment.entered_otp:
            self.write(cr, uid, ids, {'state':'arrived'})
        else:
            raise osv.except_osv('Error', 'Incorrect OTP')

    def generate_otp(self, cr, uid, ids, context=None):
        for id in ids:
            appointments = self.browse(cr, uid, [id])
            appointment = appointments[0]
            patient_phone_number = appointment.patient_id.phone_number
            new_otp = str(randint(10000, 99999))
            self.write(cr, uid, [id], {'generated_otp': new_otp})
            send_sms('jittat', '0943fa', '0000', patient_phone_number,
                'Your OTP is: ' + new_otp)

การสร้าง Scheduled Action