epicyon/question.py

116 lines
4.3 KiB
Python
Raw Normal View History

2020-03-22 20:36:19 +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
2019-11-29 19:22:11 +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
2019-11-29 18:46:21 +00:00
inReplyTo=replyJson['object']['inReplyTo']
questionPostFilename=locatePost(baseDir,nickname,domain,inReplyTo)
if not questionPostFilename:
2019-11-29 19:22:11 +00:00
return None
2019-11-29 18:46:21 +00:00
questionJson=loadJson(questionPostFilename)
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
2019-11-29 18:46:21 +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-01-30 10:11:08 +00:00
replyVote=replyJson['object']['name']
# does the reply name field match any possible question option?
2019-11-29 18:46:21 +00:00
foundAnswer=None
for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'):
continue
2020-01-30 10:11:08 +00:00
if possibleAnswer['name'] == replyVote:
2019-11-29 18:46:21 +00:00
foundAnswer=possibleAnswer
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
votersFileSeparator=';;;'
votersFilename=questionPostFilename.replace('.json','.voters')
if not os.path.isfile(votersFilename):
# create a new voters file
votersFile=open(votersFilename, "w")
if votersFile:
votersFile.write(replyJson['actor']+votersFileSeparator+foundAnswer+'\n')
votersFile.close()
else:
if replyJson['actor'] not in open(votersFilename).read():
# append to the voters file
votersFile=open(votersFilename, "a+")
if votersFile:
votersFile.write(replyJson['actor']+votersFileSeparator+foundAnswer+'\n')
votersFile.close()
else:
# change an entry in the voters file
with open(votersFilename, "r") as votersFile:
2020-03-22 20:36:19 +00:00
lines=votersFile.readlines()
2019-11-29 19:22:11 +00:00
newlines=[]
saveVotersFile=False
for voteLine in lines:
if voteLine.startswith(replyJson['actor']+votersFileSeparator):
newVoteLine=replyJson['actor']+votersFileSeparator+foundAnswer+'\n'
if voteLine==newVoteLine:
2019-11-29 18:46:21 +00:00
break
2019-11-29 19:22:11 +00:00
saveVotersFile=True
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
questionTotalsChanged=False
for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'):
continue
totalItems=0
with open(votersFilename, "r") as votersFile:
2020-03-22 20:36:19 +00:00
lines=votersFile.readlines()
2019-11-29 18:46:21 +00:00
for voteLine in lines:
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
saveJson(questionJson,questionPostFilename)
return questionJson