fix: workflow mechanics for submission queue (start)
This commit is contained in:
parent
08c8ab0229
commit
bb8b0d415e
3 changed files with 46 additions and 14 deletions
|
|
@ -1,6 +1,7 @@
|
|||
# Copyright (c) 2022, Frappe Technologies and contributors
|
||||
# For license information, please see license.txt
|
||||
|
||||
from typing import Callable
|
||||
from urllib.parse import quote
|
||||
|
||||
from rq import get_current_job
|
||||
|
|
@ -35,10 +36,11 @@ class SubmissionQueue(Document):
|
|||
table = frappe.qb.DocType("Submission Queue")
|
||||
frappe.db.delete(table, filters=(table.modified < (Now() - Interval(days=days))))
|
||||
|
||||
def insert(self, to_be_queued_doc: Document, action: str):
|
||||
def insert(self, to_be_queued_doc: Document, action: str, **job_kwargs):
|
||||
self.status = "Queued"
|
||||
self.to_be_queued_doc = to_be_queued_doc
|
||||
self.action_for_queuing = action
|
||||
self.kwargs = job_kwargs
|
||||
super().insert(ignore_permissions=True)
|
||||
|
||||
def lock(self):
|
||||
|
|
@ -57,12 +59,15 @@ class SubmissionQueue(Document):
|
|||
frappe.db.commit()
|
||||
|
||||
def after_insert(self):
|
||||
self.kwargs.pop("enqueue_after_commit", None)
|
||||
|
||||
self.queue_action(
|
||||
"background_submission",
|
||||
to_be_queued_doc=self.queued_doc,
|
||||
action_for_queuing=self.action_for_queuing,
|
||||
timeout=600,
|
||||
timeout=self.kwargs.pop("timeout", 600),
|
||||
enqueue_after_commit=True,
|
||||
**self.kwargs,
|
||||
)
|
||||
|
||||
def background_submission(self, to_be_queued_doc: Document, action_for_queuing: str):
|
||||
|
|
@ -99,7 +104,7 @@ class SubmissionQueue(Document):
|
|||
else:
|
||||
doctype = self.ref_doctype
|
||||
docname = self.ref_docname
|
||||
message = _("Action {0} completed successfully on {1} {2}. View it {3}")
|
||||
message = _("Action {0} completed on {1} {2}. View it {3}")
|
||||
|
||||
message_replacements = (
|
||||
frappe.bold(action),
|
||||
|
|
@ -145,11 +150,11 @@ class SubmissionQueue(Document):
|
|||
frappe.msgprint(_("Document Unlocked"))
|
||||
|
||||
|
||||
def queue_submission(doc: Document, action: str, alert: bool = True):
|
||||
def queue_submission(doc: Document, action: str, alert: bool = True, **job_kwargs):
|
||||
queue = frappe.new_doc("Submission Queue")
|
||||
queue.ref_doctype = doc.doctype
|
||||
queue.ref_docname = doc.name
|
||||
queue.insert(doc, action)
|
||||
queue.insert(doc, action, **job_kwargs)
|
||||
|
||||
if alert:
|
||||
frappe.msgprint(
|
||||
|
|
|
|||
|
|
@ -136,7 +136,15 @@ def apply_workflow(doc, action):
|
|||
doc.save()
|
||||
elif doc.docstatus.is_draft() and new_docstatus == DocStatus.submitted():
|
||||
if doc.meta.queue_in_background and not is_scheduler_inactive():
|
||||
queue_submission(doc, action="submit")
|
||||
queue_submission(
|
||||
doc,
|
||||
action="submit",
|
||||
on_success=lambda job, connection, result, *args, **kwargs: (
|
||||
doc.add_comment("Workflow", _(next_state.state)),
|
||||
frappe.db.commit(),
|
||||
),
|
||||
)
|
||||
return
|
||||
else:
|
||||
doc.submit()
|
||||
elif doc.docstatus.is_submitted() and new_docstatus == DocStatus.submitted():
|
||||
|
|
@ -257,8 +265,11 @@ def bulk_workflow_approval(docnames, doctype, action):
|
|||
message_dict = {}
|
||||
try:
|
||||
show_progress(docnames, _("Applying: {0}").format(action), idx, docname)
|
||||
apply_workflow(frappe.get_doc(doctype, docname), action)
|
||||
d = apply_workflow(frappe.get_doc(doctype, docname), action)
|
||||
frappe.db.commit()
|
||||
if not d:
|
||||
# for background submission
|
||||
continue
|
||||
except Exception as e:
|
||||
if not frappe.message_log:
|
||||
# Exception is raised manually and not from msgprint or throw
|
||||
|
|
|
|||
|
|
@ -140,20 +140,36 @@ def confirm_action(doctype, docname, user, action):
|
|||
doc = frappe.get_doc(doctype, docname)
|
||||
newdoc = apply_workflow(doc, action)
|
||||
frappe.db.commit()
|
||||
return_success_page(newdoc)
|
||||
|
||||
return_response_page(doc, not newdoc)
|
||||
|
||||
# reset session user
|
||||
if logged_in_user == "Guest":
|
||||
frappe.set_user(logged_in_user)
|
||||
|
||||
|
||||
def return_success_page(doc):
|
||||
def return_response_page(doc, is_background=False):
|
||||
if is_background:
|
||||
title = _("Pending")
|
||||
message = (
|
||||
_("{0}: {1} is added in queue to set to next state").format(
|
||||
doc.get("doctype"), frappe.bold(doc.get("name"))
|
||||
),
|
||||
)
|
||||
color = "orange"
|
||||
else:
|
||||
title = _("Success")
|
||||
message = (
|
||||
_("{0}: {1} is set to state {2}").format(
|
||||
doc.get("doctype"), frappe.bold(doc.get("name")), frappe.bold(get_doc_workflow_state(doc))
|
||||
),
|
||||
)
|
||||
color = "green"
|
||||
|
||||
frappe.respond_as_web_page(
|
||||
_("Success"),
|
||||
_("{0}: {1} is set to state {2}").format(
|
||||
doc.get("doctype"), frappe.bold(doc.get("name")), frappe.bold(get_doc_workflow_state(doc))
|
||||
),
|
||||
indicator_color="green",
|
||||
title,
|
||||
message,
|
||||
indicator_color=color,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue