Skip to content

Commit

Permalink
update wagtail to v4.2.4 (#413)
Browse files Browse the repository at this point in the history
Co-authored-by: Paula K <paula.klinke@student.hpi.de>
  • Loading branch information
jeriox and Paula-Kli committed Nov 13, 2023
1 parent d478325 commit f92f893
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 164 deletions.
37 changes: 6 additions & 31 deletions myhpi/core/markdown/extensions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import re

import markdown
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import gettext_lazy as _
from markdown import Extension
from markdown import Extension, util
from markdown.inlinepatterns import LinkInlineProcessor
from markdown.preprocessors import Preprocessor
from wagtail.core.models import Page
from wagtail.images.models import Image
from wagtail.models import Page


class MinutesBasePreprocessor(Preprocessor):
Expand Down Expand Up @@ -147,40 +145,22 @@ def decrease(self, match):

class InternalLinkPattern(LinkInlineProcessor):
def handleMatch(self, m, data=None):
el = markdown.util.etree.Element("a")
el = util.etree.Element("a")
try:
el.set("href", self.url(m.group("id")))
el.text = markdown.util.AtomicString(m.group("title"))
el.text = util.AtomicString(m.group("title"))
except ObjectDoesNotExist:
el.text = markdown.util.AtomicString(_("[missing link]"))
el.text = util.AtomicString(_("[missing link]"))
return el, m.start(0), m.end(0)

def url(self, id):
return Page.objects.get(id=id).localized.get_url()

@staticmethod
def default_pattern():
return r"\[(?P<title>[^\[]+)\]\(page:(?P<id>\d+)\)"


class ImagePattern(LinkInlineProcessor):
def handleMatch(self, m, data=None):
el = markdown.util.etree.Element("img")
try:
el.set("src", self.url(m.group("id")))
el.set("alt", markdown.util.AtomicString(m.group("title")))
el.set("class", "rendered-image")
except ObjectDoesNotExist:
el = markdown.util.etree.Element("span")
el.text = markdown.util.AtomicString(_("[missing image]"))
return el, m.start(0), m.end(0)

def url(self, id):
return Image.objects.get(id=id).get_rendition("width-800").url

def default_pattern():
return r"!\[(?P<title>[^\[]+)\]\(image:(?P<id>\d+)\)"


class MinuteExtension(Extension):
def extendMarkdown(self, md):
md.registerExtension(self)
Expand All @@ -195,8 +175,3 @@ def extendMarkdown(self, md):
"InternalLinkPattern",
200,
)
md.inlinePatterns.register(
ImagePattern(ImagePattern.default_pattern(), md),
"ImagePattern",
200,
)
6 changes: 3 additions & 3 deletions myhpi/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from taggit.models import ItemBase, TagBase
from wagtail.admin.edit_handlers import FieldPanel, PublishingPanel
from wagtail.admin.forms import WagtailAdminPageForm
from wagtail.core.models import Page, Site
from wagtail.admin.panels import FieldPanel, PublishingPanel
from wagtail.documents.models import Document
from wagtail.models import Page, Site
from wagtail.search import index
from wagtail.snippets.models import register_snippet

Expand Down Expand Up @@ -195,7 +195,7 @@ class Minutes(BasePage):
FieldPanel("date"),
FieldPanel("moderator"),
FieldPanel("author"),
FieldPanel("participants", widget=UserSelectWidget),
FieldPanel("participants", widget=UserSelectWidget({"data-width": "100%"})),
FieldPanel("labels"),
FieldPanel("body"),
FieldPanel("guests"),
Expand Down
2 changes: 1 addition & 1 deletion myhpi/core/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from django.db.models import Q
from django.templatetags.static import static
from django.utils.html import format_html
from wagtail import hooks
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.core import hooks

from myhpi.core.models import (
AbbreviationExplanation,
Expand Down
4 changes: 2 additions & 2 deletions myhpi/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from django.db.models import F, Sum
from django.shortcuts import redirect
from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel
from wagtail.core.models import Orderable, Page
from wagtail.admin.panels import FieldPanel, InlinePanel
from wagtail.models import Orderable, Page
from wagtail.search import index

from myhpi.core.markdown.fields import CustomMarkdownField
Expand Down
2 changes: 1 addition & 1 deletion myhpi/search/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Q
from django.template.response import TemplateResponse
from wagtail.core.models import Page
from wagtail.models import Page
from wagtail.search.models import Query

from myhpi.core.models import BasePage
Expand Down
4 changes: 2 additions & 2 deletions myhpi/static/js/admin/easymde_custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ window.wagtailMarkdown.options = {
url: "/admin/images/chooser/",
onload: IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS,
responses: {
imageChosen: function (t) {
editor.codemirror.replaceSelection("![" + t.title + "](image:" + t.id + ")");
chosen: function (t) {
editor.codemirror.replaceSelection("![" + t.title + "](image:" + t.id + ",class=rendered-image,filter=width-800)");
}
},

Expand Down
30 changes: 0 additions & 30 deletions myhpi/tests/test_markdown_extensions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import re
from typing import Collection

import django
from django.test import TestCase
from django.utils.translation import activate
from wagtail.core.models import Page
from wagtail.images.models import Image

from myhpi.core.markdown.extensions import ImagePattern

django.setup()

Expand Down Expand Up @@ -103,28 +98,3 @@ def test_internal_link_preprocessor(self):
text = f"[Page title](page:{test_page.id})"
el, _, _ = ilp.handleMatch(re.match(ilp.pattern, text))
self.assertEqual(el.attrib["href"], test_page.localized.get_url())

def test_image_pattern(self):
activate("en")
from django.core.files.uploadedfile import SimpleUploadedFile

ip = ImagePattern(ImagePattern.default_pattern())

image_file = SimpleUploadedFile(
name="test_image.jpg",
content=open("myhpi/tests/files/test_image.jpg", "rb").read(),
content_type="image/jpeg",
)

image = Image.objects.create(
title="Test image",
file=image_file,
)

text = f"![Alt text](image:{image.id})"
invalid_text = "![Alt text](image:1234567890)"
el, _, _ = ip.handleMatch(re.match(ip.pattern, text))
self.assertEqual(el.attrib["src"], image.get_rendition("width-800").url)

el, _, _ = ip.handleMatch(re.match(ip.pattern, invalid_text))
self.assertEqual(el.text, "[missing image]")
2 changes: 1 addition & 1 deletion myhpi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from django.contrib.auth import views as auth_views
from django.urls import include, path, reverse_lazy
from django.views.generic import RedirectView
from wagtail import urls as wagtail_urls
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls

from myhpi.search import views as search_views
Expand Down
118 changes: 28 additions & 90 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.8"
wagtail = ">=2.16.2"
wagtail = "^4.2.4"
django-environ = "^0.10.0"
wagtail-localize = "^1.4"
wagtail-localize = "^1.6"
mozilla-django-oidc = "^3.0.0"
django-bootstrap-icons = "^0.8.3"
django-select2 = "^8.1.2"
Expand All @@ -18,7 +18,7 @@ django-debug-toolbar = "^4.2.0"
django-permissionedforms = "^0.1"
tenca = "^0.0.2"
html2text = "^2020.1.16"
wagtail-markdown = "^0.10.0"
wagtail-markdown = "^0.11.1"
autoflake = "^2.2.1"

[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit f92f893

Please sign in to comment.