Skip to content

Commit

Permalink
feat(generic): use lru cache for smaller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Feb 20, 2024
1 parent 81a9e02 commit c5c78f7
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apis_core/generic/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def generate_search_filter(model, query, fields_to_search=None):
return q


@functools.lru_cache
def mro_paths(model):
"""
Create a list of MRO classes for a Django model
Expand All @@ -51,6 +52,7 @@ def mro_paths(model):
return paths


@functools.lru_cache
def template_names_via_mro(model, suffix=""):
"""
Use the MRO to generate a list of template names for a model
Expand All @@ -59,6 +61,7 @@ def template_names_via_mro(model, suffix=""):
return [f"{prefix.lower()}{suffix}" for prefix in mro_prefix_list]


@functools.lru_cache
def class_from_path(classpath):
"""
Lookup if the class in `classpath` exists - if so return it,
Expand All @@ -75,6 +78,7 @@ def class_from_path(classpath):
return False


@functools.lru_cache
def first_match_via_mro(model, path: str = "", suffix: str = ""):
"""
Based on the MRO of a Django model, look for classes based on a
Expand All @@ -93,6 +97,7 @@ def first_match_via_mro(model, path: str = "", suffix: str = ""):
return res


@functools.lru_cache
def permission_fullname(action: str, model: object) -> str:
permission_codename = get_permission_codename(action, model._meta)
return f"{model._meta.app_label}.{permission_codename}"

0 comments on commit c5c78f7

Please sign in to comment.