first commit
This commit is contained in:
97
database.py
Normal file
97
database.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from pocketbase import PocketBase
|
||||
from schemas import EntrySchema
|
||||
from datetime import datetime
|
||||
|
||||
load_dotenv()
|
||||
|
||||
pb = PocketBase(os.getenv('POCKETBASE_URL'))
|
||||
admin_data = pb.admins.auth_with_password(os.getenv('POCKETBASE_ADMIN_EMAIL'), os.getenv('POCKETBASE_ADMIN_PASSWORD'))
|
||||
|
||||
def convert_datetime_to_pocketbase(date_time_str):
|
||||
"""
|
||||
Convert datetime string from DD-MM-YYYY HH:MM format to PocketBase datetime format.
|
||||
PocketBase (Local) expects: YYYY-MM-DD HH:MM:SS
|
||||
"""
|
||||
if date_time_str == 'N/A' or not date_time_str:
|
||||
return None
|
||||
|
||||
try:
|
||||
print(f"[DEBUG] Converting datetime: '{date_time_str}' (type: {type(date_time_str)})")
|
||||
|
||||
# Parse the input format: "DD-MM-YYYY HH:MM" or "DD-MM-YYYY (HH:MM)"
|
||||
date_time_str = date_time_str.replace("(", "").replace(")", "").strip()
|
||||
|
||||
# Try with time first
|
||||
if " " in date_time_str:
|
||||
dt = datetime.strptime(date_time_str, "%d-%m-%Y %H:%M")
|
||||
else:
|
||||
# If only date is provided, set time to 00:00
|
||||
dt = datetime.strptime(date_time_str, "%d-%m-%Y")
|
||||
|
||||
# Convert to PocketBase local datetime format: YYYY-MM-DD HH:MM:SS
|
||||
pb_format = dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(f"[DEBUG] Converted to PocketBase format: '{pb_format}'")
|
||||
return pb_format
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Error converting datetime '{date_time_str}': {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
def upload_entry(data, entry_type='opportunity', url=None):
|
||||
"""
|
||||
Upload entry to the appropriate PocketBase collection.
|
||||
|
||||
Args:
|
||||
data: Dictionary containing the entry data
|
||||
entry_type: 'opportunity' or 'event'
|
||||
url: The source URL of the entry
|
||||
"""
|
||||
print(f"[DEBUG] Uploading {entry_type} entry. Data: {data}")
|
||||
data = dict(data)
|
||||
|
||||
# Add URL to data if provided
|
||||
if url:
|
||||
data['url'] = url
|
||||
print(f"[DEBUG] Added URL: {url}")
|
||||
|
||||
try:
|
||||
if entry_type == 'event':
|
||||
# Map 'date_time' from agent to 'datetime' for PocketBase
|
||||
if 'date_time' in data:
|
||||
original_dt = data['date_time']
|
||||
# Convert and map to PocketBase field name
|
||||
data['datetime'] = convert_datetime_to_pocketbase(data['date_time'])
|
||||
# Remove the original field since PocketBase expects 'datetime'
|
||||
del data['date_time']
|
||||
print(f"[DEBUG] Event datetime: '{original_dt}' -> '{data['datetime']}'")
|
||||
else:
|
||||
print(f"[WARNING] No 'date_time' field found in event data")
|
||||
|
||||
# Upload to events collection
|
||||
print(f"[DEBUG] Creating record in 'events' collection with data: {data}")
|
||||
result = pb.collection('events').create(data)
|
||||
print(f"[DEBUG] Successfully created record: {result}")
|
||||
return result
|
||||
else:
|
||||
# Opportunities - convert deadline to datetime format
|
||||
if 'deadline' in data:
|
||||
original_deadline = data['deadline']
|
||||
# Convert deadline to PocketBase datetime format
|
||||
data['deadline'] = convert_datetime_to_pocketbase(data['deadline'])
|
||||
print(f"[DEBUG] Opportunity deadline: '{original_deadline}' -> '{data['deadline']}'")
|
||||
else:
|
||||
print(f"[WARNING] No 'deadline' field found in opportunity data")
|
||||
|
||||
# Upload to opportunities collection
|
||||
print(f"[DEBUG] Creating record in 'opportunities' collection with data: {data}")
|
||||
result = pb.collection('opportunities').create(data)
|
||||
print(f"[DEBUG] Successfully created record: {result}")
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Failed to upload entry to PocketBase: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
raise
|
||||
Reference in New Issue
Block a user