-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
210 lines (177 loc) · 6.8 KB
/
database.py
File metadata and controls
210 lines (177 loc) · 6.8 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import psycopg2
import psycopg2.extras
import json
from datetime import datetime
import os
def get_db_connection():
"""Get database connection using Railway environment variables"""
# Railway provides these automatically when you add PostgreSQL
database_url = os.getenv('DATABASE_URL')
if database_url:
# Use DATABASE_URL if available (Railway format)
return psycopg2.connect(database_url, sslmode='require')
else:
# Fallback to individual environment variables
return psycopg2.connect(
host=os.getenv('PGHOST', 'localhost'),
port=os.getenv('PGPORT', 5432),
database=os.getenv('PGDATABASE', 'donations'),
user=os.getenv('PGUSER', 'postgres'),
password=os.getenv('PGPASSWORD', ''),
sslmode='require'
)
def init_database():
"""Initialize the donations database"""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS donations (
id SERIAL PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(255),
message TEXT,
amount DECIMAL(10,2) NOT NULL,
paypal_transaction_id VARCHAR(255) UNIQUE,
paypal_payer_email VARCHAR(255),
donation_type VARCHAR(50) DEFAULT 'oneTime',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Create index for better performance
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_donations_created_at
ON donations(created_at DESC)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_donations_transaction_id
ON donations(paypal_transaction_id)
''')
conn.commit()
cursor.close()
conn.close()
print("PostgreSQL database initialized successfully")
except Exception as e:
print(f"Error initializing database: {str(e)}")
raise
def save_donation(donation_data):
"""Save a donation to the database"""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO donations
(first_name, last_name, email, message, amount, paypal_transaction_id, paypal_payer_email, donation_type)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id
''', (
donation_data.get('first_name', ''),
donation_data.get('last_name', ''),
donation_data.get('email', ''),
donation_data.get('message', ''),
float(donation_data.get('amount', 0)),
donation_data.get('transaction_id', ''),
donation_data.get('payer_email', ''),
donation_data.get('donation_type', 'oneTime')
))
donation_id = cursor.fetchone()[0]
conn.commit()
cursor.close()
conn.close()
print(f"Donation saved successfully with ID: {donation_id}")
return donation_id
except psycopg2.IntegrityError:
print(f"Donation with transaction ID {donation_data.get('transaction_id')} already exists")
if conn:
conn.rollback()
cursor.close()
conn.close()
return None
except Exception as e:
print(f"Error saving donation: {str(e)}")
if conn:
conn.rollback()
cursor.close()
conn.close()
return None
def get_all_donations():
"""Get all donations from the database"""
try:
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute('''
SELECT id, first_name, last_name, email, message, amount,
paypal_transaction_id, paypal_payer_email, donation_type, created_at
FROM donations
ORDER BY created_at DESC
''')
donations = cursor.fetchall()
cursor.close()
conn.close()
# Convert to list of dictionaries for compatibility
donation_list = []
for donation in donations:
donation_dict = dict(donation)
# Convert datetime to string for JSON serialization
if donation_dict['created_at']:
donation_dict['created_at'] = donation_dict['created_at'].isoformat()
donation_list.append(donation_dict)
return donation_list
except Exception as e:
print(f"Error retrieving donations: {str(e)}")
return []
def get_donation_stats():
"""Get basic donation statistics"""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Total donations and amount
cursor.execute('SELECT COUNT(*), COALESCE(SUM(amount), 0) FROM donations')
count, total = cursor.fetchone()
# Average donation
cursor.execute('SELECT COALESCE(AVG(amount), 0) FROM donations WHERE amount > 0')
average = cursor.fetchone()[0]
cursor.close()
conn.close()
return {
'total_donations': count or 0,
'total_amount': float(total or 0),
'average_donation': round(float(average or 0), 2)
}
except Exception as e:
print(f"Error getting donation stats: {str(e)}")
return {'total_donations': 0, 'total_amount': 0, 'average_donation': 0}
def backup_donations_to_json():
"""Create a JSON backup of all donations"""
try:
donations = get_all_donations()
stats = get_donation_stats()
backup_data = {
'backup_date': datetime.now().isoformat(),
'stats': stats,
'donations': donations
}
backup_filename = f'donations_backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}.json'
with open(backup_filename, 'w') as f:
json.dump(backup_data, f, indent=2, default=str)
print(f"Backup created: {backup_filename}")
return backup_filename
except Exception as e:
print(f"Error creating backup: {str(e)}")
return None
def test_database_connection():
"""Test database connection and return status"""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT 1')
result = cursor.fetchone()
cursor.close()
conn.close()
return True, "Database connection successful"
except Exception as e:
return False, f"Database connection failed: {str(e)}"
# Only initialize database when explicitly called
if __name__ == "__main__":
init_database()