Narweb: possibility to have more than 2 downloads (#1024)

* Narweb: possibility to have more than 2 downloads

By default, I set this to 3 downloads.
If you want to have more than 3 downloads, you have only the variable
self.__max_download to change in the narrativeweb.py.

* better pylint score.
* Narweb: add md5 checksum for each file
* Display a message if the file doesn't exist

Fixes #11626
This commit is contained in:
Serge Noiraud 2020-04-05 10:18:39 +02:00 committed by GitHub
parent 2ce32d8c6b
commit 3601877a63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 97 deletions

View File

@ -57,6 +57,7 @@ from gramps.plugins.lib.libhtml import Html
#------------------------------------------------
from gramps.plugins.webreport.basepage import BasePage
from gramps.plugins.webreport.common import (FULLCLEAR, html_escape)
from gramps.gen.utils.file import create_checksum
_ = glocale.translation.sgettext
LOG = logging.getLogger(".NarrativeWeb")
@ -78,17 +79,13 @@ class DownloadPage(BasePage):
return
# menu options for class
# download and description #1
# download and description #n ( 1 <= n < 5 )
dlfname1 = self.report.dl_fname1
dldescr1 = self.report.dl_descr1
# download and description #2
dlfname2 = self.report.dl_fname2
dldescr2 = self.report.dl_descr2
dlfname = self.report.dl_fname
dldescr = self.report.dl_descr
# if no filenames at all, return???
if dlfname1 or dlfname2:
if dlfname:
output_file, sio = self.report.create_file("download")
result = self.write_header(self._('Download'))
@ -126,66 +123,61 @@ class DownloadPage(BasePage):
for (label, colclass) in [
(self._("File Name"), "Filename"),
(self._("Description"), "Description"),
(self._("Last Modified"), "Modified")])
(self._("Last Modified"), "Modified"),
(self._("MD5"), "Md5")])
# table body
tbody = Html("tbody")
table += tbody
dwnld = 0
# if dlfname1 is not None, show it???
if dlfname1:
for fnamex in dlfname:
# if fnamex is not None, do we have a file to download?
if fnamex:
fname = os.path.basename(dlfname[fnamex])
# if fname is not None, show it
if fname:
dwnld += 1
trow = Html("tr", id='Row01')
tbody += trow
dldescrx = dldescr[fnamex]
tcell = Html("td", class_="ColumnFilename") + (
Html("a", fname, href=fname,
title=html_escape(dldescrx))
)
trow += tcell
dldescr1 = dldescrx or "&nbsp;"
trow += Html("td", dldescr1, inline=True,
class_="ColumnDescription")
tcell = Html("td", class_="ColumnModified",
inline=True)
trow += tcell
if os.path.exists(dlfname[fnamex]):
md5 = create_checksum(dlfname[fnamex])
trow += Html("td", md5, class_="ColumnMd5",
inline=True)
modified = os.stat(dlfname[fnamex]).st_mtime
last_mod = datetime.datetime.fromtimestamp(
modified)
tcell += last_mod
# copy the file
self.report.copy_file(dlfname[fnamex],
fname)
else:
tcell += self._("Cannot open file")
if not dwnld:
# We have several files to download
# but all file names are empty
dldescrx = _("No file to download")
trow = Html("tr", id='Row01')
tbody += trow
fname = os.path.basename(dlfname1)
# TODO dlfname1 is filename, convert disk path to URL
tcell = Html("td", class_="ColumnFilename") + (
Html("a", fname, href=dlfname1,
title=html_escape(dldescr1))
)
tcell = Html("td", class_="ColumnFilename",
colspan=3) + Html("h2", dldescrx)
trow += tcell
dldescr1 = dldescr1 or "&nbsp;"
trow += Html("td", dldescr1,
class_="ColumnDescription", inline=True)
tcell = Html("td", class_="ColumnModified", inline=True)
trow += tcell
if os.path.exists(dlfname1):
modified = os.stat(dlfname1).st_mtime
last_mod = datetime.datetime.fromtimestamp(modified)
tcell += last_mod
else:
tcell += "&nbsp;"
# if download filename #2, show it???
if dlfname2:
# begin row #2
trow = Html("tr", id='Row02')
tbody += trow
fname = os.path.basename(dlfname2)
tcell = Html("td", class_="ColumnFilename") + (
Html("a", fname, href=dlfname2,
title=html_escape(dldescr2))
)
trow += tcell
dldescr2 = dldescr2 or "&nbsp;"
trow += Html("td", dldescr2,
class_="ColumnDescription", inline=True)
tcell = Html("td", id='Col04',
class_="ColumnModified", inline=True)
trow += tcell
if os.path.exists(dlfname2):
modified = os.stat(dlfname2).st_mtime
last_mod = datetime.datetime.fromtimestamp(modified)
tcell += last_mod
else:
tcell += "&nbsp;"
# clear line for proper styling
# create footer section
footer = self.write_footer(None)

View File

@ -193,10 +193,14 @@ class NavWebReport(Report):
# Download Options Tab
self.inc_download = self.options['incdownload']
self.dl_fname1 = self.options['down_fname1']
self.dl_descr1 = self.options['dl_descr1']
self.dl_fname2 = self.options['down_fname2']
self.dl_descr2 = self.options['dl_descr2']
self.nb_download = self.options['nbdownload']
self.dl_descr = {}
self.dl_fname = {}
for count in range(1, self.nb_download+1):
fnamex = 'down_fname%c' % str(count)
descrx = 'dl_descr%c' % str(count)
self.dl_fname[count] = self.options[fnamex]
self.dl_descr[count] = self.options[descrx]
self.encoding = self.options['encoding']
@ -1633,6 +1637,10 @@ class NavWebOptions(MenuReportOptions):
self.__maxinitialimagewidth = None
self.__citationreferents = None
self.__incdownload = None
self.__max_download = 4 # Add 1 to this counter: In reality 3 downloads
self.__nbdownload = None
self.__dl_descr = {}
self.__down_fname = {}
self.__placemappages = None
self.__familymappages = None
self.__stamenopts = None
@ -1640,15 +1648,11 @@ class NavWebOptions(MenuReportOptions):
self.__googlemapkey = None
self.__ancestortree = None
self.__css = None
self.__dl_descr1 = None
self.__dl_descr2 = None
self.__down_fname2 = None
self.__gallery = None
self.__updates = None
self.__maxdays = None
self.__maxupdates = None
self.__unused = None
self.__down_fname1 = None
self.__navigation = None
self.__target_cal_uri = None
self.__securesite = False
@ -1995,29 +1999,29 @@ class NavWebOptions(MenuReportOptions):
addopt("incdownload", self.__incdownload)
self.__incdownload.connect('value-changed', self.__download_changed)
self.__down_fname1 = DestinationOption(
_("Download Filename"),
os.path.join(config.get('paths.website-directory'), ""))
self.__down_fname1.set_help(
_("File to be used for downloading of database"))
addopt("down_fname1", self.__down_fname1)
self.__nbdownload = NumberOption(_("How many downloads"),
2, 1, self.__max_download-1)
self.__nbdownload.set_help(_("The number of download files to include "
"in the download page"))
addopt("nbdownload", self.__nbdownload)
self.__nbdownload.connect('value-changed', self.__download_changed)
self.__dl_descr1 = StringOption(_("Description for download"),
_('Smith Family Tree'))
self.__dl_descr1.set_help(_('Give a description for this file.'))
addopt("dl_descr1", self.__dl_descr1)
for count in range(1, self.__max_download):
fnamex = 'down_fname%c' % str(count)
descrx = 'dl_descr%c' % str(count)
wdir = os.path.join(config.get('paths.website-directory'), "")
__down_fname = DestinationOption(_("Download Filename #%c") %
str(count), wdir)
__down_fname.set_help(
_("File to be used for downloading of database"))
addopt(fnamex, __down_fname)
self.__down_fname[count] = __down_fname
self.__down_fname2 = DestinationOption(
_("Download Filename"),
os.path.join(config.get('paths.website-directory'), ""))
self.__down_fname2.set_help(
_("File to be used for downloading of database"))
addopt("down_fname2", self.__down_fname2)
self.__dl_descr2 = StringOption(_("Description for download"),
_('Johnson Family Tree'))
self.__dl_descr2.set_help(_('Give a description for this file.'))
addopt("dl_descr2", self.__dl_descr2)
__dl_descr = StringOption(_("Description for download"),
_('Family Tree #%c') % str(count))
__dl_descr.set_help(_('Give a description for this file.'))
addopt(descrx, __dl_descr)
self.__dl_descr[count] = __dl_descr
self.__download_changed()
@ -2377,15 +2381,23 @@ class NavWebOptions(MenuReportOptions):
Handles the changing nature of include download page
"""
if self.__incdownload.get_value():
self.__down_fname1.set_available(True)
self.__dl_descr1.set_available(True)
self.__down_fname2.set_available(True)
self.__dl_descr2.set_available(True)
self.__nbdownload.set_available(True)
for count in range(1, self.__max_download):
if count <= self.__nbdownload.get_value():
self.__down_fname[count].set_available(True)
self.__dl_descr[count].set_available(True)
else:
self.__down_fname[count].set_available(False)
self.__dl_descr[count].set_available(False)
else:
self.__down_fname1.set_available(False)
self.__dl_descr1.set_available(False)
self.__down_fname2.set_available(False)
self.__dl_descr2.set_available(False)
self.__nbdownload.set_available(False)
for count in range(1, self.__max_download):
if count <= self.__nbdownload.get_value():
self.__down_fname[count].set_available(False)
self.__dl_descr[count].set_available(False)
else:
self.__down_fname[count].set_available(False)
self.__dl_descr[count].set_available(False)
def __placemap_options(self):
"""