Exemplo n.º 1
0
def user_class(self):
    if not hasattr(self, '_user_class'):
        self._user_class = AppConfig.get_models(
            *settings.CUSTOM_USER_MODEL.split('.', 2))
        if not self._user_class:
            raise ImproperlyConfigured('Could not get custom user model')
    return self._user_class
Exemplo n.º 2
0
    def __init__(self, apps: 'Apps', app: AppConfig):
        self.apps = apps
        self.app = app
        self.models: Mapping[str, Model] = dict()
        self.generate = False

        for djmodel in app.get_models():
            # do not process abstract models
            if djmodel._meta.abstract:
                continue

            model = Model(self, djmodel)
            self.models[model.model_name] = model
Exemplo n.º 3
0
    def handle_app_config(self, app_config: AppConfig, **options):
        """

        Args:
            app_config (AppConfig):
            **options:

        Returns:

        """
        is_resource_pkg_installed = "import_export" in sys.modules
        resource_file = os.path.join(app_config.path, 'resources.py')
        admin_file = os.path.join(app_config.path, 'admin.py')
        res_file_content = ""
        admin_file_content = ""
        has_resource_class = bool(options.get('resource') and is_resource_pkg_installed)

        for model in app_config.get_models():
            if not options.get('all'):
                if input("Do you want to create for {} ? [y/n]".format(model.__name__)) == 'n':
                    continue

            # check file imports
            if not res_file_content:
                res_file_content = read_if_exists(resource_file)
                # add imports if this is the first time creating this file
                res_file_content += render_template(RES_IMPORT_TEMPLATE, app_name=app_config.name)

            if not admin_file_content:
                admin_file_content = read_if_exists(admin_file)
                if ADMIN_IMPORTS not in admin_file_content:
                    admin_file_content = ADMIN_IMPORTS + admin_file_content

            # add class for a model if it doesn't exist already
            model_class = model.__module__ + '.' + model.__name__
            if model_class not in res_file_content:
                res_file_content += get_resource_cls(model, model_class)
            if model.__name__ not in admin_file_content:
                admin_file_content += get_admin_cls(has_resource_class, model)

        # finaly write file contents
        if has_resource_class:
            with open(resource_file, 'w') as writer:
                writer.write(res_file_content)
        with open(admin_file, 'w') as writer:
            writer.write(admin_file_content)