blob: 70fd775e77d5d3b9269e9ee6bd573e6c015f0227 [file] [log] [blame]
mblighe8819cd2008-02-15 16:48:40 +00001# use some undocumented Django tricks to execute custom logic after syncdb
2
mblighe8819cd2008-02-15 16:48:40 +00003from django.db.models import signals
mblighe8819cd2008-02-15 16:48:40 +00004from django.contrib import auth
Aviv Keshet62cf0472013-05-04 22:40:17 -07005from django.conf import settings
showard043abc12009-01-14 21:41:51 +00006# In this file, it is critical that we import models *just like this*. In
7# particular, we *cannot* do import common; from autotest_lib... import models.
showarda5288b42009-07-28 20:06:08 +00008# This is because when we pass the models module to signal.connect(), it
showard043abc12009-01-14 21:41:51 +00009# calls id() on the module, and the id() of a module can differ depending on how
10# it was imported. For that reason, we must import models as Django does -- not
11# through the autotest_lib magic set up through common.py. If you do that, the
12# connection won't work and the dispatcher will simply never call the method.
13from frontend.afe import models
mblighe8819cd2008-02-15 16:48:40 +000014
15BASIC_ADMIN = 'Basic admin'
16
17def create_admin_group(app, created_models, verbosity, **kwargs):
jadmanski0afbb632008-06-06 21:10:57 +000018 """\
19 Create a basic admin group with permissions for managing basic autotest
20 objects.
21 """
22 admin_group, created = auth.models.Group.objects.get_or_create(
23 name=BASIC_ADMIN)
24 admin_group.save() # must save before adding permissions
25 PermissionModel = auth.models.Permission
26 have_permissions = list(admin_group.permissions.all())
showardd1677eb2009-04-16 03:09:01 +000027 for model_name in ('host', 'label', 'test', 'aclgroup', 'profiler',
28 'atomicgroup'):
jadmanski0afbb632008-06-06 21:10:57 +000029 for permission_type in ('add', 'change', 'delete'):
30 codename = permission_type + '_' + model_name
31 permissions = list(PermissionModel.objects.filter(
32 codename=codename))
33 if len(permissions) == 0:
Aviv Keshet3986bac2013-05-21 13:55:01 -070034 if verbosity:
35 print ' No permission ' + codename
jadmanski0afbb632008-06-06 21:10:57 +000036 continue
37 for permission in permissions:
38 if permission not in have_permissions:
Aviv Keshet3986bac2013-05-21 13:55:01 -070039 if verbosity:
40 print ' Adding permission ' + codename
jadmanski0afbb632008-06-06 21:10:57 +000041 admin_group.permissions.add(permission)
Aviv Keshet3986bac2013-05-21 13:55:01 -070042 if verbosity:
43 if created:
44 print 'Created group "%s"' % BASIC_ADMIN
45 else:
46 print 'Group "%s" already exists' % BASIC_ADMIN
mblighe8819cd2008-02-15 16:48:40 +000047
Aviv Keshet62cf0472013-05-04 22:40:17 -070048if settings.AUTOTEST_CREATE_ADMIN_GROUPS:
49 signals.post_syncdb.connect(create_admin_group, sender=models)