blob: 4f6066cd810299be1233fcb524273da0ccf3001c [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
showard043abc12009-01-14 21:41:51 +00005# In this file, it is critical that we import models *just like this*. In
6# particular, we *cannot* do import common; from autotest_lib... import models.
showarda5288b42009-07-28 20:06:08 +00007# This is because when we pass the models module to signal.connect(), it
showard043abc12009-01-14 21:41:51 +00008# calls id() on the module, and the id() of a module can differ depending on how
9# it was imported. For that reason, we must import models as Django does -- not
10# through the autotest_lib magic set up through common.py. If you do that, the
11# connection won't work and the dispatcher will simply never call the method.
12from frontend.afe import models
mblighe8819cd2008-02-15 16:48:40 +000013
14BASIC_ADMIN = 'Basic admin'
15
16def create_admin_group(app, created_models, verbosity, **kwargs):
jadmanski0afbb632008-06-06 21:10:57 +000017 """\
18 Create a basic admin group with permissions for managing basic autotest
19 objects.
20 """
21 admin_group, created = auth.models.Group.objects.get_or_create(
22 name=BASIC_ADMIN)
23 admin_group.save() # must save before adding permissions
24 PermissionModel = auth.models.Permission
25 have_permissions = list(admin_group.permissions.all())
showardd1677eb2009-04-16 03:09:01 +000026 for model_name in ('host', 'label', 'test', 'aclgroup', 'profiler',
27 'atomicgroup'):
jadmanski0afbb632008-06-06 21:10:57 +000028 for permission_type in ('add', 'change', 'delete'):
29 codename = permission_type + '_' + model_name
30 permissions = list(PermissionModel.objects.filter(
31 codename=codename))
32 if len(permissions) == 0:
33 print ' No permission ' + codename
34 continue
35 for permission in permissions:
36 if permission not in have_permissions:
37 print ' Adding permission ' + codename
38 admin_group.permissions.add(permission)
39 if created:
40 print 'Created group "%s"' % BASIC_ADMIN
41 else:
42 print 'Group "%s" already exists' % BASIC_ADMIN
mblighe8819cd2008-02-15 16:48:40 +000043
44
showarda5288b42009-07-28 20:06:08 +000045signals.post_syncdb.connect(create_admin_group, sender=models)