epicyon/webapp_welcome_profile.py

136 lines
5.2 KiB
Python
Raw Normal View History

2021-02-25 10:37:20 +00:00
__filename__ = "webapp_welcome_profile.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2021-02-25 10:37:20 +00:00
__status__ = "Production"
2021-06-26 11:27:14 +00:00
__module_group__ = "Onboarding"
2021-02-25 10:37:20 +00:00
import os
from shutil import copyfile
2021-12-27 15:43:22 +00:00
from utils import remove_html
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:08:58 +00:00
from utils import get_config_param
2021-12-26 14:26:16 +00:00
from utils import get_image_extensions
2021-12-26 15:44:28 +00:00
from utils import get_image_formats
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-29 21:55:09 +00:00
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import edit_text_field
from markdown import markdown_to_html
2021-02-25 10:37:20 +00:00
2021-12-29 21:55:09 +00:00
def html_welcome_profile(base_dir: str, nickname: str, domain: str,
http_prefix: str, domain_full: str,
language: str, translate: {},
theme_name: str) -> str:
2021-02-25 10:37:20 +00:00
"""Returns the welcome profile screen to set avatar and bio
"""
# set a custom background for the welcome screen
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/accounts/welcome-background-custom.jpg'):
if not os.path.isfile(base_dir + '/accounts/welcome-background.jpg'):
copyfile(base_dir + '/accounts/welcome-background-custom.jpg',
base_dir + '/accounts/welcome-background.jpg')
2021-02-25 10:37:20 +00:00
2022-01-04 14:44:48 +00:00
profile_text = 'Welcome to Epicyon'
profile_filename = base_dir + '/accounts/welcome_profile.md'
if not os.path.isfile(profile_filename):
default_filename = None
2021-12-25 23:35:50 +00:00
if theme_name:
2022-01-04 14:44:48 +00:00
default_filename = \
2021-12-25 23:35:50 +00:00
base_dir + '/theme/' + theme_name + '/welcome/' + \
2021-02-27 10:27:39 +00:00
'profile_' + language + '.md'
2022-01-04 14:44:48 +00:00
if not os.path.isfile(default_filename):
default_filename = None
if not default_filename:
default_filename = \
2021-12-25 16:17:53 +00:00
base_dir + '/defaultwelcome/profile_' + language + '.md'
2022-01-04 14:44:48 +00:00
if not os.path.isfile(default_filename):
default_filename = base_dir + '/defaultwelcome/profile_en.md'
copyfile(default_filename, profile_filename)
2022-01-04 14:44:48 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 14:44:48 +00:00
if not instance_title:
instance_title = 'Epicyon'
2022-01-04 14:44:48 +00:00
if os.path.isfile(profile_filename):
with open(profile_filename, 'r') as fp_pro:
profile_text = fp_pro.read()
profile_text = profile_text.replace('INSTANCE', instance_title)
profile_text = markdown_to_html(remove_html(profile_text))
2021-02-25 10:37:20 +00:00
2022-01-04 14:44:48 +00:00
profile_form = ''
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-welcome.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/welcome.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/welcome.css'
2021-02-25 10:37:20 +00:00
2022-01-04 14:44:48 +00:00
profile_form = \
html_header_with_external_style(css_filename, instance_title, None)
2021-02-25 10:37:20 +00:00
# get the url of the avatar
2022-01-04 14:44:48 +00:00
ext = 'png'
2021-12-26 14:26:16 +00:00
for ext in get_image_extensions():
2022-01-04 14:44:48 +00:00
avatar_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/avatar.' + ext
2022-01-04 14:44:48 +00:00
if os.path.isfile(avatar_filename):
2021-02-25 10:37:20 +00:00
break
2022-01-04 14:44:48 +00:00
avatar_url = \
local_actor_url(http_prefix, nickname, domain_full) + \
'/avatar.' + ext
2021-02-25 10:37:20 +00:00
2022-01-04 14:44:48 +00:00
image_formats = get_image_formats()
profile_form += '<div class="container">' + profile_text + '</div>\n'
profile_form += \
2021-02-25 12:17:41 +00:00
'<form enctype="multipart/form-data" method="POST" ' + \
'accept-charset="UTF-8" ' + \
2021-02-25 14:53:19 +00:00
'action="/users/' + nickname + '/profiledata">\n'
2022-01-04 14:44:48 +00:00
profile_form += '<div class="container">\n'
profile_form += ' <center>\n'
profile_form += ' <img class="welcomeavatar" src="'
profile_form += avatar_url + '"><br>\n'
profile_form += ' <input type="file" id="avatar" name="avatar" '
profile_form += 'accept="' + image_formats + '">\n'
profile_form += ' </center>\n'
profile_form += '</div>\n'
2021-02-25 12:17:41 +00:00
2022-01-04 14:44:48 +00:00
profile_form += '<center>\n'
profile_form += \
2021-02-25 13:26:09 +00:00
' <button type="submit" class="button" ' + \
'name="previewAvatar">' + translate['Preview'] + '</button> '
2022-01-04 14:44:48 +00:00
profile_form += '</center>\n'
2021-02-25 13:26:09 +00:00
2022-01-04 14:44:48 +00:00
actor_filename = acct_dir(base_dir, nickname, domain) + '.json'
actor_json = load_json(actor_filename)
display_nickname = actor_json['name']
profile_form += '<div class="container">\n'
profile_form += \
2021-12-29 21:55:09 +00:00
edit_text_field(translate['Nickname'], 'displayNickname',
2022-01-04 14:44:48 +00:00
display_nickname)
2021-02-25 12:17:41 +00:00
2022-01-04 14:44:48 +00:00
bio_str = \
2021-12-26 10:29:52 +00:00
actor_json['summary'].replace('<p>', '').replace('</p>', '')
2022-01-04 14:44:48 +00:00
if not bio_str:
bio_str = translate['Your bio']
profile_form += ' <label class="labels">' + \
2021-02-25 12:24:26 +00:00
translate['Your bio'] + '</label><br>\n'
2022-01-04 14:44:48 +00:00
profile_form += ' <textarea id="message" name="bio" ' + \
2021-02-28 14:26:04 +00:00
'style="height:130px" spellcheck="true">' + \
2022-01-04 14:44:48 +00:00
bio_str + '</textarea>\n'
profile_form += '</div>\n'
2021-02-25 12:17:41 +00:00
2022-01-04 14:44:48 +00:00
profile_form += '<div class="container next">\n'
profile_form += \
2021-02-25 12:24:26 +00:00
' <button type="submit" class="button" ' + \
2021-02-25 16:55:40 +00:00
'name="initialWelcomeScreen">' + translate['Go Back'] + '</button> '
2022-01-04 14:44:48 +00:00
profile_form += \
2021-02-25 12:24:26 +00:00
' <button type="submit" class="button" ' + \
2021-02-25 16:55:40 +00:00
'name="finalWelcomeScreen">' + translate['Next'] + '</button>\n'
2022-01-04 14:44:48 +00:00
profile_form += '</div>\n'
2021-02-25 12:24:26 +00:00
2022-01-04 14:44:48 +00:00
profile_form += '</form>\n'
profile_form += html_footer()
return profile_form