Modern ActivityPub compliant server, designed for simplicity and accessibility. Includes calendar and sharing economy features to empower your federated community. https://code.freedombone.net/bashrc/epicyon Docs: https://epicyon.net/#install
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webapp_question.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. __filename__ = "webapp_question.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "1.2.0"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. from question import isQuestion
  10. from utils import removeIdEnding
  11. def insertQuestion(baseDir: str, translate: {},
  12. nickname: str, domain: str, port: int,
  13. content: str,
  14. postJsonObject: {}, pageNumber: int) -> str:
  15. """ Inserts question selection into a post
  16. """
  17. if not isQuestion(postJsonObject):
  18. return content
  19. if len(postJsonObject['object']['oneOf']) == 0:
  20. return content
  21. messageId = removeIdEnding(postJsonObject['id'])
  22. if '#' in messageId:
  23. messageId = messageId.split('#', 1)[0]
  24. pageNumberStr = ''
  25. if pageNumber:
  26. pageNumberStr = '?page=' + str(pageNumber)
  27. votesFilename = \
  28. baseDir + '/accounts/' + nickname + '@' + domain + '/questions.txt'
  29. showQuestionResults = False
  30. if os.path.isfile(votesFilename):
  31. if messageId in open(votesFilename).read():
  32. showQuestionResults = True
  33. if not showQuestionResults:
  34. # show the question options
  35. content += '<div class="question">'
  36. content += \
  37. '<form method="POST" action="/users/' + \
  38. nickname + '/question' + pageNumberStr + '">\n'
  39. content += \
  40. '<input type="hidden" name="messageId" value="' + \
  41. messageId + '">\n<br>\n'
  42. for choice in postJsonObject['object']['oneOf']:
  43. if not choice.get('type'):
  44. continue
  45. if not choice.get('name'):
  46. continue
  47. content += \
  48. '<input type="radio" name="answer" value="' + \
  49. choice['name'] + '"> ' + choice['name'] + '<br><br>\n'
  50. content += \
  51. '<input type="submit" value="' + \
  52. translate['Vote'] + '" class="vote"><br><br>\n'
  53. content += '</form>\n</div>\n'
  54. else:
  55. # show the responses to a question
  56. content += '<div class="questionresult">\n'
  57. # get the maximum number of votes
  58. maxVotes = 1
  59. for questionOption in postJsonObject['object']['oneOf']:
  60. if not questionOption.get('name'):
  61. continue
  62. if not questionOption.get('replies'):
  63. continue
  64. votes = 0
  65. try:
  66. votes = int(questionOption['replies']['totalItems'])
  67. except BaseException:
  68. pass
  69. if votes > maxVotes:
  70. maxVotes = int(votes+1)
  71. # show the votes as sliders
  72. questionCtr = 1
  73. for questionOption in postJsonObject['object']['oneOf']:
  74. if not questionOption.get('name'):
  75. continue
  76. if not questionOption.get('replies'):
  77. continue
  78. votes = 0
  79. try:
  80. votes = int(questionOption['replies']['totalItems'])
  81. except BaseException:
  82. pass
  83. votesPercent = str(int(votes * 100 / maxVotes))
  84. content += \
  85. '<p><input type="text" title="' + str(votes) + \
  86. '" name="skillName' + str(questionCtr) + \
  87. '" value="' + questionOption['name'] + \
  88. ' (' + str(votes) + ')" style="width:40%">\n'
  89. content += \
  90. '<input type="range" min="1" max="100" ' + \
  91. 'class="slider" title="' + \
  92. str(votes) + '" name="skillValue' + str(questionCtr) + \
  93. '" value="' + votesPercent + '"></p>\n'
  94. questionCtr += 1
  95. content += '</div>\n'
  96. return content