Example #1
0
    def handle(self, *args, **options):
        app_configs = [
            a for a in apps.get_app_configs() if a.name.startswith('apps.')
        ]
        for app_config in app_configs:
            dirname = os.path.join(os.path.dirname(app_config.module.__file__),
                                   'fixtures')
            filename = os.path.join(
                dirname, '{name}_data.json'.format(name=app_config.label))
            if not list(app_config.get_models()):
                continue
            models = [
                '{}.{}'.format(app_config.label, m._meta.object_name.lower())
                for m in app_config.get_models() if m._meta.proxy is False
            ]
            self.write_fixtures(filename, *models)

        self.write_fixtures(
            os.path.join(settings.BASE_DIR,
                         'apps/user/fixtures/user_data.json'),
            *['auth.user', 'auth.group', 'account'])

        self.write_fixtures(
            os.path.join(settings.BASE_DIR,
                         'apps/core/fixtures/initial_data.json'), *['sites'])
Example #2
0
def get_apps():
    """Get list of all authentik apps"""
    from django.apps.registry import apps

    for _app in apps.get_app_configs():
        if _app.name.startswith("authentik"):
            yield _app
Example #3
0
def synchronize_client(client):
    """
    Synchronize all models as resources for a client.

    :type client: django_keycloak.models.Client
    """
    for app_config in apps.get_app_configs():
        synchronize_resources(client=client, app_config=app_config)
Example #4
0
    def count_registers(self, full=True):

        print('--------- CountRegisters ----------')

        for app in apps.get_app_configs():
            for m in app.get_models():
                count = m.objects.all().count()
                if full or count > 100000:
                    print(count, m, app)
Example #5
0
def get_all_app_models():
    r = {}
    for app in apps.get_app_configs():
        model_names = app.models.keys()
        if not model_names:
            continue
        m = r[app.label] = {}
        for model_name in model_names:
            m[model_name] = dict(full={}, part={})
    return r
Example #6
0
def search(request):
    """
    Search rules (Scripts and Signatures) from all the probes.
    """
    if request.GET.get("pattern"):
        pattern = request.GET.get("pattern")
        if pattern != "" and pattern != " ":
            data = list()
            for app in apps.get_app_configs():
                first = True
                for model in app.get_models():
                    if issubclass(model, Rule):
                        if app.verbose_name != "Rules":
                            if first:
                                first = False
                                probe_data = dict()
                                try:
                                    my_class = getattr(
                                        importlib.import_module(app.label +
                                                                ".models"),
                                        'Signature' + app.verbose_name)
                                    signatures = my_class.find(pattern)
                                    probe_data.update(
                                        {'signatures': signatures})

                                    my_class = getattr(
                                        importlib.import_module(app.label +
                                                                ".models"),
                                        'Script' + app.verbose_name)
                                    scripts = my_class.find(pattern)
                                    probe_data.update({'scripts': scripts})

                                    my_class = getattr(
                                        importlib.import_module(app.label +
                                                                ".models"),
                                        'Rule' + app.verbose_name)
                                    rules = my_class.find(pattern)
                                    probe_data.update({'rules': rules})
                                except AttributeError:
                                    pass
                                probe_data.update({'name': app.label})
                                data.append(probe_data)
            return render(request, 'rules/search.html', {
                'data': data,
                'pattern': pattern
            })
        else:
            return render(request, 'rules/search.html',
                          {'message': 'Pattern ' + pattern + ' not found'})
    else:
        return render(request, 'rules/search.html',
                      {'message': 'Pattern not found'})
Example #7
0
    def ready(self):
        # Find all schema.graphql and resolver modules and load them
        self.type_defs = list()
        self.types = list()
        self.resolvers = list()

        logger.debug("Trying to load resolvers and schemas:")
        for config in apps.get_app_configs():
            self.load_schema(config)
            self.load_resolvers(config)
            self.load_custom_types(config)

        logger.debug("Types: %s",
                     ", ".join([_type.name for _type in self.types]))
Example #8
0
def index(request):
    """
    Display all probes instances.
    """
    instances = dict()
    for app in apps.get_app_configs():
        for model in app.get_models():
            if issubclass(model, Probe):
                if app.verbose_name != "Home":
                    my_class = getattr(
                        importlib.import_module(app.label + ".models"),
                        app.verbose_name)
                    instances[app.label] = my_class.get_all()
    return render(request, 'home/index.html', {'instances': instances})
Example #9
0
def get_models(app_mod=None, include_auto_created=False):
    """Return the models belonging to an app.

    Args:
        app_mod (module, optional):
            The application module.

        include_auto_created (bool, optional):
            Whether to return auto-created models (such as many-to-many
            models) in the results.

    Returns:
        list:
        The list of modules belonging to the app.
    """
    if apps:
        # Django >= 1.7
        if app_mod is None:
            return apps.get_models(include_auto_created=include_auto_created)

        for app_config in apps.get_app_configs():
            if app_config.models_module is app_mod:
                return [
                    model for model in app_config.get_models(
                        include_auto_created=include_auto_created)
                    if not model._meta.abstract
                ]

        return []
    else:
        # Django < 1.7
        models = _get_models(app_mod,
                             include_auto_created=include_auto_created)

        if app_mod is not None:
            # Avoids a circular import.
            from django_evolution.utils.apps import get_app_name

            app_mod_name = get_app_name(app_mod)

            models = [
                model for model in models
                if model.__module__.startswith(app_mod_name)
            ]

        return models
Example #10
0
def get_apps():
    """Return the list of all installed apps with models.

    This returns the apps from the app registry on Django >= 1.7, and from
    the old-style cache on Django < 1.7.

    Returns:
        list: A list of all the modules containing model classes.
    """
    if apps:
        # Django >= 1.7
        return [
            app.models_module for app in apps.get_app_configs()
            if app.models_module is not None
        ]
    else:
        # Django < 1.7
        return cache.get_apps()
def main(options):
    verbosity = int(options.get("verbosity"))
    exclude_list = options.get("exclude")
    if exclude_list is None:
        exclude_list = []
    results = {}
    for app in apps.get_app_configs():
        if app.name in exclude_list:
            if verbosity > 2:
                print("excluding:", app.name)
            continue
        if verbosity > 2:
            print("trying:", app.name)
        try:
            m = __import__("%s.utils.EMAIL" % app.name)
        except ImportError:
            pass  # no email generator for this app
        else:
            if verbosity > 1:
                print("collecting from:", app.name)
            try:
                d = m.utils.EMAIL.main()
            except:
                print("-" * 25)
                print("APPLICATION:", app.name)
                traceback.print_exc()
                print("=" * 50)
            else:
                for key, data in d.items():
                    if verbosity > 2:
                        print("({}, {}) = {}".format(app.name, key, data))
                    results[(app.name, key)] = data

    # Fix 'safe strings' Django-ism.
    uni_res = {}
    deactivate = []
    for key, value in results.items():
        if value is None:
            if verbosity > 2:
                print("flagging", key, "for deativation")
            deactivate.append(key)
        else:
            uni_res[key] = value
    return uni_res, deactivate
Example #12
0
def get_apps():
    """Return the list of all installed apps with models.

    This returns the apps from the app registry on Django >= 1.7, and from
    the old-style cache on Django < 1.7.

    Returns:
        list: A list of all the modules containing model classes.
    """
    if apps:
        # Django >= 1.7
        return [
            app.models_module
            for app in apps.get_app_configs()
            if app.models_module is not None
        ]
    else:
        # Django < 1.7
        return cache.get_apps()
Example #13
0
def run(*args):
    if args[0] and args[1]:
        source = args[0] + '/probemanager'
        dest = args[1].rstrip('/') + '/probemanager'
    else:
        source = settings.BASE_DIR
        dest = settings.BASE_DIR
    # en prod git_tag prendre des sources ou copier git_root = settings.BASE_DIR
    with open(dest + '/version.txt', 'w') as f:
        f.write(git_tag(source))
    for app in apps.get_app_configs():
        for model in app.get_models():
            if issubclass(model, Probe):
                if app.verbose_name != "Core":
                    dest_app = dest + "/" + app.label + '/'
                    source_app = source + "/" + app.label + '/'
                    with open(dest_app + 'version.txt', 'w') as f:
                        f.write(git_tag(source_app))
    exit(0)
Example #14
0
def add_groups(apps, schema_editor):
    # create all ContentTypes premission before this migration
    for app_config in apps_alt.get_app_configs():
        create_permissions(app_config)

    Group = apps.get_model('auth', 'Group')
    Permission = apps.get_model('auth', 'Permission')
    """
    dividing two groups of user here
    - Set it's name
    - Clearify it's premission
    """
    # bidder group
    bidder = Group.objects.create(name="Bidder")

    permission_list = \
    list(
        chain(
            Permission.objects.filter(codename__contains = "event"),
            Permission.objects.filter(codename__contains = "post"),
            Permission.objects.filter(codename__contains = "message")
        )
    )

    # CRUD of post Event Msg
    bidder.permissions.set(permission_list)
    # save the setting
    bidder.save()

    # poster group
    poster = Group.objects.create(name="Poster")
    permission_list = list(
        chain(Permission.objects.filter(codename__contains="bid"),
              Permission.objects.filter(codename__contains="message")))

    # CRUD of post Event Msg
    poster.permissions.set(permission_list)
    # save the setting
    poster.save()
Example #15
0
def run(*args):
    template = """

{{ name }}
{% for char in range(name|length) -%}
=
{%- endfor %}

.. toctree::

   {{ module }}

"""
    template_include = """.. include:: ../../probemanager/{{ module }}/README.rst

"""
    t = Template(template)
    t_include = Template(template_include)
    with open(settings.ROOT_DIR + '/docs/modules/index.rst', 'w') as f:
        for app in apps.get_app_configs():
            for model in app.get_models():
                if issubclass(model, Probe):
                    if app.verbose_name != "Core":
                        path = settings.BASE_DIR + "/" + app.label + "/README.rst"
                        if os.path.isfile(path):
                            template_rendered = t.render(module=app.label,
                                                         name=app.verbose_name)
                            template_include_rendered = t_include.render(
                                module=app.label)
                            f_include = open(
                                settings.ROOT_DIR + '/docs/modules/' +
                                app.label + '.rst', 'w')
                            f_include.write(template_include_rendered)
                            f_include.close()
                            f.write(template_rendered)
                            break
    exit(0)
Example #16
0
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework_swagger.views import get_swagger_view
from django.apps.registry import apps
from home.models import Probe

schema_view = get_swagger_view(title='ProbeManager API', url='/')

urlpatterns_modules = list()
for app in apps.get_app_configs():
    for model in app.get_models():
        if issubclass(model, Probe):
            if app.verbose_name != "Home":
                urlpatterns_modules.append(
                    url(r'^' + app.label + '/', include(app.label + '.urls')))

urlpatterns = [
    url(r'^api/v1/doc/$', schema_view),
    url(r'^api/v1/', include('api.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^select2/', include('select2.urls')),
    url(r'^rules/', include('rules.urls')),
    url(r'^', include('home.urls')),
]
urlpatterns += urlpatterns_modules
Example #17
0
    def handle(self, *args, **options):
        """"
        Obtiene los grupos de usuarios y el codename de sus permisos, elimina todos los permisos registrados
        y los regenera, agrega los permisos correspondientes a cada grupo verficando si el permiso existe.

        """

        per_laboratory_administrator = None
        per_professor = None
        per_student = None

        laboratory_administrator = Group.objects.filter(
            name="Laboratory Administrator").first()
        professor = Group.objects.filter(name="Professor").first()
        student = Group.objects.filter(name="Student").first()

        if laboratory_administrator:
            per_laboratory_administrator = list(
                Group.objects.filter(
                    name="Laboratory Administrator").values_list(
                        'permissions__codename', flat=True))
            per_laboratory_administrator += [
                'add_organizationusermanagement',
                'change_organizationusermanagement',
                'delete_organizationusermanagement',
                'view_organizationusermanagement'
            ]
        if professor:
            per_professor = list(
                Group.objects.filter(name="Professor").values_list(
                    'permissions__codename', flat=True))

        if student:
            per_student = list(
                Group.objects.filter(name="Student").values_list(
                    'permissions__codename', flat=True))

        Permission.objects.all().delete()
        for app_config in apps.get_app_configs():
            create_permissions(app_config)

        if laboratory_administrator and per_laboratory_administrator:

            for per in per_laboratory_administrator:
                permission = Permission.objects.filter(codename=per).first()
                if permission:
                    laboratory_administrator.permissions.add(permission)

        if professor and per_professor:

            for per in per_professor:
                permission = Permission.objects.filter(codename=per).first()
                if permission:
                    professor.permissions.add(permission)

        if student and per_student:

            for per in per_student:
                permission = Permission.objects.filter(codename=per).first()
                if permission:
                    student.permissions.add(permission)
Example #18
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''
    old_sys_path = sys.path[:]
    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    if project_path not in sys.path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    project_settings = '{}.settings'.format(project)
    os.environ['DJANGO_SETTINGS_MODULE'] = project_settings

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        log.error('Found Django settings, but Django is not installed.')
        return

    log.warn('Loading Django Settings (Using {}): {}'.format(
        django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = set()
    settings_imports = set()

    try:
        settings = LazySettings()
        settings._setup()
        for k, v in vars(settings._wrapped).items():
            if k not in _excluded_settings and re.match(r'^[A-Z_]+$', k):
                # log.debug('Scanning Django setting: %s', k)
                scan_django_settings(v, settings_imports)

        # Manually scan INSTALLED_APPS since the broad scan won't include
        # strings without a period in it .
        for app in getattr(settings, 'INSTALLED_APPS', []):
            if hasattr(app, '__file__') and getattr(app, '__file__'):
                imp, _ = utils.import_path_from_file(getattr(app, '__file__'))
                installed_apps.add(imp)
            else:
                installed_apps.add(app)
    except Exception as e:
        log.error('Could not load Django settings: %s', e)
        log.debug('', exc_info=True)
        return

    if not installed_apps or not settings_imports:
        log.error('Got empty settings values from Django settings.')

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    installed_apps.add(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                log.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        log.debug('Could not use AppConfig: {}'.format(e))

    # Restore before sub scans can occur
    sys.path[:] = old_sys_path
    os.chdir(cwd)

    for item in settings_imports:
        need_scan = item.startswith(_filescan_modules)
        yield ('django', item, project_path if need_scan else None)

    for app in installed_apps:
        need_scan = app.startswith(project)
        yield ('django', app, project_path if need_scan else None)
Example #19
0
 def get_installed_apps(self):
     for app_config in apps.get_app_configs():
         yield app_config.name, app_config
Example #20
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''
    old_sys_path = sys.path[:]
    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    if project_path not in sys.path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    project_settings = '{}.settings'.format(project)
    os.environ['DJANGO_SETTINGS_MODULE'] = project_settings

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        log.error('Found Django settings, but Django is not installed.')
        return

    log.warn('Loading Django Settings (Using {}): {}'
                    .format(django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = set()
    settings_imports = set()

    try:
        settings = LazySettings()
        settings._setup()
        for k, v in vars(settings._wrapped).items():
            if k not in _excluded_settings and re.match(r'^[A-Z_]+$', k):
                # log.debug('Scanning Django setting: %s', k)
                scan_django_settings(v, settings_imports)

        # Manually scan INSTALLED_APPS since the broad scan won't include
        # strings without a period in it .
        for app in getattr(settings, 'INSTALLED_APPS', []):
            if hasattr(app, '__file__') and getattr(app, '__file__'):
                imp, _ = utils.import_path_from_file(getattr(app, '__file__'))
                installed_apps.add(imp)
            else:
                installed_apps.add(app)
    except Exception as e:
        log.error('Could not load Django settings: %s', e)
        log.debug('', exc_info=True)
        return

    if not installed_apps or not settings_imports:
        log.error('Got empty settings values from Django settings.')

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    installed_apps.add(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                log.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        log.debug('Could not use AppConfig: {}'.format(e))

    # Restore before sub scans can occur
    sys.path[:] = old_sys_path
    os.chdir(cwd)

    for item in settings_imports:
        need_scan = item.startswith(_filescan_modules)
        yield ('django', item, project_path if need_scan else None)

    for app in installed_apps:
        need_scan = app.startswith(project)
        yield ('django', app, project_path if need_scan else None)
Example #21
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''

    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    remove_path = project_path not in sys.path
    if remove_path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(project)

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        printer.error('Found Django settings, but Django is not installed.')
        return

    printer.error('Loading Django Settings (Using {}): {}'
                    .format(django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = None
    db_settings = None
    cache_settings = None

    try:
        settings = LazySettings()
        installed_apps = getattr(settings, 'INSTALLED_APPS', None)
        db_settings = getattr(settings, 'DATABASES', None)
        cache_settings = getattr(settings, 'CACHES', None)
    except Exception as e:
        printer.error(u'Could not load Django settings: {}'.format(e))
        return

    if not installed_apps or not db_settings:
        printer.error(u'Could not load INSTALLED_APPS or DATABASES.')

    # Find typical Django modules that rely on packages that the user chooses
    # to install.
    django_base = os.path.normpath(os.path.join(os.path.dirname(django.__file__), '..'))
    django_modules = set()
    if db_settings:
        for backend, db_conf in db_settings.items():
            engine = db_conf.get('ENGINE')
            if not engine:
                continue
            if hasattr(engine, '__file__'):
                django_modules.add(engine.__file__)
            else:
                p = os.path.join(django_base, *engine.split('.'))
                django_modules.add(os.path.join(p, 'base.py'))

    if cache_settings:
        for backend, cache_conf in cache_settings.items():
            engine = cache_conf.get('BACKEND')
            if hasattr(engine, '__file__'):
                django_modules.add(engine.__file__)
            else:
                p = os.path.join(django_base, *engine.split('.')[:-1])
                django_modules.add(p + '.py')

    if django_modules:
        for mod in django_modules:
            if os.path.exists(mod):
                for item in _scan_file(mod, 'django'):
                    yield item

    django_installed_apps = []

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    django_installed_apps.append(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                printer.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        django_installed_apps = list(installed_apps)
        printer.debug('Could not use AppConfig: {}'.format(e))

    for app in django_installed_apps:
        import_parts = app.split('.')
        # Start with the longest import path and work down
        for i in range(len(import_parts), 0, -1):
            yield ('django', '.'.join(import_parts[:i]))

    os.chdir(cwd)
    if remove_path:
        sys.path.remove(project_path)
Example #22
0
    def ajuste_metadata_com_set_values(self):
        for app in apps.get_app_configs():

            if not app.name.startswith('cmj') and not app.name.startswith(
                    'sapl'):
                continue

            print(app)

            for m in app.get_models():
                model_exec = False

                for f in m._meta.get_fields():
                    dua = f
                    print(dua)
                    if hasattr(dua, 'auto_now') and dua.auto_now:
                        #print(m, 'auto_now deve ser desativado.')
                        # continue  # auto_now deve ser desativado
                        print(m, 'desativando auto_now')
                        dua.auto_now = False

                    if not isinstance(f, FileField):
                        continue

                    # se possui FileField, o model então
                    # deve possuir FIELDFILE_NAME
                    assert hasattr(m, 'FIELDFILE_NAME'
                                   ), '{} não possui FIELDFILE_NAME'.format(
                                       m._meta.label)

                    # se possui FileField, o model então
                    # deve possuir metadata
                    assert hasattr(
                        m, 'metadata'), '{} não possui metadata'.format(
                            m._meta.label)

                    # o campo field deve estar em FIELDFILE_NAME
                    assert f.name in m.FIELDFILE_NAME, '{} não está no FIELDFILE_NAME de {}'.format(
                        f.name, m._meta.label)

                    model_exec = True

                if not model_exec:
                    continue

                print(m, m.objects.all().count())

                # if m != DocumentoAdministrativo:
                #    continue

                for i in m.objects.all().order_by('-id'):  # [:500]:

                    if not hasattr(i, 'metadata'):
                        continue

                    metadata = i.metadata if i.metadata else {}
                    for fn in i.FIELDFILE_NAME:

                        ff = getattr(i, fn)
                        if not ff:
                            continue

                        if not os.path.exists(ff.path):
                            print('Arquivo registrado mas não existe', i.id, i)
                            continue

                        if not metadata:
                            continue

                        if 'signs' not in metadata:
                            continue

                        if fn not in metadata['signs']:
                            continue

                        if not metadata['signs'][fn]:
                            print(i)
                            i.save()
Example #23
0
 def get_apps():
     for app_config in apps.get_app_configs():
         yield app_config.name, app_config
Example #24
0
def get_apps_labels():
    """
    Get apps list and return tuple of apps labels.
    """
    return tuple(app.label for app in apps.get_app_configs())
Example #25
0
 def get_apps():
     for app_config in apps.get_app_configs():
         yield app_config.name, app_config
Example #26
0
def get_apps():
    """Get list of all p2 apps"""
    from django.apps.registry import apps
    for _app in apps.get_app_configs():
        if _app.name.startswith('p2'):
            yield _app