2024-03-21 14:34:37 -04:00
|
|
|
from django.db.models import ManyToOneRel
|
2023-01-06 09:42:13 -05:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.timezone import localtime
|
2018-02-02 13:32:16 -05:00
|
|
|
|
2022-11-02 16:26:26 -04:00
|
|
|
|
2018-05-30 11:19:10 -04:00
|
|
|
def dynamic_import(name):
|
|
|
|
"""
|
|
|
|
Dynamically import a class from an absolute path string
|
|
|
|
"""
|
|
|
|
components = name.split('.')
|
|
|
|
mod = __import__(components[0])
|
|
|
|
for comp in components[1:]:
|
|
|
|
mod = getattr(mod, comp)
|
|
|
|
return mod
|
2018-06-22 14:00:23 -04:00
|
|
|
|
|
|
|
|
2023-01-06 09:42:13 -05:00
|
|
|
def local_now():
|
|
|
|
"""
|
|
|
|
Return the current date & time in the system timezone.
|
|
|
|
"""
|
|
|
|
return localtime(timezone.now())
|
2023-11-16 12:02:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_related_models(model, ordered=True):
|
|
|
|
"""
|
|
|
|
Return a list of all models which have a ForeignKey to the given model and the name of the field. For example,
|
|
|
|
`get_related_models(Tenant)` will return all models which have a ForeignKey relationship to Tenant.
|
|
|
|
"""
|
|
|
|
related_models = [
|
|
|
|
(field.related_model, field.remote_field.name)
|
|
|
|
for field in model._meta.related_objects
|
|
|
|
if type(field) is ManyToOneRel
|
|
|
|
]
|
|
|
|
|
|
|
|
if ordered:
|
2023-12-01 11:23:38 -05:00
|
|
|
return sorted(related_models, key=lambda x: x[0]._meta.verbose_name.lower())
|
2023-11-16 12:02:32 -05:00
|
|
|
|
|
|
|
return related_models
|