__filename__ = "webapp_column_right.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Web Interface Columns"
import os
from datetime import datetime
from content import removeLongWords
from content import limitRepeatedWords
from utils import get_fav_filename_from_url
from utils import get_base_content_from_post
from utils import remove_html
from utils import locate_post
from utils import load_json
from utils import votes_on_newswire_item
from utils import get_nickname_from_actor
from utils import is_editor
from utils import get_config_param
from utils import remove_domain_port
from utils import acct_dir
from posts import isModerator
from newswire import getNewswireFaviconUrl
from webapp_utils import getRightImageFile
from webapp_utils import htmlHeaderWithExternalStyle
from webapp_utils import htmlFooter
from webapp_utils import getBannerFile
from webapp_utils import htmlPostSeparator
from webapp_utils import headerButtonsFrontScreen
from webapp_utils import editTextField
def _votesIndicator(totalVotes: int, positive_voting: bool) -> str:
"""Returns an indicator of the number of votes on a newswire item
"""
if totalVotes <= 0:
return ''
totalVotesStr = ' '
for v in range(totalVotes):
if positive_voting:
totalVotesStr += '✓'
else:
totalVotesStr += '✗'
return totalVotesStr
def getRightColumnContent(base_dir: str, nickname: str, domain_full: str,
http_prefix: str, translate: {},
moderator: bool, editor: bool,
newswire: {}, positive_voting: bool,
showBackButton: bool, timelinePath: str,
showPublishButton: bool,
show_publish_as_icon: bool,
rss_icon_at_top: bool,
publish_button_at_top: bool,
authorized: bool,
showHeaderImage: bool,
theme: str,
defaultTimeline: str,
accessKeys: {}) -> str:
"""Returns html content for the right column
"""
htmlStr = ''
domain = remove_domain_port(domain_full)
if authorized:
# only show the publish button if logged in, otherwise replace it with
# a login button
titleStr = translate['Publish a blog article']
if defaultTimeline == 'tlfeatures':
titleStr = translate['Publish a news article']
publishButtonStr = \
' ' + \
'\n'
else:
# if not logged in then replace the publish button with
# a login button
publishButtonStr = \
' \n'
# show publish button at the top if needed
if publish_button_at_top:
htmlStr += '
' + publishButtonStr + '
'
# show a column header image, eg. title of the theme or newswire banner
editImageClass = ''
if showHeaderImage:
rightImageFile, rightColumnImageFilename = \
getRightImageFile(base_dir, nickname, domain, theme)
# show the image at the top of the column
editImageClass = 'rightColEdit'
if os.path.isfile(rightColumnImageFilename):
editImageClass = 'rightColEditImage'
htmlStr += \
'\n
\n' + \
' \n' + \
'
\n'
if showPublishButton or editor or rss_icon_at_top:
if not showHeaderImage:
htmlStr += '
'
if editImageClass == 'rightColEdit':
htmlStr += '\n
\n'
# whether to show a back icon
# This is probably going to be osolete soon
if showBackButton:
htmlStr += \
' ' + \
'\n'
if showPublishButton and not publish_button_at_top:
if not show_publish_as_icon:
htmlStr += publishButtonStr
# show the edit icon
if editor:
if os.path.isfile(base_dir + '/accounts/newswiremoderation.txt'):
# show the edit icon highlighted
htmlStr += \
' ' + \
'\n'
else:
# show the edit icon
htmlStr += \
' ' + \
'\n'
# show the RSS icons
rssIconStr = \
' ' + \
'\n'
rssIconStr += \
' ' + \
'\n'
if rss_icon_at_top:
htmlStr += rssIconStr
# show publish icon at top
if showPublishButton:
if show_publish_as_icon:
titleStr = translate['Publish a blog article']
if defaultTimeline == 'tlfeatures':
titleStr = translate['Publish a news article']
htmlStr += \
' ' + \
'\n'
if editImageClass == 'rightColEdit':
htmlStr += '
\n'
else:
if showHeaderImage:
htmlStr += ' \n'
if showPublishButton or editor or rss_icon_at_top:
if not showHeaderImage:
htmlStr += '
'
# show the newswire lines
newswireContentStr = \
_htmlNewswire(base_dir, newswire, nickname, moderator, translate,
positive_voting)
htmlStr += newswireContentStr
# show the rss icon at the bottom, typically on the right hand side
if newswireContentStr and not rss_icon_at_top:
htmlStr += '
' + rssIconStr + '
'
return htmlStr
def _getBrokenFavSubstitute() -> str:
"""Substitute link used if a favicon is not available
"""
return " onerror=\"this.onerror=null; this.src='/newswire_favicon.ico'\""
def _htmlNewswire(base_dir: str, newswire: {}, nickname: str, moderator: bool,
translate: {}, positive_voting: bool) -> str:
"""Converts a newswire dict into html
"""
separatorStr = htmlPostSeparator(base_dir, 'right')
htmlStr = ''
for dateStr, item in newswire.items():
item[0] = remove_html(item[0]).strip()
if not item[0]:
continue
# remove any CDATA
if 'CDATA[' in item[0]:
item[0] = item[0].split('CDATA[')[1]
if ']' in item[0]:
item[0] = item[0].split(']')[0]
try:
publishedDate = \
datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S%z")
except BaseException:
print('EX: _htmlNewswire bad date format ' + dateStr)
continue
dateShown = publishedDate.strftime("%Y-%m-%d %H:%M")
dateStrLink = dateStr.replace('T', ' ')
dateStrLink = dateStrLink.replace('Z', '')
url = item[1]
faviconUrl = getNewswireFaviconUrl(url)
faviconLink = ''
if faviconUrl:
cachedFaviconFilename = \
get_fav_filename_from_url(base_dir, faviconUrl)
if os.path.isfile(cachedFaviconFilename):
faviconUrl = \
cachedFaviconFilename.replace(base_dir, '')
else:
extensions = ('png', 'jpg', 'gif', 'avif', 'svg', 'webp')
for ext in extensions:
cachedFaviconFilename = \
get_fav_filename_from_url(base_dir, faviconUrl)
cachedFaviconFilename = \
cachedFaviconFilename.replace('.ico', '.' + ext)
if os.path.isfile(cachedFaviconFilename):
faviconUrl = \
cachedFaviconFilename.replace(base_dir, '')
faviconLink = \
''
moderatedItem = item[5]
htmlStr += separatorStr
if moderatedItem and 'vote:' + nickname in item[2]:
totalVotesStr = ''
totalVotes = 0
if moderator:
totalVotes = votes_on_newswire_item(item[2])
totalVotesStr = \
_votesIndicator(totalVotes, positive_voting)
title = removeLongWords(item[0], 16, []).replace('\n', ' ')
title = limitRepeatedWords(title, 6)
htmlStr += '
\n'
else:
htmlStr += ' '
htmlStr += dateShown + '\n'
else:
totalVotesStr = ''
totalVotes = 0
if moderator:
if moderatedItem:
totalVotes = votes_on_newswire_item(item[2])
# show a number of ticks or crosses for how many
# votes for or against
totalVotesStr = \
_votesIndicator(totalVotes, positive_voting)
title = removeLongWords(item[0], 16, []).replace('\n', ' ')
title = limitRepeatedWords(title, 6)
if moderator and moderatedItem:
htmlStr += '