83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
import json
|
|
|
|
def write_json(key, value):
|
|
path = 'db.json'
|
|
with open(path,'r') as jsonfile:
|
|
datafile = json.load(jsonfile)
|
|
datafile[key] = value
|
|
|
|
with open(path,'w') as jsonfile:
|
|
json.dump(datafile, jsonfile, indent=4, sort_keys=True)
|
|
|
|
def read_json():
|
|
path = 'db.json'
|
|
with open(path,'r') as jsonfile:
|
|
datafile = json.load(jsonfile)
|
|
return datafile
|
|
|
|
def read_json_value(key):
|
|
path = 'db.json'
|
|
with open(path, 'r') as jsonfile:
|
|
datafile = json.load(jsonfile)
|
|
|
|
if key in datafile:
|
|
return datafile[key]
|
|
else:
|
|
return None
|
|
|
|
def verify_equal(string1, string2):
|
|
return True if string1 == string2 else False
|
|
|
|
def validate_login(username, password):
|
|
if username == read_json_value("username"):
|
|
if password == read_json_value("password"):
|
|
return True
|
|
return False
|
|
return False
|
|
|
|
def get_children():
|
|
data = read_json()
|
|
children = []
|
|
|
|
if 'children' in data:
|
|
for child in data['children']:
|
|
children.append([child['id'], child['name'], child['birth']])
|
|
return children
|
|
|
|
def get_child_by_name(name):
|
|
data = read_json()
|
|
|
|
if 'children' in data:
|
|
for child in data['children']:
|
|
if child['name'] == name:
|
|
return [child['id'], child['name'], child['birth']]
|
|
return None
|
|
|
|
def remove_child_by_id(child_id):
|
|
data = read_json()
|
|
if 'children' in data:
|
|
updated_children = []
|
|
for child in data['children']:
|
|
if child['id'] != child_id:
|
|
updated_children.append(child)
|
|
|
|
data['children'] = updated_children
|
|
|
|
write_json('children', data['children'])
|
|
|
|
|
|
def add_child(child_id, name, birth):
|
|
data = read_json()
|
|
|
|
new_child = {
|
|
"id": child_id,
|
|
"name": name,
|
|
"birth": birth
|
|
}
|
|
|
|
if 'children' not in data:
|
|
data['children'] = []
|
|
|
|
data['children'].append(new_child)
|
|
|
|
write_json('children', data['children']) |