| 1 |
|
| 2 |
from itertools import chain |
| 3 |
|
| 4 |
|
| 5 |
def default_org_model(): |
| 6 |
"""Encapsulates importing the concrete model""" |
| 7 |
from organizations.models import Organization |
| 8 |
|
| 9 |
return Organization |
| 10 |
|
| 11 |
|
| 12 |
def model_field_names(model): |
| 13 |
""" |
| 14 |
Returns a list of field names in the model |
| 15 |
|
| 16 |
Direct from Django upgrade migration guide. |
| 17 |
""" |
| 18 |
return list( |
| 19 |
set( |
| 20 |
chain.from_iterable( |
| 21 |
(field.name, field.attname) |
| 22 |
if hasattr(field, "attname") |
| 23 |
else (field.name,) |
| 24 |
for field in model._meta.get_fields() |
| 25 |
if not (field.many_to_one and field.related_model is None) |
| 26 |
) |
| 27 |
) |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def create_organization( |
| 32 |
user, |
| 33 |
name, |
| 34 |
slug=None, |
| 35 |
is_active=None, |
| 36 |
org_defaults=None, |
| 37 |
org_user_defaults=None, |
| 38 |
**kwargs |
| 39 |
): |
| 40 |
""" |
| 41 |
Returns a new organization, also creating an initial organization user who |
| 42 |
is the owner. |
| 43 |
|
| 44 |
The specific models can be specified if a custom organization app is used. |
| 45 |
The simplest way would be to use a partial. |
| 46 |
|
| 47 |
>>> from organizations.utils import create_organization |
| 48 |
>>> from myapp.models import Account |
| 49 |
>>> from functools import partial |
| 50 |
>>> create_account = partial(create_organization, model=Account) |
| 51 |
|
| 52 |
""" |
| 53 |
org_model = ( |
| 54 |
kwargs.pop("model", None) |
| 55 |
or kwargs.pop("org_model", None) |
| 56 |
or default_org_model() |
| 57 |
) |
| 58 |
kwargs.pop("org_user_model", None) |
| 59 |
|
| 60 |
org_owner_model = org_model.owner.related.related_model |
| 61 |
org_user_model = org_model.organization_users.rel.related_model |
| 62 |
|
| 63 |
if org_defaults is None: |
| 64 |
org_defaults = {} |
| 65 |
if org_user_defaults is None: |
| 66 |
if "is_admin" in model_field_names(org_user_model): |
| 67 |
org_user_defaults = {"is_admin": True} |
| 68 |
else: |
| 69 |
org_user_defaults = {} |
| 70 |
|
| 71 |
if slug is not None: |
| 72 |
org_defaults.update({"slug": slug}) |
| 73 |
if is_active is not None: |
| 74 |
org_defaults.update({"is_active": is_active}) |
| 75 |
|
| 76 |
org_defaults.update({"name": name}) |
| 77 |
organization = org_model.objects.create(**org_defaults) |
| 78 |
|
| 79 |
org_user_defaults.update({"organization": organization, "user": user}) |
| 80 |
new_user = org_user_model.objects.create(**org_user_defaults) |
| 81 |
|
| 82 |
org_owner_model.objects.create( |
| 83 |
organization=organization, organization_user=new_user |
| 84 |
) |
| 85 |
return organization |
| 86 |
|
| 87 |
|
| 88 |
def model_field_attr(model, model_field, attr): |
| 89 |
""" |
| 90 |
Returns the specified attribute for the specified field on the model class. |
| 91 |
""" |
| 92 |
fields = dict([(field.name, field) for field in model._meta.fields]) |
| 93 |
return getattr(fields[model_field], attr) |