wrAIte / data_storage.py
suhamemon1
initial commit
848090a
raw
history blame
1.67 kB
import uuid
import os
import json
def store_user_data(user_id, storage_type, storage_data):
# Set the base directory relative to the current script location
base_dir = "./user_data/" # Changed to a relative path
user_dir = base_dir + str(user_id)
os.makedirs(user_dir, exist_ok=True)
file_path = os.path.join(user_dir, f"{storage_type}_{user_id}.json")
# Check if user file exists and load existing data
if os.path.exists(file_path):
try:
with open(file_path, 'r') as file:
existing_data = json.load(file)
# Update existing data with new data
existing_data.update(storage_data)
data = existing_data
except json.JSONDecodeError:
# If JSON decoding fails, overwrite the corrupted file with new data
pass
# Write the data to the file
with open(file_path, 'w') as file:
json.dump(storage_data, file, indent=4)
def store_user_questionnaire(user_id, storage_data):
# Set the base directory relative to the current script location
# Check if user file exists and load existing data
if os.path.exists(file_path):
try:
with open(file_path, 'r') as file:
existing_data = json.load(file)
# Update existing data with new data
existing_data.update(storage_data)
data = existing_data
except json.JSONDecodeError:
# If JSON decoding fails, overwrite the corrupted file with new data
pass
# Write the data to the file
with open(file_path, 'w') as file:
json.dump(storage_data, file, indent=4)