ผลต่างระหว่างรุ่นของ "Openerp/histest"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 125: แถว 125:
 
               action="histest.action_histest_today_appointments"/>
 
               action="histest.action_histest_today_appointments"/>
  
 +
</syntaxhighlight>
 +
 +
=== ไฟล์ histest_workflow.xml ===
 +
กำหนดให้มีการเรียกใช้เมท็อด action_arrived() เมื่อเข้าสู่สเตท arrived
 +
<syntaxhighlight lang="xml">
 +
    <record model="workflow.activity" id="act_arrived">
 +
      <field name="wkf_id" ref="wkf_appointment" />
 +
      <field name="name">arrived</field>
 +
      <field name="kind">function</field>
 +
      <field name="action">action_arrived()</field>
 +
    </record>
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== การสร้าง Scheduled Action ==
 
== การสร้าง Scheduled Action ==

รุ่นแก้ไขเมื่อ 17:05, 2 พฤษภาคม 2557

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

ไฟล์ sms.py

เตรียมฟังก์ชัน send_sms สำหรับส่ง SMS ผ่านเว็บเซอร์วิส thsms.com

import urllib
import urllib2

def send_sms(username, password, fromnumber, tonumber, message):
    data = {'method': 'send',
            'username': username,
            'password': password,
            'from': fromnumber,
            'to': tonumber,
            'message': message,
            }
    encoded_data = urllib.urlencode(data)
    url = 'http://www.thsms.com/api/rest?' + encoded_data
    result = urllib2.urlopen(url)

ไฟล์ 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)

ไฟล์ histest_view.xml

เพิ่มฟิลด์ phone_number ในฟอร์มวิวของโมเดล HISPatient

    <record model="ir.ui.view" id="view_histest_patient_form">
            :
            <group colspan="1">
              <field name="phone_number"/>
              <field name="sex"/>
              <field name="id_number" widget="person_id" />
              <field name="appointment_ids" />
            </group>
            :
    </record>

เพิ่มฟิลด์ generated_otp และ entered_otp ใฟ้ฟอร์มวิวของโมเดล HISAppointment รวมถึงสร้างปุ่ม Generate OTP และทำให้ปุ่มทั้งหมดแสดงผลเฉพาะใน edit mode

    <record model="ir.ui.view" id="view_histest_appointment_form">
          :
          <group>
            <field name="patient_id"/>
            <field name="department_id"/>
            <field name="date"/>
            <field name="state"/>
            <field name="generated_otp"/>
            <field name="entered_otp"/>
            <button name="generate_otp" string="Generate OTP" type="object"
                class="oe_edit_only" />
            <button name="arrive" string="Patient Arrived" states="new"
                class="oe_edit_only" />
            <button name="done" string="Done" states="arrived"
                class="oe_edit_only" />
          </group>
          :
    </record>

เพิ่ม action window เพื่อแสดงผลเฉพาะการนัดหมายของวันที่ปัจจุบัน พร้อม menu item เพื่อเปิดหน้าวิว

    <record model="ir.actions.act_window" id="action_histest_today_appointments">
      <field name="name">Today Appointments</field>
      <field name="res_model">histest.appointment</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="domain">
          [('date','=',context_today().strftime('%Y-%m-%d'))]
      </field>
    </record>

    <menuitem name="การนัดหมายวันนี้" id="menu_histest_today_appointments"
              parent="menu_histest_main" sequence="6"
              action="histest.action_histest_today_appointments"/>

ไฟล์ histest_workflow.xml

กำหนดให้มีการเรียกใช้เมท็อด action_arrived() เมื่อเข้าสู่สเตท arrived

    <record model="workflow.activity" id="act_arrived">
      <field name="wkf_id" ref="wkf_appointment" />
      <field name="name">arrived</field>
      <field name="kind">function</field>
      <field name="action">action_arrived()</field>
    </record>

การสร้าง Scheduled Action