From 9fdae09271f5b6793edda90111648616b02c3670 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 30 Oct 2026 19:40:20 +0100 Subject: [PATCH 1/3] Minimal fixes --- Dockerfile | 6 +++--- src/redakcja/settings/__init__.py | 18 ++++++++++++++++-- src/redakcja/settings/defaults.py | 3 +++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 256a22c9..cda7f2fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -79,11 +79,11 @@ ARG GID=8200 # COPY nginx.conf conf.d/... RUN addgroup -g $GID app && addgroup nginx app -COPY --from=static-builder /app/var/static /app/var/static +COPY --from=static-builder /app/src/static /app/var/static -FROM base AS prod +FROM prod-base AS prod -COPY --from=static-builder /app/var/static/staticfiles.json /app/var/static/staticfiles.json +COPY --from=static-builder /app/src/static/staticfiles.json /app/src/static/staticfiles.json diff --git a/src/redakcja/settings/__init__.py b/src/redakcja/settings/__init__.py index b204d0af..f2383498 100644 --- a/src/redakcja/settings/__init__.py +++ b/src/redakcja/settings/__init__.py @@ -1,11 +1,15 @@ # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -import os.path +import os import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from .defaults import * -from ..localsettings import * + +try: + from ..localsettings import * +except ImportError: + pass DATA_UPLOAD_MAX_MEMORY_SIZE = 20000000 @@ -15,6 +19,16 @@ STATICFILES_DIRS = [ PROJECT_ROOT + '/static/' ] +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'HOST': 'db', + 'USER': os.environ.get('POSTGRES_USER'), + 'PASSWORD': os.environ.get('POSTGRES_PASSWORD'), + 'NAME': os.environ.get('POSTGRES_USER'), + } +} + TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', diff --git a/src/redakcja/settings/defaults.py b/src/redakcja/settings/defaults.py index cb49c8d3..297cd422 100644 --- a/src/redakcja/settings/defaults.py +++ b/src/redakcja/settings/defaults.py @@ -49,6 +49,8 @@ USE_L10N = True # Example: "/home/media/media.lawrence.com/" MEDIA_ROOT = PROJECT_ROOT + '/media/dynamic' STATIC_ROOT = PROJECT_ROOT + '/../static/' +CATALOGUE_REPO_PATH = MEDIA_ROOT + '/repo' +CATALOGUE_IMAGE_REPO_PATH = MEDIA_ROOT + '/imgrepo' # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash if there is a path component (optional in other cases). @@ -64,6 +66,7 @@ BROKER_URL = 'django://' SHOW_APP_VERSION = False +CAS_SERVER_URL = None CAS_APPLY_ATTRIBUTES_TO_USER = True CAS_RENAME_ATTRIBUTES = { 'email': 'email', 'firstname': 'first_name', 'lastname': 'last_name'} -- 2.20.1 From f01db0e7ea781ecc960673271a861523638393cc Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 16 Nov 2026 08:39:55 +0100 Subject: [PATCH 2/3] Absolute URLs on ilustr --- src/documents/ilustr.py | 76 +++++++++++++++++++ src/documents/models/book.py | 20 +++++ src/documents/templates/documents/base.html | 8 ++ .../templates/documents/book_detail.html | 13 ++++ src/documents/urls.py | 1 + src/documents/views.py | 18 ++++- .../static/js/wiki/view_properties.js | 2 +- src/wiki/static/wiki/scss/visual.scss | 13 ++++ 8 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/documents/ilustr.py diff --git a/src/documents/ilustr.py b/src/documents/ilustr.py new file mode 100644 index 00000000..30102adb --- /dev/null +++ b/src/documents/ilustr.py @@ -0,0 +1,76 @@ +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +"""Replacing relative addresses of illustrations with absolute URLs.""" +import os.path +import re +from urllib.parse import quote, unquote, urljoin +from django.conf import settings + + +SRC_RE = re.compile(r'''(]*?\bsrc\s*=\s*)(["'])(.*?)\2''', re.S) + + +def is_absolute(src): + return src.startswith(('http://', 'https://')) + + +def absolutize(text, gallery_url, gallery_dir): + """Replaces relative `src` of illustrations in text with absolute URLs. + + Only replaces addresses of files which exist in the gallery. + Returns the new text, a list of replaced and a list of unresolved addresses. + """ + fixed = [] + unresolved = [] + + def repl(m): + src = m.group(3) + if is_absolute(src): + return m.group(0) + path = os.path.normpath(os.path.join(gallery_dir, unquote(src))) + if not src or not os.path.isfile(path): + unresolved.append(src) + return m.group(0) + fixed.append(src) + url = urljoin(gallery_url, quote(src, safe='/%')) + return m.group(1) + m.group(2) + url + m.group(2) + + return SRC_RE.sub(repl, text), fixed, unresolved + + +class IlustrFixer: + def __init__(self, base_url, **author): + self.images_url = urljoin(base_url, settings.MEDIA_URL + settings.IMAGE_DIR) + self.images_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR) + self.author = author + self.stats = dict.fromkeys(['books', 'fixed', 'unresolved', 'conflicts'], 0) + + def fix_book(self, book): + if not book.gallery: + gallery_url = gallery_dir = None + else: + gallery_url = urljoin(self.images_url, quote(book.gallery) + '/') + gallery_dir = os.path.join(self.images_dir, book.gallery) + + for chunk in book.chunk_set.all(): + head = chunk.head + if head is None: + continue + head_text = head.materialize() + if gallery_url is None: + unresolved = [m.group(3) for m in SRC_RE.finditer(head_text) + if not is_absolute(m.group(3))] + if unresolved: + self.stats['unresolved'] += len(unresolved) + continue + + new_text, fixed, unresolved = absolutize(head_text, gallery_url, gallery_dir) + self.stats['fixed'] += len(fixed) + self.stats['unresolved'] += len(unresolved) + + if fixed: + chunk.commit( + new_text, description='Pełne adresy ilustracji.', + publishable=head.publishable, + **self.author) diff --git a/src/documents/models/book.py b/src/documents/models/book.py index aa4e776e..1245ceab 100644 --- a/src/documents/models/book.py +++ b/src/documents/models/book.py @@ -307,6 +307,26 @@ class Book(models.Model): valid_about = self.correct_about() assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about + srcs = self.relative_ilustr_srcs(changes=changes) + assert not srcs, 'Ilustracje bez pełnego adresu: ' + ', '.join( + src or '(pusty)' for src in srcs) + + def relative_ilustr_srcs(self, changes=None): + """Returns `src` of illustrations which aren't absolute URLs. + + Those only work relative to the gallery, so they break + outside of it. Checks the current text, unless `changes` are given. + """ + from lxml import etree + try: + tree = etree.fromstring(self.materialize(changes=changes).encode('utf-8')) + except (self.NoTextError, etree.XMLSyntaxError): + return [] + return [ + elem.get('src', '') for elem in tree.iter('ilustr') + if not elem.get('src', '').startswith(('http://', 'https://')) + ] + def publishable_error(self): try: return self.assert_publishable() diff --git a/src/documents/templates/documents/base.html b/src/documents/templates/documents/base.html index f8368888..c2c72434 100644 --- a/src/documents/templates/documents/base.html +++ b/src/documents/templates/documents/base.html @@ -52,6 +52,14 @@
+ {% if messages %} + {% for message in messages %} + + {% endfor %} + {% endif %} {% block content %}
diff --git a/src/documents/templates/documents/book_detail.html b/src/documents/templates/documents/book_detail.html index a9c4d2b0..8bf4329a 100644 --- a/src/documents/templates/documents/book_detail.html +++ b/src/documents/templates/documents/book_detail.html @@ -173,6 +173,19 @@ {% endif %}

+ {% with srcs=book.relative_ilustr_srcs %} + {% if srcs %} +
+ {% csrf_token %} + Ilustracje bez pełnego adresu: + {% for src in srcs %}{{ src|default:"(pusty)" }}{% if not forloop.last %}, {% endif %}{% endfor %} + {% if user.is_authenticated %} + + {% endif %} +
+ {% endif %} + {% endwith %} + {% if publishable %}

{% trans "Full XML" %}
diff --git a/src/documents/urls.py b/src/documents/urls.py index 60aa3fae..f23aad88 100644 --- a/src/documents/urls.py +++ b/src/documents/urls.py @@ -31,6 +31,7 @@ urlpatterns = [ path('create/', views.create_missing, name='documents_create_missing'), path('book//publish', views.publish, name="documents_publish"), + path('book//fix-ilustr', views.fix_ilustr, name="documents_fix_ilustr"), path('book//', views.book, name="documents_book"), path('book//gallery/', diff --git a/src/documents/views.py b/src/documents/views.py index e87dc6db..cab58bee 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -11,7 +11,7 @@ import os from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit from django.conf import settings -from django.contrib import auth +from django.contrib import auth, messages from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required, permission_required from django.urls import reverse @@ -676,6 +676,22 @@ def publish(request, slug): return http.HttpResponseRedirect(book.get_absolute_url()) +@require_POST +@login_required +def fix_ilustr(request, slug): + from .ilustr import IlustrFixer + book = get_object_or_404(Book, slug=slug) + protocol = 'https://' if request.is_secure() else 'http://' + fixer = IlustrFixer(protocol + request.get_host(), author=request.user) + with transaction.atomic(): + fixer.fix_book(book) + if fixer.stats['unresolved'] or fixer.stats['conflicts']: + messages.warning(request, 'Nie wszystkie ilustracje udało się naprawić.') + elif fixer.stats['fixed']: + messages.success(request, 'Naprawiono adresy ilustracji.') + return http.HttpResponseRedirect(book.get_absolute_url()) + + @require_POST @login_required def publish_image(request, slug): diff --git a/src/redakcja/static/js/wiki/view_properties.js b/src/redakcja/static/js/wiki/view_properties.js index dde914fc..6ae6e571 100644 --- a/src/redakcja/static/js/wiki/view_properties.js +++ b/src/redakcja/static/js/wiki/view_properties.js @@ -171,7 +171,7 @@ self.doc.refreshImageGallery({ success: function(galleryImages) { - $.each(self.doc.galleryImages, (i, imgItem) => { + $.each(galleryImages, (i, imgItem) => { let img = $("").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() { imglist.find('img').removeClass('active'); $(this).addClass('active'); diff --git a/src/wiki/static/wiki/scss/visual.scss b/src/wiki/static/wiki/scss/visual.scss index 027340a0..3130a156 100644 --- a/src/wiki/static/wiki/scss/visual.scss +++ b/src/wiki/static/wiki/scss/visual.scss @@ -4,5 +4,18 @@ font-size: 1rem; line-height: 1.5; } + + // Images require absolute src. + [x-node="ilustr"]:not([x-a-wl-src^="http://"]):not([x-a-wl-src^="https://"]) { + outline: 3px solid #dc3545; + + &::before { + content: "Ilustracja bez pełnego adresu"; + display: block; + color: #dc3545; + font-family: sans-serif; + font-size: .8rem; + } + } } -- 2.20.1 From ac5de8695878cb6aabf9161930af189cf5d15316 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 16 Nov 2026 08:59:28 +0100 Subject: [PATCH 3/3] Fixes --- requirements/requirements.txt | 2 +- src/catalogue/models.py | 1 + src/cover/forms.py | 15 ++++++++++----- src/cover/templates/cover/image_detail.html | 7 +++++-- src/documents/helpers.py | 6 +++--- src/documents/views.py | 19 ++++++++++++++----- src/redakcja/settings/__init__.py | 2 -- src/redakcja/settings/defaults.py | 1 + 8 files changed, 35 insertions(+), 18 deletions(-) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index f005eba9..0163c6af 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -10,7 +10,7 @@ python-slugify==8.0.1 python-docx==0.8.11 Wikidata==0.7 -librarian==24.5.9 +librarian==26.11 ## Django Django==4.1.9 diff --git a/src/catalogue/models.py b/src/catalogue/models.py index 8c3d4cdf..84daa469 100644 --- a/src/catalogue/models.py +++ b/src/catalogue/models.py @@ -668,6 +668,7 @@ class Thema(models.Model): @staticmethod def standardize(code): + if code is None: return None return code.split('.')[0] class Audience(models.Model): diff --git a/src/cover/forms.py b/src/cover/forms.py index aad80c4d..a1ba182d 100644 --- a/src/cover/forms.py +++ b/src/cover/forms.py @@ -79,14 +79,19 @@ class ImageEditForm(forms.ModelForm): model = Image exclude = ['download_url', 'use_file', 'example',] + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['file'].required = False + def clean(self): cleaned_data = super(ImageEditForm, self).clean() uploaded_file = cleaned_data.get('file', None) - width, height = PILImage.open(uploaded_file.file).size - min_width, min_height = settings.MIN_COVER_SIZE - if width < min_width or height < min_height: - raise forms.ValidationError(gettext('Image too small: %sx%s, minimal dimensions %sx%s') % - (width, height, min_width, min_height)) + if uploaded_file is not None: + width, height = PILImage.open(uploaded_file.file).size + min_width, min_height = settings.MIN_COVER_SIZE + if width < min_width or height < min_height: + raise forms.ValidationError(gettext('Image too small: %sx%s, minimal dimensions %sx%s') % + (width, height, min_width, min_height)) class ReadonlyImageEditForm(ImageEditForm): diff --git a/src/cover/templates/cover/image_detail.html b/src/cover/templates/cover/image_detail.html index 6a67eecc..64ab3d81 100644 --- a/src/cover/templates/cover/image_detail.html +++ b/src/cover/templates/cover/image_detail.html @@ -32,6 +32,7 @@

+ {% if object.use_file %} {% with perc=object.cut_percentages %} {% thumbnail object.file perc.th as th %} @@ -43,7 +44,8 @@
{% endlocalize %} - {% endwith %} + {% endwith %} + {% endif %}

{{ object.title }} by {{ object.author }}, {% if object.license_url %}{% endif %} @@ -58,11 +60,12 @@
- +{% if object.use_file %} + {% endif %}
diff --git a/src/documents/helpers.py b/src/documents/helpers.py index df15b91d..8617d234 100644 --- a/src/documents/helpers.py +++ b/src/documents/helpers.py @@ -3,7 +3,7 @@ # from datetime import date from functools import wraps -from os.path import join +from os.path import join, exists from os import listdir, stat from shutil import move, rmtree from django.conf import settings @@ -77,9 +77,9 @@ class GalleryMerger(object): return self.dest_size is not None def merge(self): - if not self.dest: + if not self.dest or not exists(self.path(self.dest)): return self.src - if not self.src: + if not self.src or not exists(self.path(self.src)): return self.dest files = listdir(self.path(self.dest)) diff --git a/src/documents/views.py b/src/documents/views.py index cab58bee..1fe7b054 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -264,7 +264,11 @@ def book_html(request, slug): return HttpResponseForbidden("Not authorized.") doc = book.wldocument(parse_dublincore=False) - html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()}) + html = doc.as_html( + gallery_url='%stmp/html/' % settings.MEDIA_URL, + gallery_path='%s/tmp/html/' % settings.MEDIA_ROOT, + base_url=request.build_absolute_uri(book.gallery_url()), + options={'gallery': "'%s'" % book.gallery_url()}) html = html.get_bytes().decode('utf-8') if html is not None else '' # response = http.HttpResponse(html, content_type='text/html') @@ -290,7 +294,7 @@ def book_pdf(request, slug, mobile=False): doc = book.wldocument() # TODO: error handling customizations = ['26pt', 'nothemes', 'nomargins', 'notoc'] if mobile else None - pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_path()), customizations=customizations) + pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_url()), customizations=customizations) from .ebook_utils import serve_file return serve_file(pdf_file.get_filename(), book.slug + '.pdf', 'application/pdf') @@ -414,9 +418,14 @@ def book(request, slug): cbook_by_slug = None if book.dc_slug: - audio_items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{book.dc_slug}.json').json()['items'] - has_audio = bool(audio_items) - can_sell_audio = has_audio and all(x['project']['can_sell'] for x in audio_items) + try: + audio_items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{book.dc_slug}.json').json()['items'] + except: + has_audio = None + can_sell_audio = None + else: + has_audio = bool(audio_items) + can_sell_audio = has_audio and all(x['project']['can_sell'] for x in audio_items) if book.catalogue_book is None or book.dc_slug != book.catalogue_book.slug: cbook_by_slug = catalogue.models.Book.objects.filter(slug=book.dc_slug).first() diff --git a/src/redakcja/settings/__init__.py b/src/redakcja/settings/__init__.py index f2383498..3eed3e45 100644 --- a/src/redakcja/settings/__init__.py +++ b/src/redakcja/settings/__init__.py @@ -135,8 +135,6 @@ LOCALE_PATHS = [ LOGIN_REDIRECT_URL = '/documents/user' -MIN_COVER_SIZE = (915, 1270) - LEGIMI_SMALL_WORDS = 2000 LEGIMI_BIG_WORDS = 10000 LEGIMI_SMALL_PRICE = 7 diff --git a/src/redakcja/settings/defaults.py b/src/redakcja/settings/defaults.py index 297cd422..db07037d 100644 --- a/src/redakcja/settings/defaults.py +++ b/src/redakcja/settings/defaults.py @@ -78,3 +78,4 @@ LEGIMI_USERNAME = None LEGIMI_PASSWORD = None LEGIMI_PUBLISHER_ID = None +MIN_COVER_SIZE = (800, 800) -- 2.20.1