Ejemplo n.º 1
0
def index(request):
    snippet_model_opts = [
        model._meta for model in get_snippet_models()
        if user_can_edit_snippet_type(request.user, model)]
    return render(request, 'wagtailsnippets/snippets/index.html', {
        'snippet_model_opts': sorted(
            snippet_model_opts, key=lambda x: x.verbose_name.lower())})
Ejemplo n.º 2
0
def get_snippet_type_choices():
    return [
        (
            m.__module__ + '.' + m.__name__,
            m._meta.verbose_name_plural.capitalize()
        ) for m in get_snippet_models()
    ]
Ejemplo n.º 3
0
def get_snippet_field_choices():
    return [
        (
            m._meta.verbose_name_plural.capitalize(),
            getattr(m, 'snippet_list_field_choices', [])
        ) for m in get_snippet_models()
    ]
Ejemplo n.º 4
0
def get_snippet_field_choices():
    return [
        (
            m._meta.verbose_name_plural.capitalize(),
            getattr(m, 'snippet_list_field_choices', [])
        ) for m in get_snippet_models()
    ]
Ejemplo n.º 5
0
def get_snippet_type_choices():
    return [
        (
            m.__module__ + '.' + m.__name__,
            m._meta.verbose_name_plural.capitalize()
        ) for m in get_snippet_models()
    ]
Ejemplo n.º 6
0
class SnippetList(blocks.StructBlock):
    heading = blocks.CharBlock(required=False)
    body = blocks.RichTextBlock(required=False)
    image = atoms.ImageBasic(required=False)

    snippet_type = blocks.ChoiceBlock(
        choices=[
            (
                m.__module__ + '.' + m.__name__,
                m._meta.verbose_name_plural.capitalize()
            ) for m in get_snippet_models()
        ],
        required=True
    )
    actions = blocks.ListBlock(blocks.StructBlock([
        ('link_label', blocks.CharBlock(
            help_text='E.g., "Download" or "Order free prints"'
        )),
        ('snippet_field', blocks.ChoiceBlock(
            choices=[
                (
                    m._meta.verbose_name_plural.capitalize(),
                    getattr(m, 'snippet_list_field_choices', [])
                ) for m in get_snippet_models()
            ],
            help_text='Corresponds to the available fields for the selected'
                      'snippet type.'
        )),
    ]))

    tags = blocks.ListBlock(
        blocks.CharBlock(label='Tag'),
        help_text='Enter tag names to filter the snippets. For a snippet to '
                  'match and be output in the list, it must have been tagged '
                  'with all of the tag names listed here. The tag names '
                  'are case-insensitive.'
    )

    class Meta:
        icon = 'table'
        template = '_includes/organisms/snippet-list.html'
Ejemplo n.º 7
0
def patch_wagtail_models():
    # After all models being registered the Page or BaseSetting subclasses and snippets are patched
    registered_models = translator.get_registered_models()

    # We need to sort the models to ensure that subclasses of a model are registered first,
    # or else if the panels are inherited all the changes on the subclass would be
    # reflected in the superclass
    registered_models.sort(key=compare_class_tree_depth)

    for model_class in registered_models:
        if issubclass(model_class, Page) or model_class in get_snippet_models() or issubclass(model_class, BaseSetting):
            WagtailTranslator(model_class)
    def _patch_other_models(self, model):
        if hasattr(model, 'edit_handler'):
            edit_handler = model.edit_handler
            for tab in edit_handler:
                tab.children = self._patch_panels(tab.children)
        elif hasattr(model, 'panels'):
            model.panels = self._patch_panels(model.panels)

        if model in get_snippet_models() and model in SNIPPET_EDIT_HANDLERS:
            del SNIPPET_EDIT_HANDLERS[model]
        else:
            get_setting_edit_handler.cache_clear()
def patch_wagtail_models():
    # After all models being registered the Page or BaseSetting subclasses and snippets are patched
    registered_models = translator.get_registered_models()

    # We need to sort the models to ensure that subclasses of a model are registered first,
    # or else if the panels are inherited all the changes on the subclass would be
    # reflected in the superclass
    registered_models.sort(key=compare_class_tree_depth)

    for model_class in registered_models:
        if issubclass(model_class, Page) or model_class in get_snippet_models() or issubclass(model_class, BaseSetting):
            WagtailTranslator(model_class)
Ejemplo n.º 10
0
def user_can_edit_snippets(user):
    """
    true if user has 'add', 'change' or 'delete' permission
    on any model registered as a snippet type
    """
    snippet_models = get_snippet_models()

    for model in snippet_models:
        if user_can_edit_snippet_type(user, model):
            return True

    return False
Ejemplo n.º 11
0
def user_can_edit_snippets(user):
    """
    true if user has 'add', 'change' or 'delete' permission
    on any model registered as a snippet type
    """
    snippet_models = get_snippet_models()

    for model in snippet_models:
        if user_can_edit_snippet_type(user, model):
            return True

    return False
    def _patch_other_models(self, model):
        if hasattr(model, 'edit_handler'):
            edit_handler = model.edit_handler
            for tab in edit_handler:
                tab.children = self._patch_panels(tab.children)
        elif hasattr(model, 'panels'):
            model.panels = self._patch_panels(model.panels)

        if model in get_snippet_models() and model in SNIPPET_EDIT_HANDLERS:
            del SNIPPET_EDIT_HANDLERS[model]
        else:
            get_setting_edit_handler.cache_clear()
Ejemplo n.º 13
0
def get_snippet_model_from_url_params(app_name, model_name):
    """
    Retrieve a model from an app_label / model_name combo.
    Raise Http404 if the model is not a valid snippet type.
    """
    try:
        model = apps.get_model(app_name, model_name)
    except LookupError:
        raise Http404
    if model not in get_snippet_models():
        # don't allow people to hack the URL to edit content types that aren't registered as snippets
        raise Http404

    return model
Ejemplo n.º 14
0
def index(request):
    """
    Fetches all human-readabe names of all snippet classes and presents them
    in a list.
    """
    snippet_types = [
        (
            content_type._meta.app_label,
            content_type._meta.model.__name__,
            content_type._meta.verbose_name,
            getattr(content_type._meta, 'description', ''),
        )
        for content_type in get_snippet_models()
        if user_can_edit_snippet_type(request.user, content_type)
    ]
    return render_modal_workflow(
        request,
        'wagtailembedder/snippets/types_list.html',
        'wagtailembedder/snippets/types_list.js',
        {
            'snippet_types': snippet_types,
        }
    )
Ejemplo n.º 15
0
def register_permissions():
    content_types = ContentType.objects.get_for_models(*get_snippet_models()).values()
    return Permission.objects.filter(content_type__in=content_types)
def autodiscover():
    """
    Auto-discover INSTALLED_APPS translation.py modules and fail silently when
    not present. This forces an import on them to register.
    Also import explicit modules.
    """
    import os
    import sys
    import copy
    from django.conf import settings
    from django.utils.module_loading import module_has_submodule
    from wagtail_modeltranslation.translator import translator
    from wagtail_modeltranslation.settings import TRANSLATION_FILES, DEBUG

    if django.VERSION < (1, 7):
        from django.utils.importlib import import_module
        mods = [(app, import_module(app)) for app in settings.INSTALLED_APPS]
    else:
        from importlib import import_module
        from django.apps import apps
        mods = [
            (
                app_config.name,
                app_config.module
            ) for app_config in apps.get_app_configs()
            ]

    for (app, mod) in mods:
        # Attempt to import the app's translation module.
        module = '%s.translation' % app
        before_import_registry = copy.copy(translator._registry)
        try:
            import_module(module)
        except:
            # Reset the model registry to the state before the last import as
            # this import will have to reoccur on the next request and this
            # could raise NotRegistered and AlreadyRegistered exceptions
            translator._registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an translation module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'translation'):
                raise

    for module in TRANSLATION_FILES:
        import_module(module)

    # After all models being registered the Page subclasses and snippets are patched
    for model in translator.get_registered_models():
        if issubclass(model, Page) or model in get_snippet_models():
            WagtailTranslator(model)

    # In debug mode, print a list of registered models and pid to stdout.
    # Note: Differing model order is fine, we don't rely on a particular
    # order, as far as base classes are registered before subclasses.
    if DEBUG:
        try:
            if sys.argv[1] in ('runserver', 'runserver_plus'):
                models = translator.get_registered_models()
                names = ', '.join(m.__name__ for m in models)
                print('wagtail_modeltranslation: Registered %d models for '
                      'translation (%s) [pid: %d].' %
                      (len(models), names, os.getpid()))
        except IndexError:
            pass
Ejemplo n.º 17
0
def autodiscover():
    """
    Auto-discover INSTALLED_APPS translation.py modules and fail silently when
    not present. This forces an import on them to register.
    Also import explicit modules.
    """
    import os
    import sys
    import copy
    from django.conf import settings
    from django.utils.module_loading import module_has_submodule
    from wagtail_modeltranslation.translator import translator
    from wagtail_modeltranslation.settings import TRANSLATION_FILES, DEBUG

    if django.VERSION < (1, 7):
        from django.utils.importlib import import_module
        mods = [(app, import_module(app)) for app in settings.INSTALLED_APPS]
    else:
        from importlib import import_module
        from django.apps import apps
        mods = [
            (
                app_config.name,
                app_config.module
            ) for app_config in apps.get_app_configs()
            ]

    for (app, mod) in mods:
        # Attempt to import the app's translation module.
        module = '%s.translation' % app
        before_import_registry = copy.copy(translator._registry)
        try:
            import_module(module)
        except:
            # Reset the model registry to the state before the last import as
            # this import will have to reoccur on the next request and this
            # could raise NotRegistered and AlreadyRegistered exceptions
            translator._registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an translation module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'translation'):
                raise

    for module in TRANSLATION_FILES:
        import_module(module)

    # After all models being registered the Page subclasses and snippets are patched
    for model in translator.get_registered_models():
        if issubclass(model, Page) or model in get_snippet_models():
            WagtailTranslator(model)

    # In debug mode, print a list of registered models and pid to stdout.
    # Note: Differing model order is fine, we don't rely on a particular
    # order, as far as base classes are registered before subclasses.
    if DEBUG:
        try:
            if sys.argv[1] in ('runserver', 'runserver_plus'):
                models = translator.get_registered_models()
                names = ', '.join(m.__name__ for m in models)
                print('wagtail_modeltranslation: Registered %d models for '
                      'translation (%s) [pid: %d].' %
                      (len(models), names, os.getpid()))
        except IndexError:
            pass
def register_permissions():
    content_types = ContentType.objects.get_for_models(
        *get_snippet_models()).values()
    return Permission.objects.filter(content_type__in=content_types)