1from django.db.models import signals
2from django.apps import AppConfig
3
4
5class ConstanceConfig(AppConfig):
6    name = 'constance'
7    verbose_name = 'Constance'
8
9    def ready(self):
10        super(ConstanceConfig, self).ready()
11        signals.post_migrate.connect(self.create_perm,
12                                     dispatch_uid='constance.create_perm')
13
14    def create_perm(self, using=None, *args, **kwargs):
15        """
16        Creates a fake content type and permission
17        to be able to check for permissions
18        """
19        from django.conf import settings
20        from django.contrib.auth.models import Permission
21        from django.contrib.contenttypes.models import ContentType
22
23        constance_dbs = getattr(settings, 'CONSTANCE_DBS', None)
24        if constance_dbs is not None and using not in constance_dbs:
25            return
26        if ContentType._meta.installed and Permission._meta.installed:
27            content_type, created = ContentType.objects.using(using).get_or_create(
28                app_label='constance',
29                model='config',
30            )
31
32            permission, created = Permission.objects.using(using).get_or_create(
33                content_type=content_type,
34                codename='change_config',
35                defaults={'name': 'Can change config'})
36