From 3bafdb37893e892a6c4ccc3824213f5e9540ee6f Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Tue, 21 Nov 2017 22:44:01 +0530 Subject: [PATCH] manual: Make manual available as a PDF download - serving gzipped version of PDF manual - works with the latest versions of both Firefox and Chromium - closes #1117 Signed-off-by: Joseph Nuthalapati Reviewed-by: Sunil Mohan Adapa --- plinth/modules/help/help.py | 69 ++++++++++++++++++++++++------------- plinth/modules/help/urls.py | 9 ++--- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/plinth/modules/help/help.py b/plinth/modules/help/help.py index 18caad4f1..83aea2154 100644 --- a/plinth/modules/help/help.py +++ b/plinth/modules/help/help.py @@ -14,31 +14,40 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # - """ Help module for Plinth. """ +import gzip +import mimetypes import os -from apt.cache import Cache -from django.http import Http404 -from django.template.response import TemplateResponse -from django.utils.translation import ugettext as _, ugettext_lazy -from plinth import cfg, __version__ +from apt.cache import Cache +from django.core.files.base import ContentFile +from django.http import Http404, HttpResponse +from django.template.response import TemplateResponse +from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy + +from plinth import __version__, cfg from plinth.menu import main_menu def init(): """Initialize the Help module""" - menu = main_menu.add_urlname(ugettext_lazy('Documentation'), - 'glyphicon-book', 'help:index') - menu.add_urlname(ugettext_lazy('Where to Get Help'), 'glyphicon-search', - 'help:index_explicit', order=5) - menu.add_urlname(ugettext_lazy('Manual'), 'glyphicon-info-sign', - 'help:manual', order=10) - menu.add_urlname(ugettext_lazy('About'), 'glyphicon-star', 'help:about', - order=100) + menu = main_menu.add_urlname( + ugettext_lazy('Documentation'), 'glyphicon-book', 'help:index') + menu.add_urlname( + ugettext_lazy('Where to Get Help'), 'glyphicon-search', + 'help:index-explicit', order=5) + menu.add_urlname( + ugettext_lazy('Manual'), 'glyphicon-info-sign', 'help:manual', + order=10) + menu.add_urlname( + ugettext_lazy('Download Manual'), 'glyphicon-download-alt', + 'help:download-manual', order=15) + menu.add_urlname( + ugettext_lazy('About'), 'glyphicon-star', 'help:about', order=100) def index(request): @@ -63,16 +72,31 @@ def about(request): def manual(request): """Serve the manual page from the 'doc' directory""" try: - with open(os.path.join(cfg.doc_dir, 'freedombox-manual.part.html'), - 'r', encoding='utf-8') as input_file: + with open( + os.path.join(cfg.doc_dir, 'freedombox-manual.part.html'), 'r', + encoding='utf-8') as input_file: content = input_file.read() except IOError: raise Http404 - return TemplateResponse( - request, 'help_manual.html', - {'title': _('{box_name} Manual').format(box_name=_(cfg.box_name)), - 'content': content}) + return TemplateResponse(request, 'help_manual.html', { + 'title': _('{box_name} Manual').format(box_name=_(cfg.box_name)), + 'content': content + }) + + +def download_manual(request): + """Serve the PDF version of the manual from the 'doc' directory""" + manual_name = 'freedombox-manual.pdf.gz' + try: + with gzip.open(os.path.join(cfg.doc_dir, manual_name), 'rb') as f: + content = f.read() + except IOError: + raise Http404('File {} does not exist.'.format(manual_name)) + + return HttpResponse( + ContentFile(content), + content_type=mimetypes.guess_type(manual_name)[0]) def status_log(request): @@ -82,10 +106,7 @@ def status_log(request): data = log_file.readlines() data = ''.join(data[-num_lines:]) - context = { - 'num_lines': num_lines, - 'data': data - } + context = {'num_lines': num_lines, 'data': data} return TemplateResponse(request, 'statuslog.html', context) diff --git a/plinth/modules/help/urls.py b/plinth/modules/help/urls.py index f27fa8168..ca26ce467 100644 --- a/plinth/modules/help/urls.py +++ b/plinth/modules/help/urls.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # - """ URLs for the Help module """ @@ -25,14 +24,16 @@ from plinth.utils import non_admin_view from . import help as views - urlpatterns = [ # having two urls for one page is a hack to help the current url/menu # system highlight the correct menu item. Every submenu-item with the same # url prefix as the main-menu is highlighted automatically. url(r'^help/$', non_admin_view(views.index), name='index'), - url(r'^help/index/$', non_admin_view(views.index), name='index_explicit'), + url(r'^help/index/$', non_admin_view(views.index), name='index-explicit'), url(r'^help/about/$', non_admin_view(views.about), name='about'), url(r'^help/manual/$', non_admin_view(views.manual), name='manual'), - url(r'^help/status-log/$', non_admin_view(views.status_log), name='status-log'), + url(r'^help/manual/download/$', + non_admin_view(views.download_manual), name='download-manual'), + url(r'^help/status-log/$', + non_admin_view(views.status_log), name='status-log'), ]