| 
									
										
										
										
											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: {}, | 
					
						
							|  |  |  |                     nickname: str, domain: str, port: int, | 
					
						
							|  |  |  |                     content: str, | 
					
						
							|  |  |  |                     post_json_object: {}, pageNumber: 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 | 
					
						
							| 
									
										
										
										
											2021-12-27 11:20:57 +00:00
										 |  |  |     messageId = remove_id_ending(post_json_object['id']) | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  |     if '#' in messageId: | 
					
						
							|  |  |  |         messageId = messageId.split('#', 1)[0] | 
					
						
							|  |  |  |     pageNumberStr = '' | 
					
						
							|  |  |  |     if pageNumber: | 
					
						
							|  |  |  |         pageNumberStr = '?page=' + str(pageNumber) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     votesFilename = \ | 
					
						
							| 
									
										
										
										
											2021-12-26 12:02:29 +00:00
										 |  |  |         acct_dir(base_dir, nickname, domain) + '/questions.txt' | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     showQuestionResults = False | 
					
						
							|  |  |  |     if os.path.isfile(votesFilename): | 
					
						
							|  |  |  |         if messageId in open(votesFilename).read(): | 
					
						
							|  |  |  |             showQuestionResults = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not showQuestionResults: | 
					
						
							|  |  |  |         # show the question options | 
					
						
							|  |  |  |         content += '<div class="question">' | 
					
						
							|  |  |  |         content += \ | 
					
						
							|  |  |  |             '<form method="POST" action="/users/' + \ | 
					
						
							|  |  |  |             nickname + '/question' + pageNumberStr + '">\n' | 
					
						
							|  |  |  |         content += \ | 
					
						
							|  |  |  |             '<input type="hidden" name="messageId" value="' + \ | 
					
						
							|  |  |  |             messageId + '">\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 | 
					
						
							|  |  |  |         maxVotes = 1 | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |         for questionOption in post_json_object['object']['oneOf']: | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  |             if not questionOption.get('name'): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if not questionOption.get('replies'): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             votes = 0 | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 votes = int(questionOption['replies']['totalItems']) | 
					
						
							|  |  |  |             except BaseException: | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |                 print('EX: insert_question unable to convert to int') | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  |             if votes > maxVotes: | 
					
						
							|  |  |  |                 maxVotes = int(votes+1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # show the votes as sliders | 
					
						
							|  |  |  |         questionCtr = 1 | 
					
						
							| 
									
										
										
										
											2021-12-25 22:09:19 +00:00
										 |  |  |         for questionOption in post_json_object['object']['oneOf']: | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  |             if not questionOption.get('name'): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if not questionOption.get('replies'): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             votes = 0 | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 votes = int(questionOption['replies']['totalItems']) | 
					
						
							|  |  |  |             except BaseException: | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |                 print('EX: insert_question unable to convert to int 2') | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  |             votesPercent = str(int(votes * 100 / maxVotes)) | 
					
						
							| 
									
										
										
										
											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">' + \ | 
					
						
							|  |  |  |                 questionOption['name'] + '</label><br>\n' + \ | 
					
						
							|  |  |  |                 '  <svg class="voteresult">\n' + \ | 
					
						
							|  |  |  |                 '    <rect width="' + votesPercent + \ | 
					
						
							|  |  |  |                 '%" class="voteresultbar" />\n' + \ | 
					
						
							|  |  |  |                 '  </svg>' + \ | 
					
						
							|  |  |  |                 '  <label class="labels">' + votesPercent + '%</label>\n' + \ | 
					
						
							|  |  |  |                 '</p>\n' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 19:41:01 +00:00
										 |  |  |             questionCtr += 1 | 
					
						
							|  |  |  |         content += '</div>\n' | 
					
						
							|  |  |  |     return content |