A node to show the list of documents in a table.
Requires a global JS "docManager" which is an instance of "DocumentsManager()". Requires a global JS "asideManager which is an instance of "AsideManager()".
A node to show the list of documents in a table.
Requires a global JS "docManager" which is an instance of "DocumentsManager()". Requires a global JS "asideManager which is an instance of "AsideManager()".
Create the node instance.
def __init__(self, parent: str, doc_manager: WhakerKitDocsManager=None, current_user: str=''):
"""Create the node instance.
:param parent: (str) The parent node identifier
:param doc_manager: (WhakerKitDocsManager) Contains the list of documents to display in the table
:param current_user: (str) The identified user -- can perform actions on files
"""
super(DocumentsNode, self).__init__(parent, 'documents_table', 'table', attributes={'id': 'documents_table', 'role': 'grid'})
if doc_manager is None:
self.__doc_manager = WhakerKitDocsManager()
self.__doc_manager.collect_docs()
elif isinstance(doc_manager, WhakerKitDocsManager) is True:
self.__doc_manager = doc_manager
else:
raise TypeError('Given doc_manager must be an instance of WhakerKitDocsManager. Got {:s} instead'.format(type(doc_manager)))
self.__current_user = WhakerKitDocsManager.format_author(current_user)
self.reset()
Reset the children to the default elements.
Create the table content: a row is a document, a column is a property of the document.
def reset(self):
"""Reset the children to the default elements.
Create the table content: a row is a document, a column is a property
of the document.
"""
self.clear_children()
thead = HTMLNode(self.identifier, 'thead', 'thead')
self.append_child(thead)
tr = HTMLNode(thead.identifier, None, 'tr')
thead.append_child(tr)
for field in DOC_FIELDS:
field_property = DOC_FIELDS[field]
if field_property.is_sortable is True:
th = DocumentsNode.__add_sortable_th(tr, field, field_property.label)
else:
th = DocumentsNode.__add_th(tr, field, field_property.label)
if field_property.is_toggable is True:
th.add_attribute('data-sort', field)
if field_property.is_hidden is True:
th.add_attribute('class', 'hidden')
DocumentsNode.__add_th(tr, 'actions', get_msg(MSG_ACTIONS))
tbody = HTMLNode(self.identifier, 'tbody', 'tbody')
self.append_child(tbody)
if len(self.__doc_manager) > 0:
self.__fill_in_table(tbody)
Add a th to the parent node.
column_name is used for both the sorting and the styling of the 'th'.
@staticmethod
def __add_sortable_th(parent: HTMLNode, column_name, value=''):
"""Add a th to the parent node.
column_name is used for both the sorting and the styling of the 'th'.
"""
th = HTMLNode(parent.identifier, None, 'th', attributes={'id': column_name + '_th'})
b = HTMLNode(th.identifier, None, 'button', value=value, attributes={'class': 'sortatable', 'data-sort': column_name})
th.append_child(b)
parent.append_child(th)
return th
Add a th to the parent node.
column_name is used for the styling of the 'th'.
@staticmethod
def __add_th(parent: HTMLNode, column_name, value=''):
"""Add a th to the parent node.
column_name is used for the styling of the 'th'.
"""
th = HTMLNode(parent.identifier, None, 'th', attributes={'id': column_name + '_th'}, value=value)
parent.append_child(th)
return th
Add documents to the table.
def __fill_in_table(self, tbody: HTMLNode):
"""Add documents to the table."""
for doc in self.__doc_manager.get_docs_sorted_by_most_viewed():
tr = HTMLNode(tbody.identifier, None, 'tr')
tr.add_attribute('id', doc.folder_name)
tbody.append_child(tr)
for field in DOC_FIELDS:
field_property = DOC_FIELDS[field]
td = HTMLNode(tr.identifier, None, 'td')
tr.append_child(td)
if field_property.is_hidden is True:
td.add_attribute('class', 'hidden')
if field == 'filename':
td.set_value(doc.filename)
elif field == 'author':
td.set_value(doc.author.replace(whakerkit.sg.FIELDS_NAME_SEPARATOR, ' '))
elif field == 'filetype':
td.set_value(doc.filetype)
elif field == 'date':
td.set_value(doc.date.strftime('%Y-%m-%d'))
elif field == 'description':
td.set_value(doc.description)
elif field == 'downloads':
td.set_value(str(doc.downloads))
td = HTMLNode(tr.identifier, None, 'td')
tr.append_child(td)
self.__add_actions_in_column(doc, td)
def __add_actions_in_column(self, doc, td):
self.__add_icon_button(td, f"asideManager.showDetails('{doc.folder_name}');", whakerkit.sg.path + 'statics/icons/info.png', get_msg(MSG_ACTION_DETAILS))
btn = WhakerKitDownloadNode(td.identifier, self.__doc_manager.get_doc_relative_path(doc), doc.folder_name, get_msg(MSG_DOWNLOAD))
btn.add_attribute('class', 'text-reveal-button')
td.append_child(btn)
logging.debug(f'Document author: {doc.author}, current user: {self.__current_user}')
if self.__current_user == doc.author:
self.__add_icon_button(td, f"docManager.describeDocument('{doc.folder_name}');", whakerkit.sg.path + 'statics/icons/describe.png', get_msg(MSG_ACTION_DESCRIBE))
btn = self.__add_icon_button(td, f"docManager.deleteDocument('{doc.folder_name}')", whakerkit.sg.path + 'statics/icons/delete.png', get_msg(MSG_ACTION_DELETE))
btn.add_attribute('id', 'delete_button')
def __add_icon_button(self, parent, onclick, icon, label):
btn = HTMLNode(parent.identifier, None, 'button', attributes={'onclick': onclick, 'class': 'text-reveal-button'})
parent.append_child(btn)
img = HTMLNode(btn.identifier, None, 'img', attributes={'alt': '', 'src': icon, 'onclick': onclick})
btn.append_child(img)
span = HTMLNode(btn.identifier, None, 'span', value=label, attributes={'onclick': onclick})
btn.append_child(span)
return btn