Files
ide-cadastro/libs.py
ovosimpatico 23362988bc Add functionality to remove child and associated picture by ID.
Handle file not found exception when retrieving child picture.
Update interface language to Portuguese.
Fix minor layout issues and button text.
2024-02-01 16:55:02 -03:00

104 lines
2.4 KiB
Python

import json
from PIL import Image
import io
import os
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'])
def get_pic(id):
try:
image = Image.open(f"assets/{id}.jpg")
except FileNotFoundError:
image = Image.open(f"assets/0.jpg")
image.thumbnail((200, 200))
bio = io.BytesIO()
image.save(bio, format="PNG")
return bio.getvalue()
def removepicbyid(id):
path = f"assets/{id}.jpg"
if os.path.exists(path):
os.remove(path)
return True
else:
return False