epicyon/question.py

125 lines
4.7 KiB
Python
Raw Normal View History

2020-04-04 10:12:34 +00:00
__filename__ = "question.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-11-29 18:46:21 +00:00
import os
from utils import locatePost
from utils import loadJson
from utils import saveJson
2020-04-04 10:12:34 +00:00
def questionUpdateVotes(baseDir: str, nickname: str, domain: str,
replyJson: {}) -> {}:
2019-11-29 18:46:21 +00:00
""" For a given reply update the votes on a question
2019-11-29 19:22:11 +00:00
Returns the question json object if the vote totals were changed
2019-11-29 18:46:21 +00:00
"""
if not replyJson.get('object'):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not isinstance(replyJson['object'], dict):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not replyJson['object'].get('inReplyTo'):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not replyJson['object']['inReplyTo']:
2019-11-29 19:22:11 +00:00
return None
2020-01-30 10:11:08 +00:00
if not replyJson['object'].get('name'):
2019-11-29 19:22:11 +00:00
return None
2020-04-04 10:12:34 +00:00
inReplyTo = replyJson['object']['inReplyTo']
questionPostFilename = locatePost(baseDir, nickname, domain, inReplyTo)
2019-11-29 18:46:21 +00:00
if not questionPostFilename:
2019-11-29 19:22:11 +00:00
return None
2020-04-04 10:12:34 +00:00
questionJson = loadJson(questionPostFilename)
2019-11-29 18:46:21 +00:00
if not questionJson:
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not questionJson.get('object'):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not isinstance(questionJson['object'], dict):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not questionJson['object'].get('type'):
2019-11-29 19:22:11 +00:00
return None
2020-04-04 10:12:34 +00:00
if questionJson['type'] != 'Question':
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not questionJson['object'].get('oneOf'):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
if not isinstance(questionJson['object']['oneOf'], list):
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:50:08 +00:00
if not questionJson['object'].get('content'):
2019-11-29 19:22:11 +00:00
return None
2020-04-04 10:12:34 +00:00
replyVote = replyJson['object']['name']
2020-01-30 10:11:08 +00:00
# does the reply name field match any possible question option?
2020-04-04 10:12:34 +00:00
foundAnswer = None
2019-11-29 18:46:21 +00:00
for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'):
continue
2020-01-30 10:11:08 +00:00
if possibleAnswer['name'] == replyVote:
2020-04-04 10:12:34 +00:00
foundAnswer = possibleAnswer
2019-11-29 18:46:21 +00:00
break
if not foundAnswer:
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
# update the voters file
2020-04-04 10:12:34 +00:00
votersFileSeparator = ';;;'
votersFilename = questionPostFilename.replace('.json', '.voters')
2019-11-29 18:46:21 +00:00
if not os.path.isfile(votersFilename):
# create a new voters file
2020-04-04 10:12:34 +00:00
votersFile = open(votersFilename, "w")
2019-11-29 18:46:21 +00:00
if votersFile:
2020-04-04 10:12:34 +00:00
votersFile.write(replyJson['actor'] +
votersFileSeparator +
foundAnswer + '\n')
2019-11-29 18:46:21 +00:00
votersFile.close()
else:
if replyJson['actor'] not in open(votersFilename).read():
# append to the voters file
2020-04-04 10:12:34 +00:00
votersFile = open(votersFilename, "a+")
2019-11-29 18:46:21 +00:00
if votersFile:
2020-04-04 10:12:34 +00:00
votersFile.write(replyJson['actor'] +
votersFileSeparator +
foundAnswer + '\n')
2019-11-29 18:46:21 +00:00
votersFile.close()
else:
# change an entry in the voters file
with open(votersFilename, "r") as votersFile:
2020-04-04 10:12:34 +00:00
lines = votersFile.readlines()
newlines = []
saveVotersFile = False
2019-11-29 19:22:11 +00:00
for voteLine in lines:
2020-04-04 10:12:34 +00:00
if voteLine.startswith(replyJson['actor'] +
votersFileSeparator):
newVoteLine = replyJson['actor'] + \
votersFileSeparator + foundAnswer + '\n'
if voteLine == newVoteLine:
2019-11-29 18:46:21 +00:00
break
2020-04-04 10:12:34 +00:00
saveVotersFile = True
2019-11-29 19:22:11 +00:00
newlines.append(newVoteLine)
else:
newlines.append(voteLine)
if saveVotersFile:
with open(votersFilename, "w") as votersFile:
for voteLine in newlines:
votersFile.write(voteLine)
else:
return None
2019-11-29 18:46:21 +00:00
# update the vote counts
2020-04-04 10:12:34 +00:00
questionTotalsChanged = False
2019-11-29 18:46:21 +00:00
for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'):
continue
2020-04-04 10:12:34 +00:00
totalItems = 0
2019-11-29 18:46:21 +00:00
with open(votersFilename, "r") as votersFile:
2020-04-04 10:12:34 +00:00
lines = votersFile.readlines()
2019-11-29 18:46:21 +00:00
for voteLine in lines:
2020-04-04 10:12:34 +00:00
if voteLine.endswith(votersFileSeparator +
possibleAnswer['name'] + '\n'):
totalItems += 1
if possibleAnswer['replies']['totalItems'] != totalItems:
possibleAnswer['replies']['totalItems'] = totalItems
questionTotalsChanged = True
2019-11-29 19:22:11 +00:00
if not questionTotalsChanged:
return None
# save the question with altered totals
2020-04-04 10:12:34 +00:00
saveJson(questionJson, questionPostFilename)
2019-11-29 19:22:11 +00:00
return questionJson