def __init__(self, *args, **kwargs):
        super(TranslationMixin, self).__init__(*args, **kwargs)

        # if state is not None, no need to patch all the fields
        # this is necessary for data migrations
        # if getattr(self, '_state').db is not None:
        #     return

        TranslationMixin._translation_options = translator.\
            get_options_for_model(
                self.__class__)

        if self.__class__._translated:
            return

        # CONSTRUCT TEMPORARY EDIT HANDLER
        if issubclass(self.__class__, Page):
            edit_handler_class = get_page_edit_handler(self.__class__)
        else:
            edit_handler_class = get_snippet_edit_handler(self.__class__)
        TranslationMixin._wgform_class = edit_handler_class.get_form_class(
            self.__class__)

        defined_tabs = TranslationMixin._fetch_defined_tabs(self.__class__)

        for tab_name, tab in defined_tabs:
            patched_tab = []

            for panel in tab:
                trtab = TranslationMixin._patch_panel(self, panel)

                if trtab:
                    for x in trtab:
                        patched_tab.append(x)

            setattr(self.__class__, tab_name, patched_tab)

        # DELETE TEMPORARY EDIT HANDLER IN ORDER TO LET WAGTAIL RECONSTRUCT
        # NEW EDIT HANDLER BASED ON NEW TRANSLATION PANELS
        if issubclass(self.__class__, Page):
            if self.__class__ in PAGE_EDIT_HANDLERS:
                del PAGE_EDIT_HANDLERS[self.__class__]
            edit_handler_class = get_page_edit_handler(self.__class__)
        else:
            if self.__class__ in SNIPPET_EDIT_HANDLERS:
                del SNIPPET_EDIT_HANDLERS[self.__class__]
            edit_handler_class = get_snippet_edit_handler(self.__class__)

        form = edit_handler_class.get_form_class(self.__class__)
        for fname, f in form.base_fields.items():
            # set field required on formset level if original field is required
            # as well
            if fname in self._required_fields:
                f.required = True

            if fname in TranslationMixin._translation_options.fields and TranslationMixin._is_orig_required(
                    fname):
                f.required = False

        self.__class__._translated = True
    def __init__(self, *args, **kwargs):
        super(TranslationMixin, self).__init__(*args, **kwargs)

        # if state is not None, no need to patch all the fields
        # this is necessary for data migrations
        if getattr(self, '_state').db is not None:
            return

        TranslationMixin._translation_options = translator.\
            get_options_for_model(
                self.__class__)

        if self.__class__._translated:
            return

        # CONSTRUCT TEMPORARY EDIT HANDLER
        if issubclass(self.__class__, Page):
            edit_handler_class = get_page_edit_handler(self.__class__)
        else:
            edit_handler_class = get_snippet_edit_handler(self.__class__)
        TranslationMixin._wgform_class = edit_handler_class.get_form_class(
            self.__class__)

        defined_tabs = TranslationMixin._fetch_defined_tabs(self.__class__)

        for tab_name, tab in defined_tabs:
            patched_tab = []

            for panel in tab:
                trtab = TranslationMixin._patch_panel(self, panel)

                if trtab:
                    for x in trtab:
                        patched_tab.append(x)

            setattr(self.__class__, tab_name, patched_tab)

        # DELETE TEMPORARY EDIT HANDLER IN ORDER TO LET WAGTAIL RECONSTRUCT
        # NEW EDIT HANDLER BASED ON NEW TRANSLATION PANELS
        if issubclass(self.__class__, Page):
            if self.__class__ in PAGE_EDIT_HANDLERS:
                del PAGE_EDIT_HANDLERS[self.__class__]
            edit_handler_class = get_page_edit_handler(self.__class__)
        else:
            if self.__class__ in SNIPPET_EDIT_HANDLERS:
                del SNIPPET_EDIT_HANDLERS[self.__class__]
            edit_handler_class = get_snippet_edit_handler(self.__class__)

        form = edit_handler_class.get_form_class(self.__class__)
        for fname, f in form.base_fields.items():
            # set field required on formset level if original field is required
            # as well
            if fname in self._required_fields:
                f.required = True

            if fname in TranslationMixin._translation_options.fields and TranslationMixin._is_orig_required(fname):
                f.required = False

        self.__class__._translated = True
예제 #3
0
def add_panel_to_edit_handler(model, panel_cls, heading, index=None):
    """
    Adds specified panel class to model class.

    :param model: the model class.
    :param panel_cls: the panel class.
    :param heading: the panel heading.
    :param index: the index position to insert at.
    """
    from wagtail.wagtailadmin.views.pages import get_page_edit_handler

    edit_handler = get_page_edit_handler(model)
    panel_instance = ObjectList([
        panel_cls(),
    ], heading=heading).bind_to_model(model)

    if index:
        edit_handler.children.insert(index, panel_instance)
    else:
        edit_handler.children.append(panel_instance)
예제 #4
0
def add_panel_to_edit_handler(model, panel_cls, heading, index=None):
    """
    Adds specified panel class to model class.

    :param model: the model class.
    :param panel_cls: the panel class.
    :param heading: the panel heading.
    :param index: the index position to insert at.
    """
    from wagtail.wagtailadmin.views.pages import get_page_edit_handler

    edit_handler    = get_page_edit_handler(model)
    panel_instance  = ObjectList(
        [panel_cls(),],
        heading = heading
    ).bind_to_model(model)

    if index:
        edit_handler.children.insert(index, panel_instance)
    else:
        edit_handler.children.append(panel_instance)
예제 #5
0
    def __init__(self, model):

        # Check if this class was already patched
        if model in WagtailTranslator._patched_models:
            return

        WagtailTranslator._base_model = model
        WagtailTranslator._required_fields = {}

        # CONSTRUCT TEMPORARY EDIT HANDLER
        if issubclass(model, Page):
            if hasattr(model, 'get_edit_handler'):
                edit_handler_class = model.get_edit_handler()
            else:
                edit_handler_class = get_page_edit_handler(model)
        else:
            edit_handler_class = get_snippet_edit_handler(model)
        WagtailTranslator._base_model_form = edit_handler_class.get_form_class(
            model)

        defined_tabs = WagtailTranslator._fetch_defined_tabs(model)

        for tab_name, tab in defined_tabs:
            patched_tab = []

            for panel in tab:
                trtab = WagtailTranslator._patch_panel(model, panel)

                if trtab:
                    for x in trtab:
                        patched_tab.append(x)

            setattr(model, tab_name, patched_tab)

        # DELETE TEMPORARY EDIT HANDLER IN ORDER TO LET WAGTAIL RECONSTRUCT
        # NEW EDIT HANDLER BASED ON NEW TRANSLATION PANELS
        if issubclass(model, Page):
            if hasattr(model, 'get_edit_handler'):
                model.get_edit_handler.cache_clear()
                edit_handler_class = model.get_edit_handler()
            else:
                if model in PAGE_EDIT_HANDLERS:
                    del PAGE_EDIT_HANDLERS[model]
                edit_handler_class = get_page_edit_handler(model)
        else:
            if model in SNIPPET_EDIT_HANDLERS:
                del SNIPPET_EDIT_HANDLERS[model]
            edit_handler_class = get_snippet_edit_handler(model)

        # Set the required of the translated fields that were required on the original field
        form = edit_handler_class.get_form_class(model)
        for fname, f in form.base_fields.items():
            if fname in WagtailTranslator._required_fields[model]:
                f.required = True

        # Do the same to the formsets
        for related_name, formset in form.formsets.iteritems():
            if (formset.model in WagtailTranslator._required_fields
                    and WagtailTranslator._required_fields[formset.model]):
                for fname, f in formset.form.base_fields.items():
                    if fname in WagtailTranslator._required_fields[
                            formset.model]:
                        f.required = True

        # Overide page methods
        if issubclass(model, Page):
            model.move = _new_move
            model.set_url_path = _new_set_url_path
            model.route = _new_route
            model.get_site_root_paths = _new_get_site_root_paths
            model.relative_url = _new_relative_url
            model.url = _new_url
            _patch_clean(model)
            _patch_elasticsearch_fields(model)

        WagtailTranslator._patched_models.append(model)
    def __init__(self, model):

        # Check if this class was already patched
        if model in WagtailTranslator._patched_models:
            return

        WagtailTranslator._base_model = model
        WagtailTranslator._required_fields = {}

        # CONSTRUCT TEMPORARY EDIT HANDLER
        if issubclass(model, Page):
            if hasattr(model, 'get_edit_handler'):
                edit_handler_class = model.get_edit_handler()
            else:
                edit_handler_class = get_page_edit_handler(model)
        else:
            edit_handler_class = get_snippet_edit_handler(model)
        WagtailTranslator._base_model_form = edit_handler_class.get_form_class(model)

        defined_tabs = WagtailTranslator._fetch_defined_tabs(model)

        for tab_name, tab in defined_tabs:
            patched_tab = []

            for panel in tab:
                trtab = WagtailTranslator._patch_panel(model, panel)

                if trtab:
                    for x in trtab:
                        patched_tab.append(x)

            setattr(model, tab_name, patched_tab)

        # DELETE TEMPORARY EDIT HANDLER IN ORDER TO LET WAGTAIL RECONSTRUCT
        # NEW EDIT HANDLER BASED ON NEW TRANSLATION PANELS
        if issubclass(model, Page):
            if hasattr(model, 'get_edit_handler'):
                model.get_edit_handler.cache_clear()
                edit_handler_class = model.get_edit_handler()
            else:
                if model in PAGE_EDIT_HANDLERS:
                    del PAGE_EDIT_HANDLERS[model]
                edit_handler_class = get_page_edit_handler(model)
        else:
            if model in SNIPPET_EDIT_HANDLERS:
                del SNIPPET_EDIT_HANDLERS[model]
            edit_handler_class = get_snippet_edit_handler(model)

        # Set the required of the translated fields that were required on the original field
        form = edit_handler_class.get_form_class(model)
        for fname, f in form.base_fields.items():
            if fname in WagtailTranslator._required_fields[model]:
                f.required = True

        # Do the same to the formsets
        for related_name, formset in form.formsets.items():
            if (formset.model in WagtailTranslator._required_fields and
                    WagtailTranslator._required_fields[formset.model]):
                for fname, f in formset.form.base_fields.items():
                    if fname in WagtailTranslator._required_fields[formset.model]:
                        f.required = True

        # Overide page methods
        if issubclass(model, Page):
            model.move = _new_move
            model.set_url_path = _new_set_url_path
            model.route = _new_route
            model.get_site_root_paths = _new_get_site_root_paths
            model.relative_url = _new_relative_url
            model.url = _new_url
            _patch_clean(model)
            _patch_elasticsearch_fields(model)

        WagtailTranslator._patched_models.append(model)