refactor(whitelisted): Add typing hints to APIs
* Refactor type checks defined in APIs * Remove dead/deprecated kwargs usages * Added appropriate hints to APIs and consecutive utils defined in the following modules: - frappe.realtime - frappe.translate - frappe.utils.global_search - frappe.www.third_party_apps - frappe.www.search - frappe.www.printview
This commit is contained in:
parent
fa88d5f7d9
commit
d3250f6504
8 changed files with 50 additions and 51 deletions
|
|
@ -744,7 +744,7 @@ def test_password_strength(new_password: str, user_data: tuple = None):
|
|||
if new_password:
|
||||
result = _test_password_strength(new_password, user_inputs=user_data)
|
||||
password_policy_validation_passed = False
|
||||
minimum_password_score = frappe.get_system_settings("minimum_password_score") or 0
|
||||
minimum_password_score = cint(frappe.get_system_settings("minimum_password_score")) or 0
|
||||
|
||||
# score should be greater than 0 and minimum_password_score
|
||||
if result.get("score") and result.get("score") >= minimum_password_score:
|
||||
|
|
|
|||
|
|
@ -673,7 +673,6 @@ frappe.ui.form.PrintView = class {
|
|||
args: {
|
||||
doc: this.frm.doc,
|
||||
print_format: this.selected_format(),
|
||||
_lang: this.lang_code,
|
||||
},
|
||||
callback: function (r) {
|
||||
if (!r.exc) {
|
||||
|
|
|
|||
|
|
@ -103,10 +103,7 @@ def get_redis_server():
|
|||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def can_subscribe_doc(doctype, docname):
|
||||
if os.environ.get("CI"):
|
||||
return True
|
||||
|
||||
def can_subscribe_doc(doctype: str, docname: str) -> bool:
|
||||
from frappe.exceptions import PermissionError
|
||||
from frappe.sessions import Session
|
||||
|
||||
|
|
@ -118,7 +115,7 @@ def can_subscribe_doc(doctype, docname):
|
|||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def can_subscribe_list(doctype):
|
||||
def can_subscribe_list(doctype: str) -> bool:
|
||||
from frappe.exceptions import PermissionError
|
||||
|
||||
if not frappe.has_permission(user=frappe.session.user, doctype=doctype, ptype="read"):
|
||||
|
|
|
|||
|
|
@ -1279,7 +1279,7 @@ def get_translator_url():
|
|||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def get_all_languages(with_language_name=False):
|
||||
def get_all_languages(with_language_name: bool = False) -> list:
|
||||
"""Returns all enabled language codes ar, ch etc"""
|
||||
|
||||
def get_language_codes():
|
||||
|
|
@ -1298,7 +1298,7 @@ def get_all_languages(with_language_name=False):
|
|||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def set_preferred_language_cookie(preferred_language):
|
||||
def set_preferred_language_cookie(preferred_language: str):
|
||||
frappe.local.cookie_manager.set_cookie("preferred_language", preferred_language)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -489,7 +489,7 @@ def search(text, start=0, limit=20, doctype=""):
|
|||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def web_search(text, scope=None, start=0, limit=20):
|
||||
def web_search(text: str, scope: str = None, start: int = 0, limit: int = 20):
|
||||
"""
|
||||
Search for given text in __global_search where published = 1
|
||||
:param text: phrase to be searched
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import copy
|
|||
import json
|
||||
import os
|
||||
import re
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import frappe
|
||||
from frappe import _, get_module_path
|
||||
|
|
@ -13,6 +14,10 @@ from frappe.core.doctype.document_share_key.document_share_key import is_expired
|
|||
from frappe.utils import cint, sanitize_html, strip_html
|
||||
from frappe.utils.jinja_globals import is_rtl
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from frappe.model.document import Document
|
||||
from frappe.printing.doctype.print_format.print_format import PrintFormat
|
||||
|
||||
no_cache = 1
|
||||
|
||||
standard_format = "templates/print_formats/standard.html"
|
||||
|
|
@ -88,13 +93,12 @@ def get_print_format_doc(print_format_name, meta):
|
|||
|
||||
|
||||
def get_rendered_template(
|
||||
doc,
|
||||
name=None,
|
||||
print_format=None,
|
||||
doc: "Document",
|
||||
print_format: str = None,
|
||||
meta=None,
|
||||
no_letterhead=None,
|
||||
letterhead=None,
|
||||
trigger_print=False,
|
||||
no_letterhead: bool = None,
|
||||
letterhead: str = None,
|
||||
trigger_print: bool = False,
|
||||
settings=None,
|
||||
):
|
||||
|
||||
|
|
@ -184,7 +188,7 @@ def get_rendered_template(
|
|||
letter_head.footer, {"doc": doc.as_dict()}
|
||||
)
|
||||
|
||||
convert_markdown(doc, meta)
|
||||
convert_markdown(doc)
|
||||
|
||||
args = {}
|
||||
# extract `print_heading_template` from the first field and remove it
|
||||
|
|
@ -257,9 +261,9 @@ def set_title_values_for_table_and_multiselect_fields(meta, doc):
|
|||
set_title_values_for_link_and_dynamic_link_fields(_meta, value, doc)
|
||||
|
||||
|
||||
def convert_markdown(doc, meta):
|
||||
def convert_markdown(doc: "Document"):
|
||||
"""Convert text field values to markdown if necessary"""
|
||||
for field in meta.fields:
|
||||
for field in doc.meta.fields:
|
||||
if field.fieldtype == "Text Editor":
|
||||
value = doc.get(field.fieldname)
|
||||
if value and "<!-- markdown -->" in value:
|
||||
|
|
@ -268,34 +272,30 @@ def convert_markdown(doc, meta):
|
|||
|
||||
@frappe.whitelist()
|
||||
def get_html_and_style(
|
||||
doc,
|
||||
name=None,
|
||||
print_format=None,
|
||||
meta=None,
|
||||
no_letterhead=None,
|
||||
letterhead=None,
|
||||
trigger_print=False,
|
||||
style=None,
|
||||
settings=None,
|
||||
templates=None,
|
||||
doc: str,
|
||||
name: str = None,
|
||||
print_format: str = None,
|
||||
no_letterhead: bool = None,
|
||||
letterhead: str = None,
|
||||
trigger_print: bool = False,
|
||||
style: str = None,
|
||||
settings: str = None,
|
||||
):
|
||||
"""Returns `html` and `style` of print format, used in PDF etc"""
|
||||
|
||||
if isinstance(doc, str) and isinstance(name, str):
|
||||
doc = frappe.get_doc(doc, name)
|
||||
if isinstance(name, str):
|
||||
document = frappe.get_doc(doc, name)
|
||||
else:
|
||||
document = frappe.get_doc(json.loads(doc))
|
||||
|
||||
if isinstance(doc, str):
|
||||
doc = frappe.get_doc(json.loads(doc))
|
||||
|
||||
print_format = get_print_format_doc(print_format, meta=meta or frappe.get_meta(doc.doctype))
|
||||
set_link_titles(doc)
|
||||
print_format = get_print_format_doc(print_format, meta=document.meta)
|
||||
set_link_titles(document)
|
||||
|
||||
try:
|
||||
html = get_rendered_template(
|
||||
doc,
|
||||
name=name,
|
||||
doc=document,
|
||||
print_format=print_format,
|
||||
meta=meta,
|
||||
meta=document.meta,
|
||||
no_letterhead=no_letterhead,
|
||||
letterhead=letterhead,
|
||||
trigger_print=trigger_print,
|
||||
|
|
@ -309,16 +309,15 @@ def get_html_and_style(
|
|||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_rendered_raw_commands(doc, name=None, print_format=None, meta=None, lang=None):
|
||||
def get_rendered_raw_commands(doc: str, name: str = None, print_format: str = None):
|
||||
"""Returns Rendered Raw Commands of print format, used to send directly to printer"""
|
||||
|
||||
if isinstance(doc, str) and isinstance(name, str):
|
||||
doc = frappe.get_doc(doc, name)
|
||||
if isinstance(name, str):
|
||||
document = frappe.get_doc(doc, name)
|
||||
else:
|
||||
document = frappe.get_doc(json.loads(doc))
|
||||
|
||||
if isinstance(doc, str):
|
||||
doc = frappe.get_doc(json.loads(doc))
|
||||
|
||||
print_format = get_print_format_doc(print_format, meta=meta or frappe.get_meta(doc.doctype))
|
||||
print_format = get_print_format_doc(print_format, meta=document.meta)
|
||||
|
||||
if not print_format or (print_format and not print_format.raw_printing):
|
||||
frappe.throw(
|
||||
|
|
@ -326,7 +325,9 @@ def get_rendered_raw_commands(doc, name=None, print_format=None, meta=None, lang
|
|||
)
|
||||
|
||||
return {
|
||||
"raw_commands": get_rendered_template(doc, name=name, print_format=print_format, meta=meta)
|
||||
"raw_commands": get_rendered_template(
|
||||
doc=document, name=name, print_format=print_format, meta=document.meta
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -361,7 +362,7 @@ def validate_key(key, doc):
|
|||
raise frappe.exceptions.InvalidKeyError
|
||||
|
||||
|
||||
def get_letter_head(doc, no_letterhead, letterhead=None):
|
||||
def get_letter_head(doc: "Document", no_letterhead: bool, letterhead: str = None):
|
||||
if no_letterhead:
|
||||
return {}
|
||||
if letterhead:
|
||||
|
|
@ -519,7 +520,9 @@ def has_value(df, doc):
|
|||
return True
|
||||
|
||||
|
||||
def get_print_style(style=None, print_format=None, for_legacy=False):
|
||||
def get_print_style(
|
||||
style: str = None, print_format: "PrintFormat" = None, for_legacy: bool = False
|
||||
):
|
||||
print_settings = frappe.get_doc("Print Settings")
|
||||
|
||||
if not style:
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ def get_context(context):
|
|||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def get_search_results(text, scope=None, start=0, as_html=False):
|
||||
def get_search_results(text: str, scope: str = None, start: int = 0, as_html: bool = False):
|
||||
results = web_search(text, scope, start, limit=21)
|
||||
out = frappe._dict()
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ def get_first_login(client):
|
|||
|
||||
|
||||
@frappe.whitelist()
|
||||
def delete_client(client_id):
|
||||
def delete_client(client_id: str):
|
||||
active_client_id_tokens = frappe.get_all(
|
||||
"OAuth Bearer Token", filters=[["user", "=", frappe.session.user], ["client", "=", client_id]]
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue