Skip to content

Commit

Permalink
fix: cache get_allowed_fieldnames_for_doctype at the request level
Browse files Browse the repository at this point in the history
  • Loading branch information
fahimalizain committed Aug 26, 2022
1 parent 54e0715 commit ede2360
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions frappe_graphql/utils/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def get_allowed_fieldnames_for_doctype(doctype: str, parent_doctype: str = None)
Gets a list of fieldnames that's allowed for the current User to
read on the specified doctype. This includes default_fields
"""
_from_locals = _get_allowed_fieldnames_from_locals(doctype, parent_doctype)
if _from_locals is not None:
return _from_locals

fieldnames = list(default_fields)
fieldnames.remove("doctype")

Expand All @@ -25,6 +29,12 @@ def get_allowed_fieldnames_for_doctype(doctype: str, parent_doctype: str = None)

fieldnames.append(df.fieldname)

_set_allowed_fieldnames_to_locals(
allowed_fields=fieldnames,
doctype=doctype,
parent_doctype=parent_doctype
)

return fieldnames


Expand All @@ -45,3 +55,30 @@ def _get_permlevel_read_access(meta: Meta):
_has_access_to.append(perm.get("permlevel"))

return _has_access_to


def _get_allowed_fieldnames_from_locals(doctype: str, parent_doctype: str = None):

if not hasattr(frappe.local, "permlevel_fields"):
frappe.local.permlevel_fields = dict()

k = doctype
if parent_doctype:
k = (doctype, parent_doctype)

return frappe.local.permlevel_fields.get(k)


def _set_allowed_fieldnames_to_locals(
allowed_fields: List[str],
doctype: str,
parent_doctype: str = None):

if not hasattr(frappe.local, "permlevel_fields"):
frappe.local.permlevel_fields = dict()

k = doctype
if parent_doctype:
k = (doctype, parent_doctype)

frappe.local.permlevel_fields[k] = allowed_fields

0 comments on commit ede2360

Please sign in to comment.