epicyon/webapp_question.py

114 lines
4.1 KiB
Python
Raw Normal View History

2020-11-09 19:41:01 +00:00
__filename__ = "webapp_question.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2026-01-02 11:36:24 +00:00
__version__ = "1.7.0"
2020-11-09 19:41:01 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-11-09 19:41:01 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Web Interface"
2020-11-09 19:41:01 +00:00
2023-01-26 18:43:43 +00:00
import urllib.parse
2021-12-29 21:55:09 +00:00
from question import is_question
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2022-06-10 13:01:39 +00:00
from utils import text_in_file
2026-05-02 11:34:27 +00:00
from data import is_a_file
2020-11-09 19:41:01 +00:00
2021-12-29 21:55:09 +00:00
def insert_question(base_dir: str, translate: {},
2022-06-01 17:45:59 +00:00
nickname: str, domain: str, content: str,
2022-05-30 18:33:51 +00:00
post_json_object: {}, page_number: int) -> str:
2020-11-09 19:41:01 +00:00
""" Inserts question selection into a post
"""
2021-12-29 21:55:09 +00:00
if not is_question(post_json_object):
2020-11-09 19:41:01 +00:00
return content
2021-12-25 22:09:19 +00:00
if len(post_json_object['object']['oneOf']) == 0:
2020-11-09 19:41:01 +00:00
return content
2026-05-06 10:50:42 +00:00
message_id: str = remove_id_ending(post_json_object['id'])
2022-05-30 18:33:51 +00:00
if '#' in message_id:
message_id = message_id.split('#', 1)[0]
2026-04-27 22:37:42 +00:00
page_number_str: str = ''
2022-05-30 18:33:51 +00:00
if page_number:
page_number_str = '?page=' + str(page_number)
2020-11-09 19:41:01 +00:00
2026-05-06 10:50:42 +00:00
votes_filename: str = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/questions.txt'
2020-11-09 19:41:01 +00:00
2026-04-28 13:04:50 +00:00
show_question_results: bool = False
2026-05-02 11:34:27 +00:00
if is_a_file(votes_filename):
2022-06-10 13:01:39 +00:00
if text_in_file(message_id, votes_filename):
2022-05-30 18:33:51 +00:00
show_question_results = True
2020-11-09 19:41:01 +00:00
2022-05-30 18:33:51 +00:00
if not show_question_results:
2020-11-09 19:41:01 +00:00
# show the question options
content += '<div class="question">'
content += \
'<form method="POST" action="/users/' + \
2022-05-30 18:33:51 +00:00
nickname + '/question' + page_number_str + '">\n'
2020-11-09 19:41:01 +00:00
content += \
'<input type="hidden" name="messageId" value="' + \
2022-05-30 18:33:51 +00:00
message_id + '">\n<br>\n'
2021-12-25 22:09:19 +00:00
for choice in post_json_object['object']['oneOf']:
2020-11-09 19:41:01 +00:00
if not choice.get('type'):
continue
if not choice.get('name'):
continue
2026-05-06 10:50:42 +00:00
if not isinstance(choice['name'], str):
continue
quoted_name: str = urllib.parse.quote_plus(choice['name'])
2020-11-09 19:41:01 +00:00
content += \
'<input type="radio" name="answer" value="' + \
2023-01-26 18:43:43 +00:00
quoted_name + '" tabindex="10"> ' + \
2022-06-09 17:58:55 +00:00
choice['name'] + '<br><br>\n'
2020-11-09 19:41:01 +00:00
content += \
'<input type="submit" value="' + \
2022-06-09 17:58:55 +00:00
translate['Vote'] + '" class="vote" tabindex="10"><br><br>\n'
2020-11-09 19:41:01 +00:00
content += '</form>\n</div>\n'
else:
# show the responses to a question
content += '<div class="questionresult">\n'
# get the maximum number of votes
2026-04-28 13:48:16 +00:00
max_votes: int = 1
2022-05-30 18:33:51 +00:00
for question_option in post_json_object['object']['oneOf']:
if not question_option.get('name'):
2020-11-09 19:41:01 +00:00
continue
2022-05-30 18:33:51 +00:00
if not question_option.get('replies'):
2020-11-09 19:41:01 +00:00
continue
2026-04-28 13:04:50 +00:00
votes: int = 0
2020-11-09 19:41:01 +00:00
try:
2022-05-30 18:33:51 +00:00
votes = int(question_option['replies']['totalItems'])
2020-11-09 19:41:01 +00:00
except BaseException:
2021-12-29 21:55:09 +00:00
print('EX: insert_question unable to convert to int')
2022-05-30 18:33:51 +00:00
if votes > max_votes:
max_votes = int(votes+1)
2020-11-09 19:41:01 +00:00
# show the votes as sliders
2022-05-30 18:33:51 +00:00
for question_option in post_json_object['object']['oneOf']:
if not question_option.get('name'):
2020-11-09 19:41:01 +00:00
continue
2026-05-06 10:50:42 +00:00
if not isinstance(question_option['name'], str):
continue
2022-05-30 18:33:51 +00:00
if not question_option.get('replies'):
2020-11-09 19:41:01 +00:00
continue
2026-04-28 13:04:50 +00:00
votes: int = 0
2020-11-09 19:41:01 +00:00
try:
2022-05-30 18:33:51 +00:00
votes = int(question_option['replies']['totalItems'])
2020-11-09 19:41:01 +00:00
except BaseException:
2021-12-29 21:55:09 +00:00
print('EX: insert_question unable to convert to int 2')
2026-05-06 10:50:42 +00:00
votes_percent: str = str(int(votes * 100 / max_votes))
2021-11-04 21:08:57 +00:00
2020-11-09 19:41:01 +00:00
content += \
2021-11-04 21:08:57 +00:00
'<p>\n' + \
' <label class="labels">' + \
2022-05-30 18:33:51 +00:00
question_option['name'] + '</label><br>\n' + \
2021-11-04 21:08:57 +00:00
' <svg class="voteresult">\n' + \
2022-05-30 18:33:51 +00:00
' <rect width="' + votes_percent + \
2021-11-04 21:08:57 +00:00
'%" class="voteresultbar" />\n' + \
' </svg>' + \
2022-05-30 18:33:51 +00:00
' <label class="labels">' + votes_percent + '%</label>\n' + \
2021-11-04 21:08:57 +00:00
'</p>\n'
2020-11-09 19:41:01 +00:00
content += '</div>\n'
return content