#!/usr/bin/env python3
import sys, json
sys.path.append('lib/')
from helpers import *
qString = get_query_string()
if isinstance(qString, dict):
if ('feedback' in qString and 'page' in qString):
simplify data access
pageName = qString['page'][0]
feedback = qString['feedback'][0]
Validity data
validFeedback = ["thanks", "not_helpful", "no_opinion"]
if (feedback in validFeedback):
open file and insert data
with open("thanks.json", "r+") as file:
Load the json file
dataDict = json.load(file)
Create entry if it doesn't exist
if (pageName) not in dataDict:
dataDict[pageName] = {'thanks': 0, 'not_helpful':0, 'no_opinion':0}
Update the count
dataDict[pageName][feedback] += 1
write the data, truncating file first
file.seek(0)
file.truncate()
json.dump(dataDict, file, indent = 2)
Display success
show_header_ok()
print("# Feedback Recorded")
print(" ")
print("Thanks, your feedback has been recorded.")
print(f"=> {pageName} Return to page")
else:
show_header_ok()
print("Not valid parameters")
else:
show_header_ok()
print("No valid data, expected 'feedback' and 'page' params")
else:
show_header_ok()
print("No valid data passed")
Source