help: Fix failing setup when manual directory is not available

- It is reported on the mailing list that first setup operation on the help app
fails. The logs indicate that /usr/share/freedombox/manual directory is not
present on the system. Although this situation does not occur on a normal
installation, catch, log and ignore this error to be safe.

- Also ensure that this static files component is added in post_init so that
basic setup is not at all affected by it.

Tests:

- In the development directory, move doc/manual to doc/manual.bak. Observe that
the error message is printed during initialization but the process continues.

- With the manual directory properly restored, the full help manual is displayed
properly with images.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-01-10 12:04:06 -08:00 committed by James Valleroy
parent 335a7f92cc
commit 03a805888d
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -3,6 +3,7 @@
FreedomBox app for help pages.
"""
import logging
import os
from django.utils.translation import gettext_lazy as _
@ -11,6 +12,8 @@ from django.utils.translation import pgettext_lazy
from plinth import app as app_module
from plinth import cfg, menu, web_server
logger = logging.getLogger(__name__)
app = None
@ -54,12 +57,19 @@ class HelpApp(app_module.App):
order=100)
self.add(menu_item)
def post_init(self):
"""Perform post initialization operations."""
directory_map = {}
langs = os.listdir(os.path.join(cfg.doc_dir, 'manual'))
for lang in langs:
manual_dir = os.path.join(cfg.doc_dir, 'manual', lang, 'images')
manual_url = f'/help/manual/{lang}/images'
directory_map[manual_url] = manual_dir
try:
langs = os.listdir(os.path.join(cfg.doc_dir, 'manual'))
for lang in langs:
manual_dir = os.path.join(cfg.doc_dir, 'manual', lang,
'images')
manual_url = f'/help/manual/{lang}/images'
directory_map[manual_url] = manual_dir
except FileNotFoundError:
logger.error('Unable to find manual directory. Ensure that '
'freedombox-doc-* packages are installed')
static_files = web_server.StaticFiles('static-files-help',
directory_map)