Python swapper.get_model_name() Examples

The following are 10 code examples of swapper.get_model_name(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module swapper , or try the search function .
Example #1
Source File: __init__.py    From openwisp-users with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def set_default_organization_uuid(apps, schema_editor):
    """
    Get or create a default organization then
    set settings._OPENWISP_DEFAULT_ORG_UUID
    """
    org_model = swapper.get_model_name('openwisp_users', 'organization')
    model_app_label = swapper.split(org_model)[0]
    organization = apps.get_model(model_app_label, 'organization')
    default_organization = organization.objects.first()
    if default_organization is None:
        default_organization = organization(
            name='default',
            slug='default',
            description='This is the default organization. '
            'It was created automatically during installation. '
            'You can simply rename it to your organization name.',
        )
        default_organization.full_clean()
        default_organization.save()

    # settings._OPENWISP_DEFAULT_ORG_UUID is used in
    # openwisp-radius.migrations, it helps to enable
    # users to migrate from freeradius 3
    settings._OPENWISP_DEFAULT_ORG_UUID = default_organization.pk 
Example #2
Source File: __init__.py    From openwisp-users with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def update_admins_permissions(apps, schema_editor):
    org_model = swapper.get_model_name('openwisp_users', 'organization')
    model_app_label = swapper.split(org_model)[0]
    group = apps.get_model(model_app_label, 'group')
    email_model = swapper.get_model_name('account', 'EmailAddress')
    email_app_label = swapper.split(email_model)[0]
    try:
        admin = group.objects.get(name='Administrator')
        permissions = [
            Permission.objects.get(
                content_type__app_label=email_app_label, codename='view_emailaddress'
            ).pk,
            Permission.objects.get(
                content_type__app_label=email_app_label, codename='delete_emailaddress'
            ).pk,
            Permission.objects.get(
                content_type__app_label=email_app_label, codename='change_emailaddress'
            ).pk,
            Permission.objects.get(
                content_type__app_label=model_app_label, codename='delete_user'
            ).pk,
        ]
        admin.permissions.add(*permissions)
    except ObjectDoesNotExist:
        pass 
Example #3
Source File: apps.py    From openwisp-users with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_default_menu_items(self):
        menu_setting = 'OPENWISP_DEFAULT_ADMIN_MENU_ITEMS'
        items = [
            {'model': settings.AUTH_USER_MODEL},
            {'model': get_model_name('openwisp_users', 'Organization')},
        ]
        if not hasattr(settings, menu_setting):
            setattr(settings, menu_setting, items)
        else:
            current_menu = getattr(settings, menu_setting)
            current_menu += items 
Example #4
Source File: __init__.py    From django-freeradius with GNU General Public License v3.0 5 votes vote down vote up
def get_swapped_model(apps, app_name, model_name):
    model_path = swapper.get_model_name(app_name, model_name)
    return get_model(apps, model_path) 
Example #5
Source File: apps.py    From openwisp-controller with GNU General Public License v3.0 5 votes vote down vote up
def add_default_menu_items(self):
        menu_setting = 'OPENWISP_DEFAULT_ADMIN_MENU_ITEMS'
        items = [
            {'model': get_model_name('config', 'Device')},
            {'model': get_model_name('config', 'Template')},
            {'model': get_model_name('config', 'Vpn')},
        ]
        if not hasattr(settings, menu_setting):
            setattr(settings, menu_setting, items)
        else:
            current_menu = getattr(settings, menu_setting)
            current_menu += items 
Example #6
Source File: test_swapper.py    From django-swappable-models with MIT License 5 votes vote down vote up
def test_swap_setting(self):
        self.assertTrue(swapper.is_swapped("default_app", "Type"))
        self.assertEqual(
            swapper.get_model_name("default_app", "Type"),
            "alt_app.Type"
        ) 
Example #7
Source File: test_swapper.py    From django-swappable-models with MIT License 5 votes vote down vote up
def test_default_setting(self):
        self.assertFalse(swapper.is_swapped("default_app", "Type"))
        self.assertEqual(
            swapper.get_model_name("default_app", "Type"),
            "default_app.Type"
        ) 
Example #8
Source File: apps.py    From openwisp-network-topology with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_default_menu_items(self):
        menu_setting = 'OPENWISP_DEFAULT_ADMIN_MENU_ITEMS'
        items = [{'model': swapper.get_model_name('topology', 'Topology')}]
        if not hasattr(settings, menu_setting):
            setattr(settings, menu_setting, items)
        else:
            current_menu = getattr(settings, menu_setting)
            current_menu += items 
Example #9
Source File: __init__.py    From openwisp-radius with GNU General Public License v3.0 5 votes vote down vote up
def get_swapped_model(apps, app_name, model_name):
    model_path = swapper.get_model_name(app_name, model_name)
    app, model = swapper.split(model_path)
    return apps.get_model(app, model) 
Example #10
Source File: __init__.py    From openwisp-users with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def create_default_groups(apps, schema_editor):
    org_model = swapper.get_model_name('openwisp_users', 'organization')
    model_app_label = swapper.split(org_model)[0]
    group = apps.get_model(model_app_label, 'group')

    # To populate all the permissions
    for app_config in apps.get_app_configs():
        app_config.models_module = True
        create_permissions(app_config, apps=apps, verbosity=0)
        app_config.models_module = None

    operator = group.objects.filter(name='Operator')
    if operator.count() == 0:
        operator = group.objects.create(name='Operator')

    admin = group.objects.filter(name='Administrator')
    if admin.count() == 0:
        admin = group.objects.create(name='Administrator')
        permissions = [
            Permission.objects.get(
                content_type__app_label=model_app_label, codename='add_user'
            ).pk,
            Permission.objects.get(
                content_type__app_label=model_app_label, codename='change_user'
            ).pk,
            Permission.objects.get(
                content_type__app_label=model_app_label,
                codename='change_organizationuser',
            ).pk,
            Permission.objects.get(
                content_type__app_label=model_app_label,
                codename='delete_organizationuser',
            ).pk,
            Permission.objects.get(
                content_type__app_label=model_app_label,
                codename='add_organizationuser',
            ).pk,
        ]
        try:
            permissions += [
                Permission.objects.get(
                    content_type__app_label=model_app_label, codename='view_user'
                ).pk,
                Permission.objects.get(
                    content_type__app_label=model_app_label, codename='view_group'
                ).pk,
                Permission.objects.get(
                    content_type__app_label=model_app_label,
                    codename='view_organizationuser',
                ).pk,
            ]
        except Permission.DoesNotExist:
            pass
        admin.permissions.set(permissions)