mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | # use some undocumented Django tricks to execute custom logic after syncdb |
| 2 | |
| 3 | from django.dispatch import dispatcher |
| 4 | from django.db.models import signals |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 5 | from django.contrib import auth |
showard | 043abc1 | 2009-01-14 21:41:51 +0000 | [diff] [blame] | 6 | # 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. |
| 8 | # This is becasue when we pass the models module to dispatcher.connect(), it |
| 9 | # 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. |
| 13 | from frontend.afe import models |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 14 | |
| 15 | BASIC_ADMIN = 'Basic admin' |
| 16 | |
| 17 | def create_admin_group(app, created_models, verbosity, **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 18 | """\ |
| 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()) |
showard | d1677eb | 2009-04-16 03:09:01 +0000 | [diff] [blame] | 27 | for model_name in ('host', 'label', 'test', 'aclgroup', 'profiler', |
| 28 | 'atomicgroup'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 29 | 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: |
| 34 | print ' No permission ' + codename |
| 35 | continue |
| 36 | for permission in permissions: |
| 37 | if permission not in have_permissions: |
| 38 | print ' Adding permission ' + codename |
| 39 | admin_group.permissions.add(permission) |
| 40 | if created: |
| 41 | print 'Created group "%s"' % BASIC_ADMIN |
| 42 | else: |
| 43 | print 'Group "%s" already exists' % BASIC_ADMIN |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 44 | |
| 45 | |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 46 | dispatcher.connect(create_admin_group, sender=models, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 47 | signal=signals.post_syncdb) |