forked from inventree/InvenTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.py
More file actions
61 lines (45 loc) · 1.9 KB
/
apps.py
File metadata and controls
61 lines (45 loc) · 1.9 KB
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
55
56
57
58
59
60
61
"""App configuration class for the 'users' app."""
from django.apps import AppConfig
from django.db.utils import OperationalError, ProgrammingError
import structlog
import InvenTree.ready
logger = structlog.get_logger('inventree')
class UsersConfig(AppConfig):
"""Config class for the 'users' app."""
name = 'users'
def ready(self):
"""Called when the 'users' app is loaded at runtime."""
# skip loading if plugin registry is not loaded or we run in a background thread
if (
not InvenTree.ready.isPluginRegistryLoaded()
or not InvenTree.ready.isInMainThread()
):
return
# Skip if running migrations
if InvenTree.ready.isRunningMigrations():
return # pragma: no cover
if InvenTree.ready.canAppAccessDatabase(allow_test=True):
try:
from users.tasks import rebuild_all_permissions
rebuild_all_permissions()
except (OperationalError, ProgrammingError): # pragma: no cover
pass
except Exception as e: # pragma: no cover
logger.exception('Failed to rebuild permissions: %s', e)
try:
self.update_owners()
except (OperationalError, ProgrammingError): # pragma: no cover
pass
except Exception as e: # pragma: no cover
logger.exception('Failed to update owners: %s', e)
def update_owners(self):
"""Create an 'owner' object for each user and group instance."""
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from users.models import Owner
# Create group owners
for group in Group.objects.all():
Owner.create(group)
# Create user owners
for user in get_user_model().objects.all():
Owner.create(user)