epicyon/webapp_question.py

106 lines
3.7 KiB
Python
Raw Normal View History

2020-11-09 19:41:01 +00:00
__filename__ = "webapp_question.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.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
import os
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
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
2022-05-30 18:33:51 +00:00
message_id = remove_id_ending(post_json_object['id'])
if '#' in message_id:
message_id = message_id.split('#', 1)[0]
page_number_str = ''
if page_number:
page_number_str = '?page=' + str(page_number)
2020-11-09 19:41:01 +00:00
2022-05-30 18:33:51 +00:00
votes_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/questions.txt'
2020-11-09 19:41:01 +00:00
2022-05-30 18:33:51 +00:00
show_question_results = False
if os.path.isfile(votes_filename):
2022-06-09 14:46:30 +00:00
if message_id in open(votes_filename, encoding='utf-8').read():
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
content += \
'<input type="radio" name="answer" value="' + \
choice['name'] + '"> ' + choice['name'] + '<br><br>\n'
content += \
'<input type="submit" value="' + \
translate['Vote'] + '" class="vote"><br><br>\n'
content += '</form>\n</div>\n'
else:
# show the responses to a question
content += '<div class="questionresult">\n'
# get the maximum number of votes
2022-05-30 18:33:51 +00:00
max_votes = 1
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
votes = 0
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
2022-05-30 18:33:51 +00:00
if not question_option.get('replies'):
2020-11-09 19:41:01 +00:00
continue
votes = 0
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')
2022-05-30 18:33:51 +00:00
votes_percent = 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