File size: 1,669 Bytes
848090a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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)
|