postgresql 根据基本文档类型将对象保存在另一个文档类型中?

hec6srdp  于 2023-06-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(129)

我正在特灵触发一个事件来保存基于另一个doctype的doctype数据。
我的doctype是这样的:
1.员工具有工作角色。
1.岗位职责有两种单据类型,分别为“岗位职责课程项目”表和“岗位职责技能项目”表。
1.“工作角色课程项目”是与“课程模板”链接的表类型。
1.“工作角色技能项”是与“技能”链接的表类型。
我想要达到的是:
1.当使用将数据保存到雇员文档类型中的db usign Frappe时,我想将数据也保存到另外两个文档类型“课程分配”和“雇员技能”中。
1.这将通过使用与Employee DocType as Table字段链接的Job Role来实现。
1.我还有一个问题,当我第一次保存DocType时,它告诉我DocType不存在。

请注意:

我的代码是工作,我需要的是取代内部的搜索只是为课程或技能的“工作角色”,“课程分配”或“员工技能”,这是不是在“课程分配”和“员工技能”的基础上的名称和员工。
这是我为Employee DocType编写的全部代码。

import frappe
from frappe import _
from frappe.model.document import Document

class Employee(Document):
    def before_save(self):
        if not self.full_name:
            self.full_name = ((self.first_name + ' ' if self.first_name else '') + (self.middle_name + ' ' if self.middle_name else '') + (self.last_name if self.last_name else '')).strip()
        if self._doc_before_save:
            if self._doc_before_save.job_roles != self.job_roles: self.trigger_job_roles()
        # DocType dosn't exsists if the DocType of saved as first time.
        else: self.trigger_job_roles()

    def validate(self):
        if (self.work_start_date and self.work_end_date):
            if (self.work_start_date >= self.work_end_date):
                frappe.throw(_('The Work End Date should be greater than the Work Start Date'))

    def trigger_job_roles(self):
        frappe.enqueue(
            "medad_tms.trainee_management.doctype.employee.employee.assign_employee",
            doc=self,
        )

def assign_employee(doc):
    try:
        for job_role in doc.job_roles:
            for course in frappe.get_doc("Job Role", job_role.job_role).required_courses: # I want to replace this to enhance the code performace.
                if not frappe.db.exists("Course Assignment", f"{course.course}-{doc.related_user}"):
                    course_doc = frappe.new_doc("Course Assignment")
                    course_doc.trainee = doc.related_user
                    course_doc.course = course.course
                    course_doc.source = "Job Role"
                    course_doc.due_date = frappe.get_doc("Course Template", course.course).start_date
                    course_doc.insert()
            for skill in frappe.get_doc("Job Role", job_role.job_role).required_skills: # I want to replace this to enhance the code performace.
                if not frappe.db.exists("Employee Skill", f"{doc.name}-{skill.skill}"):
                    skill_doc = frappe.new_doc("Employee Skill")
                    skill_doc.employee = doc.name
                    skill_doc.skill = skill.skill
                    skill_doc.skill_type = "Training Programs"
                    skill_doc.proficiency_scale_level = 1
                    skill_doc.required_scale_level = 5
                    skill_doc.insert()
        frappe.db.commit()
        frappe.publish_realtime(
            "assign_employee",
            {"progress": 1, "total": 3,
                "message": "Assigning Courses and Skills to Employee"},
            user=frappe.session.user,
            after_commit=True,
        )
    except Exception:
        frappe.db.rollback()
        frappe.log_error(frappe.get_traceback(), "Employee")
        frappe.throw(_("Error in Assigning Courses and Skills to Employee"))
ds97pgxw

ds97pgxw1#

为了优化代码,您提到要替换从'Job Role' doctype检索课程和技能的内部循环。假设您只希望检索“课程分配”和“员工技能”文档类型中尚未存在的课程和技能

def assign_employee(doc):
try:
    for job_role in doc.job_roles:
        job_role_doc = frappe.get_doc("Job Role", job_role.job_role)
        course_assignment_names = [ca.course for ca in frappe.get_all("Course Assignment", filters={"trainee": doc.related_user}, pluck="course")]
        employee_skill_names = [es.skill for es in frappe.get_all("Employee Skill", filters={"employee": doc.name}, pluck="skill")]

        for course_item in job_role_doc.required_courses:
            course = course_item.course
            if course not in course_assignment_names:
                course_doc = frappe.new_doc("Course Assignment")
                course_doc.trainee = doc.related_user
                course_doc.course = course
                course_doc.source = "Job Role"
                course_doc.due_date = frappe.get_value("Course Template", course, "start_date")
                course_doc.insert()

        for skill_item in job_role_doc.required_skills:
            skill = skill_item.skill
            if skill not in employee_skill_names:
                skill_doc = frappe.new_doc("Employee Skill")
                skill_doc.employee = doc.name
                skill_doc.skill = skill
                skill_doc.skill_type = "Training Programs"
                skill_doc.proficiency_scale_level = 1
                skill_doc.required_scale_level = 5
                skill_doc.insert()

    frappe.db.commit()
    frappe.publish_realtime(
        "assign_employee",
        {"progress": 1, "total": 3, "message": "Assigning Courses and Skills to Employee"},
        user=frappe.session.user,
        after_commit=True,
    )
except Exception:
    frappe.db.rollback()
    frappe.log_error(frappe.get_traceback(), "Employee")
    frappe.throw(_("Error in Assigning Courses and Skills to Employee"))

相关问题