File: utils.py

package info (click to toggle)
django-invitations 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 456 kB
  • sloc: python: 1,484; makefile: 27; sh: 6
file content (54 lines) | stat: -rw-r--r-- 1,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django.apps import apps as django_apps
from django.core.exceptions import ImproperlyConfigured

from .app_settings import app_settings

try:
    import importlib
except ImportError:
    from django.utils import importlib


def import_attribute(path):
    assert isinstance(path, str)
    pkg, attr = path.rsplit(".", 1)
    ret = getattr(importlib.import_module(pkg), attr)
    return ret


def get_invite_form():
    """
    Returns the form for sending an invite.
    """
    return import_attribute(app_settings.INVITE_FORM)


def get_invitation_admin_add_form():
    """
    Returns the form for creating a new invitation in admin.
    """
    return import_attribute(app_settings.ADMIN_ADD_FORM)


def get_invitation_admin_change_form():
    """
    Returns the form for changing invitations in admin.
    """
    return import_attribute(app_settings.ADMIN_CHANGE_FORM)


def get_invitation_model():
    """
    Returns the Invitation model that is active in this project.
    """
    path = app_settings.INVITATION_MODEL
    try:
        return django_apps.get_model(path)
    except ValueError:
        raise ImproperlyConfigured("path must be of the form 'app_label.model_name'")
    except LookupError:
        raise ImproperlyConfigured(
            "path refers to model '%s' that\
             has not been installed"
            % app_settings.INVITATION_MODEL,
        )