__filename__ = "webapp_column_right.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Web Interface Columns"
import os
from content import remove_long_words
from content import limit_repeated_words
from utils import data_dir
from utils import get_image_extensions
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 utils import date_from_string_format
from posts import is_moderator
from newswire import get_newswire_favicon_url
from webapp_utils import get_right_image_file
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import get_banner_file
from webapp_utils import html_post_separator
from webapp_utils import header_buttons_front_screen
from webapp_utils import edit_text_field
from webapp_utils import text_mode_browser
def _votes_indicator(total_votes: int, positive_voting: bool) -> str:
"""Returns an indicator of the number of votes on a newswire item
"""
if total_votes <= 0:
return ''
total_votes_str = ' '
for _ in range(total_votes):
if positive_voting:
total_votes_str += '✓'
else:
total_votes_str += '✗'
return total_votes_str
def get_right_column_content(base_dir: str, nickname: str, domain_full: str,
translate: {}, moderator: bool, editor: bool,
newswire: {}, positive_voting: bool,
show_back_button: bool, timeline_path: str,
show_publish_button: bool,
show_publish_as_icon: bool,
rss_icon_at_top: bool,
publish_button_at_top: bool,
authorized: bool,
show_header_image: bool,
theme: str,
default_timeline: str,
access_keys: {}) -> str:
"""Returns html content for the right column
"""
html_str = ''
domain = remove_domain_port(domain_full)
if authorized:
# only show the publish button if logged in, otherwise replace it with
# a login button
title_str = translate['Publish a blog article']
if default_timeline == 'tlfeatures':
title_str = translate['Publish a news article']
publish_button_str = \
' ' + \
'\n'
else:
# if not logged in then replace the publish button with
# a login button
publish_button_str = \
' ' + \
'\n'
# show publish button at the top if needed
if publish_button_at_top:
html_str += '
' + publish_button_str + '
'
# show a column header image, eg. title of the theme or newswire banner
edit_image_class = ''
if show_header_image:
right_image_file, right_column_image_filename = \
get_right_image_file(base_dir, nickname, domain, theme)
# show the image at the top of the column
edit_image_class = 'rightColEdit'
if os.path.isfile(right_column_image_filename):
edit_image_class = 'rightColEditImage'
html_str += \
'\n
\n' + \
' \n' + \
'
\n'
if show_publish_button or editor or rss_icon_at_top:
if not show_header_image:
html_str += '
'
if edit_image_class == 'rightColEdit':
html_str += '\n
\n'
# whether to show a back icon
# This is probably going to be osolete soon
if show_back_button:
html_str += \
' ' + \
'\n'
if show_publish_button and not publish_button_at_top:
if not show_publish_as_icon:
html_str += publish_button_str
# start of newswire column icons
html_str += '
\n'
# 1. show publish icon at top
if show_publish_button:
if show_publish_as_icon:
title_str = translate['Publish a blog article']
if default_timeline == 'tlfeatures':
title_str = translate['Publish a news article']
html_str += \
' ' + \
'\n'
# 2. show the RSS and categories icons
rss_icon_str = \
' ' + \
'\n'
rss_icon_str += \
' ' + \
'\n'
if rss_icon_at_top:
html_str += rss_icon_str
# 3. show the edit icon
if editor:
dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/newswiremoderation.txt'):
# show the edit icon highlighted
html_str += \
' ' + \
'\n'
else:
# show the edit icon
html_str += \
' ' + \
'\n'
# end of newswire column icons
html_str += '
\n'
if edit_image_class == 'rightColEdit':
html_str += '
\n'
else:
if show_header_image:
html_str += ' \n'
if show_publish_button or editor or rss_icon_at_top:
if not show_header_image:
html_str += '
'
# show the newswire lines
newswire_content_str = \
_html_newswire(base_dir, newswire, nickname, moderator, translate,
positive_voting)
html_str += newswire_content_str
# show the rss icon at the bottom, typically on the right hand side
if newswire_content_str and not rss_icon_at_top:
html_str += '
' + rss_icon_str + '
'
return html_str
def _get_broken_fav_substitute() -> str:
"""Substitute link used if a favicon is not available
"""
return " onerror=\"this.onerror=null; this.src='/newswire_favicon.ico'\""
def _html_newswire(base_dir: str, newswire: {}, nickname: str, moderator: bool,
translate: {}, positive_voting: bool) -> str:
"""Converts a newswire dict into html
"""
separator_str = html_post_separator(base_dir, 'right')
html_str = ''
for date_str, 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]
published_date = \
date_from_string_format(date_str, ["%Y-%m-%d %H:%M:%S%z"])
if not published_date:
print('EX: _html_newswire bad date format ' + date_str)
continue
date_shown = published_date.strftime("%Y-%m-%d %H:%M")
date_str_link = date_str.replace('T', ' ')
date_str_link = date_str_link.replace('Z', '')
url = item[1]
favicon_url = get_newswire_favicon_url(url)
favicon_link = ''
if favicon_url:
cached_favicon_filename = \
get_fav_filename_from_url(base_dir, favicon_url)
if os.path.isfile(cached_favicon_filename):
favicon_url = \
cached_favicon_filename.replace(base_dir, '')
else:
extensions = get_image_extensions()
for ext in extensions:
cached_favicon_filename = \
get_fav_filename_from_url(base_dir, favicon_url)
cached_favicon_filename = \
cached_favicon_filename.replace('.ico', '.' + ext)
if os.path.isfile(cached_favicon_filename):
favicon_url = \
cached_favicon_filename.replace(base_dir, '')
favicon_link = \
''
moderated_item = item[5]
link_url = url
# is this a podcast episode?
if len(item) > 8:
# change the link url to a podcast episode screen
podcast_properties = item[8]
if podcast_properties:
if podcast_properties.get('image'):
episode_id = date_str.replace(' ', '__')
episode_id = episode_id.replace(':', 'aa')
link_url = \
'/users/' + nickname + '/?podepisode=' + episode_id
html_str += separator_str
if moderated_item and 'vote:' + nickname in item[2]:
total_votes_str = ''
total_votes = 0
if moderator:
total_votes = votes_on_newswire_item(item[2])
total_votes_str = \
_votes_indicator(total_votes, positive_voting)
title = remove_long_words(item[0], 16, []).replace('\n', ' ')
title = limit_repeated_words(title, 6)
html_str += '
\n'
else:
html_str += ' '
html_str += date_shown + '\n'
else:
total_votes_str = ''
total_votes = 0
if moderator:
if moderated_item:
total_votes = votes_on_newswire_item(item[2])
# show a number of ticks or crosses for how many
# votes for or against
total_votes_str = \
_votes_indicator(total_votes, positive_voting)
title = remove_long_words(item[0], 16, []).replace('\n', ' ')
title = limit_repeated_words(title, 6)
if moderator and moderated_item:
html_str += '