mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
a884e8a755
commit
2f8bcc9f04
147
question.py
147
question.py
|
@ -15,109 +15,120 @@ from utils import has_object_dict
|
||||||
|
|
||||||
|
|
||||||
def question_update_votes(base_dir: str, nickname: str, domain: str,
|
def question_update_votes(base_dir: str, nickname: str, domain: str,
|
||||||
replyJson: {}) -> ({}, str):
|
reply_json: {}) -> ({}, str):
|
||||||
""" For a given reply update the votes on a question
|
""" For a given reply update the votes on a question
|
||||||
Returns the question json object if the vote totals were changed
|
Returns the question json object if the vote totals were changed
|
||||||
"""
|
"""
|
||||||
if not has_object_dict(replyJson):
|
if not has_object_dict(reply_json):
|
||||||
return None, None
|
return None, None
|
||||||
if not replyJson['object'].get('inReplyTo'):
|
if not reply_json['object'].get('inReplyTo'):
|
||||||
return None, None
|
return None, None
|
||||||
if not replyJson['object']['inReplyTo']:
|
if not reply_json['object']['inReplyTo']:
|
||||||
return None, None
|
return None, None
|
||||||
if not isinstance(replyJson['object']['inReplyTo'], str):
|
if not isinstance(reply_json['object']['inReplyTo'], str):
|
||||||
return None, None
|
return None, None
|
||||||
if not replyJson['object'].get('name'):
|
if not reply_json['object'].get('name'):
|
||||||
return None, None
|
return None, None
|
||||||
inReplyTo = replyJson['object']['inReplyTo']
|
in_reply_to = reply_json['object']['inReplyTo']
|
||||||
questionPostFilename = locate_post(base_dir, nickname, domain, inReplyTo)
|
question_post_filename = \
|
||||||
if not questionPostFilename:
|
locate_post(base_dir, nickname, domain, in_reply_to)
|
||||||
|
if not question_post_filename:
|
||||||
return None, None
|
return None, None
|
||||||
questionJson = load_json(questionPostFilename)
|
question_json = load_json(question_post_filename)
|
||||||
if not questionJson:
|
if not question_json:
|
||||||
return None, None
|
return None, None
|
||||||
if not has_object_dict(questionJson):
|
if not has_object_dict(question_json):
|
||||||
return None, None
|
return None, None
|
||||||
if not questionJson['object'].get('type'):
|
if not question_json['object'].get('type'):
|
||||||
return None, None
|
return None, None
|
||||||
if questionJson['type'] != 'Question':
|
if question_json['type'] != 'Question':
|
||||||
return None, None
|
return None, None
|
||||||
if not questionJson['object'].get('oneOf'):
|
if not question_json['object'].get('oneOf'):
|
||||||
return None, None
|
return None, None
|
||||||
if not isinstance(questionJson['object']['oneOf'], list):
|
if not isinstance(question_json['object']['oneOf'], list):
|
||||||
return None, None
|
return None, None
|
||||||
if not questionJson['object'].get('content'):
|
if not question_json['object'].get('content'):
|
||||||
return None, None
|
return None, None
|
||||||
replyVote = replyJson['object']['name']
|
reply_vote = reply_json['object']['name']
|
||||||
# does the reply name field match any possible question option?
|
# does the reply name field match any possible question option?
|
||||||
foundAnswer = None, None
|
found_answer = None, None
|
||||||
for possibleAnswer in questionJson['object']['oneOf']:
|
for possible_answer in question_json['object']['oneOf']:
|
||||||
if not possibleAnswer.get('name'):
|
if not possible_answer.get('name'):
|
||||||
continue
|
continue
|
||||||
if possibleAnswer['name'] == replyVote:
|
if possible_answer['name'] == reply_vote:
|
||||||
foundAnswer = possibleAnswer
|
found_answer = possible_answer
|
||||||
break
|
break
|
||||||
if not foundAnswer:
|
if not found_answer:
|
||||||
return None, None
|
return None, None
|
||||||
# update the voters file
|
# update the voters file
|
||||||
votersFileSeparator = ';;;'
|
voters_file_separator = ';;;'
|
||||||
votersFilename = questionPostFilename.replace('.json', '.voters')
|
voters_filename = question_post_filename.replace('.json', '.voters')
|
||||||
if not os.path.isfile(votersFilename):
|
if not os.path.isfile(voters_filename):
|
||||||
# create a new voters file
|
# create a new voters file
|
||||||
with open(votersFilename, 'w+') as votersFile:
|
try:
|
||||||
votersFile.write(replyJson['actor'] +
|
with open(voters_filename, 'w+') as voters_file:
|
||||||
votersFileSeparator +
|
voters_file.write(reply_json['actor'] +
|
||||||
foundAnswer + '\n')
|
voters_file_separator +
|
||||||
|
found_answer + '\n')
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to write voters file ' + voters_filename)
|
||||||
else:
|
else:
|
||||||
if replyJson['actor'] not in open(votersFilename).read():
|
if reply_json['actor'] not in open(voters_filename).read():
|
||||||
# append to the voters file
|
# append to the voters file
|
||||||
with open(votersFilename, 'a+') as votersFile:
|
try:
|
||||||
votersFile.write(replyJson['actor'] +
|
with open(voters_filename, 'a+') as voters_file:
|
||||||
votersFileSeparator +
|
voters_file.write(reply_json['actor'] +
|
||||||
foundAnswer + '\n')
|
voters_file_separator +
|
||||||
|
found_answer + '\n')
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to append to voters file ' + voters_filename)
|
||||||
else:
|
else:
|
||||||
# change an entry in the voters file
|
# change an entry in the voters file
|
||||||
with open(votersFilename, 'r') as votersFile:
|
with open(voters_filename, 'r') as voters_file:
|
||||||
lines = votersFile.readlines()
|
lines = voters_file.readlines()
|
||||||
newlines = []
|
newlines = []
|
||||||
saveVotersFile = False
|
save_voters_file = False
|
||||||
for voteLine in lines:
|
for vote_line in lines:
|
||||||
if voteLine.startswith(replyJson['actor'] +
|
if vote_line.startswith(reply_json['actor'] +
|
||||||
votersFileSeparator):
|
voters_file_separator):
|
||||||
newVoteLine = replyJson['actor'] + \
|
new_vote_line = reply_json['actor'] + \
|
||||||
votersFileSeparator + foundAnswer + '\n'
|
voters_file_separator + found_answer + '\n'
|
||||||
if voteLine == newVoteLine:
|
if vote_line == new_vote_line:
|
||||||
break
|
break
|
||||||
saveVotersFile = True
|
save_voters_file = True
|
||||||
newlines.append(newVoteLine)
|
newlines.append(new_vote_line)
|
||||||
else:
|
else:
|
||||||
newlines.append(voteLine)
|
newlines.append(vote_line)
|
||||||
if saveVotersFile:
|
if save_voters_file:
|
||||||
with open(votersFilename, 'w+') as votersFile:
|
try:
|
||||||
for voteLine in newlines:
|
with open(voters_filename, 'w+') as voters_file:
|
||||||
votersFile.write(voteLine)
|
for vote_line in newlines:
|
||||||
|
voters_file.write(vote_line)
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to write voters file2 ' +
|
||||||
|
voters_filename)
|
||||||
else:
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
# update the vote counts
|
# update the vote counts
|
||||||
questionTotalsChanged = False
|
question_totals_changed = False
|
||||||
for possibleAnswer in questionJson['object']['oneOf']:
|
for possible_answer in question_json['object']['oneOf']:
|
||||||
if not possibleAnswer.get('name'):
|
if not possible_answer.get('name'):
|
||||||
continue
|
continue
|
||||||
totalItems = 0
|
total_items = 0
|
||||||
with open(votersFilename, 'r') as votersFile:
|
with open(voters_filename, 'r') as voters_file:
|
||||||
lines = votersFile.readlines()
|
lines = voters_file.readlines()
|
||||||
for voteLine in lines:
|
for vote_line in lines:
|
||||||
if voteLine.endswith(votersFileSeparator +
|
if vote_line.endswith(voters_file_separator +
|
||||||
possibleAnswer['name'] + '\n'):
|
possible_answer['name'] + '\n'):
|
||||||
totalItems += 1
|
total_items += 1
|
||||||
if possibleAnswer['replies']['totalItems'] != totalItems:
|
if possible_answer['replies']['totalItems'] != total_items:
|
||||||
possibleAnswer['replies']['totalItems'] = totalItems
|
possible_answer['replies']['totalItems'] = total_items
|
||||||
questionTotalsChanged = True
|
question_totals_changed = True
|
||||||
if not questionTotalsChanged:
|
if not question_totals_changed:
|
||||||
return None, None
|
return None, None
|
||||||
# save the question with altered totals
|
# save the question with altered totals
|
||||||
save_json(questionJson, questionPostFilename)
|
save_json(question_json, question_post_filename)
|
||||||
return questionJson, questionPostFilename
|
return question_json, question_post_filename
|
||||||
|
|
||||||
|
|
||||||
def is_question(postObjectJson: {}) -> bool:
|
def is_question(postObjectJson: {}) -> bool:
|
||||||
|
|
Loading…
Reference in New Issue