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 <njoseph@thoughtworks.com>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Joseph Nuthalapati 2017-11-21 22:44:01 +05:30
parent 202d0bf5c7
commit 3bafdb3789
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
2 changed files with 50 additions and 28 deletions

View File

@ -14,31 +14,40 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
""" """
Help module for Plinth. Help module for Plinth.
""" """
import gzip
import mimetypes
import os 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 from plinth.menu import main_menu
def init(): def init():
"""Initialize the Help module""" """Initialize the Help module"""
menu = main_menu.add_urlname(ugettext_lazy('Documentation'), menu = main_menu.add_urlname(
'glyphicon-book', 'help:index') ugettext_lazy('Documentation'), 'glyphicon-book', 'help:index')
menu.add_urlname(ugettext_lazy('Where to Get Help'), 'glyphicon-search', menu.add_urlname(
'help:index_explicit', order=5) ugettext_lazy('Where to Get Help'), 'glyphicon-search',
menu.add_urlname(ugettext_lazy('Manual'), 'glyphicon-info-sign', 'help:index-explicit', order=5)
'help:manual', order=10) menu.add_urlname(
menu.add_urlname(ugettext_lazy('About'), 'glyphicon-star', 'help:about', ugettext_lazy('Manual'), 'glyphicon-info-sign', 'help:manual',
order=100) 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): def index(request):
@ -63,16 +72,31 @@ def about(request):
def manual(request): def manual(request):
"""Serve the manual page from the 'doc' directory""" """Serve the manual page from the 'doc' directory"""
try: try:
with open(os.path.join(cfg.doc_dir, 'freedombox-manual.part.html'), with open(
'r', encoding='utf-8') as input_file: os.path.join(cfg.doc_dir, 'freedombox-manual.part.html'), 'r',
encoding='utf-8') as input_file:
content = input_file.read() content = input_file.read()
except IOError: except IOError:
raise Http404 raise Http404
return TemplateResponse( return TemplateResponse(request, 'help_manual.html', {
request, 'help_manual.html', 'title': _('{box_name} Manual').format(box_name=_(cfg.box_name)),
{'title': _('{box_name} Manual').format(box_name=_(cfg.box_name)), 'content': content
'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): def status_log(request):
@ -82,10 +106,7 @@ def status_log(request):
data = log_file.readlines() data = log_file.readlines()
data = ''.join(data[-num_lines:]) data = ''.join(data[-num_lines:])
context = { context = {'num_lines': num_lines, 'data': data}
'num_lines': num_lines,
'data': data
}
return TemplateResponse(request, 'statuslog.html', context) return TemplateResponse(request, 'statuslog.html', context)

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
""" """
URLs for the Help module URLs for the Help module
""" """
@ -25,14 +24,16 @@ from plinth.utils import non_admin_view
from . import help as views from . import help as views
urlpatterns = [ urlpatterns = [
# having two urls for one page is a hack to help the current url/menu # 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 # system highlight the correct menu item. Every submenu-item with the same
# url prefix as the main-menu is highlighted automatically. # url prefix as the main-menu is highlighted automatically.
url(r'^help/$', non_admin_view(views.index), name='index'), 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/about/$', non_admin_view(views.about), name='about'),
url(r'^help/manual/$', non_admin_view(views.manual), name='manual'), 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'),
] ]