Skip to content

Commit

Permalink
Merge pull request #5 from alyf-de/develop
Browse files Browse the repository at this point in the history
feat: use title as subfolder name (#4)
  • Loading branch information
barredterra authored Jan 13, 2021
2 parents 38f77cb + 2d8143c commit 07f75d1
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 414 deletions.
64 changes: 34 additions & 30 deletions pdf_on_submit/attach_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,33 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import frappe

from frappe import _
from frappe import publish_progress
from frappe.core.doctype.file.file import create_new_folder
from frappe.utils.file_manager import save_file


def attach_pdf(doc, event=None):
settings = frappe.get_single("PDF on Submit Settings")
enabled = [row.document_type for row in settings.enabled_for]

if doc.doctype not in enabled:
return

fallback_language = frappe.db.get_single_value("System Settings", "language") or "en"
args = {
"doctype": doc.doctype,
"name": doc.name,
"party": getattr(doc, "customer", _("Unknown")),
"lang": getattr(doc, "language", "en")
"title": doc.get_title(),
"lang": getattr(doc, "language", fallback_language),
"show_progress": not settings.create_pdf_in_background
}

if doc.doctype == "Quotation":
args["party"] = doc.party_name

settings = frappe.get_single("PDF on Submit Settings")
slug = "_".join(doc.doctype.lower().split(" ")) # "Sales Invoice" -> "sales_invoice"

if settings.get(slug):
if settings.create_pdf_in_background:
enqueue(args)
else:
execute(**args)
if settings.create_pdf_in_background:
enqueue(args)
else:
execute(**args)


def enqueue(args):
Expand All @@ -46,44 +51,44 @@ def enqueue(args):
timeout=30, is_async=True, **args)


def execute(doctype, name, party, lang=None):
def execute(doctype, name, title, lang=None, show_progress=True):
"""
Queue calls this method, when it's ready.
1. Create necessary folders
2. Get raw PDF data
3. Save PDF file and attach it to the document
"""
settings = frappe.get_single("PDF on Submit Settings")
show_progress = not settings.create_pdf_in_background
progress_title = _("Creating PDF ...")
progress = frappe._dict(title=_("Creating PDF ..."), percent=0, doctype=doctype, docname=name)

if lang:
frappe.local.lang = lang

if show_progress:
publish_progress(percent=0, title=progress_title)
publish_progress(**progress)

doctype_folder = create_folder(_(doctype), "Home")
party_folder = create_folder(party, doctype_folder)
title_folder = create_folder(title, doctype_folder)

if show_progress:
publish_progress(percent=33, title=progress_title)

progress.percent = 33
publish_progress(**progress)

pdf_data = get_pdf_data(doctype, name)

if show_progress:
publish_progress(percent=66, title=progress_title)

save_and_attach(pdf_data, doctype, name, party_folder)

progress.percent = 66
publish_progress(**progress)

save_and_attach(pdf_data, doctype, name, title_folder)

if show_progress:
publish_progress(percent=100, title=progress_title)
progress.percent = 100
publish_progress(**progress)


def create_folder(folder, parent):
"""Make sure the folder exists and return it's name."""
from frappe.core.doctype.file.file import create_new_folder
new_folder_name = "/".join([parent, folder])

if not frappe.db.exists("File", new_folder_name):
Expand All @@ -104,7 +109,6 @@ def save_and_attach(content, to_doctype, to_name, folder):
File document is linked to another document.
"""
from frappe.utils.file_manager import save_file
file_name = "{}.pdf".format(to_name.replace(" ", "-").replace("/", "-"))
save_file(file_name, content, to_doctype,
to_name, folder=folder, is_private=1)
14 changes: 1 addition & 13 deletions pdf_on_submit/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,7 @@
# "on_cancel": "method",
# "on_trash": "method"
# }
"Quotation": {
"on_submit": "pdf_on_submit.attach_pdf.attach_pdf"
},
"Sales Order": {
"on_submit": "pdf_on_submit.attach_pdf.attach_pdf"
},
"Sales Invoice": {
"on_submit": "pdf_on_submit.attach_pdf.attach_pdf"
},
"Delivery Note": {
"on_submit": "pdf_on_submit.attach_pdf.attach_pdf"
},
"Dunning": {
"*": {
"on_submit": "pdf_on_submit.attach_pdf.attach_pdf"
}
}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"actions": [],
"creation": "2021-01-13 15:41:24.310478",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"document_type"
],
"fields": [
{
"fieldname": "document_type",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Document Type",
"options": "DocType",
"reqd": 1
}
],
"istable": 1,
"links": [],
"modified": "2021-01-13 15:41:24.310478",
"modified_by": "Administrator",
"module": "PDF on Submit",
"name": "Enabled DocType",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021, Raffael Meyer and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
# import frappe
from frappe.model.document import Document

class EnabledDocType(Document):
pass
Loading

0 comments on commit 07f75d1

Please sign in to comment.