-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1064 lines (912 loc) · 37.8 KB
/
app.py
File metadata and controls
1064 lines (912 loc) · 37.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
# Force unbuffered output so logs appear in Docker/Dockge
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
from flask import Flask, render_template, request, jsonify, redirect, url_for, session, flash
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import threading
import time
import traceback
from price_fetcher import price_fetcher
import os
from werkzeug.utils import secure_filename
import uuid
from functools import wraps
# Disable __pycache__ creation (only works if set before Python starts - use run.sh)
os.environ['PYTHONDONTWRITEBYTECODE'] = '1'
app = Flask(__name__)
# Secret key for sessions (from environment variable or default for dev)
app.secret_key = os.environ.get('SECRET_KEY', 'dev-key-change-in-production')
# Authentication credentials from environment variables
ADMIN_USERNAME = os.environ.get('ADMIN_USERNAME', 'admin')
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', 'changeme')
# Save database in data directory (for Docker volume)
basedir = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(basedir, 'data')
os.makedirs(data_dir, exist_ok=True)
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(data_dir, "database.db")}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# File upload configuration
UPLOAD_FOLDER = os.path.join(basedir, 'static', 'images')
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# No max file size limit
# Create upload directories if they don't exist
for category in ['metals', 'coins', 'goldbacks']:
os.makedirs(os.path.join(UPLOAD_FOLDER, category), exist_ok=True)
db = SQLAlchemy(app)
# Login required decorator
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not session.get('logged_in'):
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# Login attempt tracking (global)
# tries: resets each lockout cycle, total_fails: never resets until success
login_state = {'tries': 0, 'total_fails': 0, 'lockout_until': None}
def get_lockout_seconds(total_fails):
"""2 min on 3rd total fail, +1 min for every fail after that"""
if total_fails < 3:
return 0
return 120 + (total_fails - 3) * 60
# Custom Jinja filter to remove trailing zeros
@app.template_filter('trim_zeros')
def trim_zeros(value):
"""Remove trailing zeros from decimal numbers (max 2 decimal places)"""
if value is None:
return '0'
# Format with max 2 decimals, then strip trailing zeros
formatted = f'{value:,.2f}'
# Remove trailing zeros after decimal point
if '.' in formatted:
formatted = formatted.rstrip('0').rstrip('.')
return formatted
@app.template_filter('format_percent')
def format_percent(value):
"""Format percentage: no decimals if >= 100%, otherwise show decimals"""
if value is None:
return '0'
# If 100% or more, show no decimals
if abs(value) >= 100:
return f'{int(value):,}'
# Otherwise use trim_zeros logic
formatted = f'{value:.2f}'
if '.' in formatted:
formatted = formatted.rstrip('0').rstrip('.')
return formatted
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def save_upload_file(file, category='metals'):
"""Save uploaded file and return the relative path"""
if file and allowed_file(file.filename):
# Generate unique filename
ext = file.filename.rsplit('.', 1)[1].lower()
filename = f"{uuid.uuid4().hex}.{ext}"
# Save to category folder
filepath = os.path.join(UPLOAD_FOLDER, category, filename)
file.save(filepath)
# Return relative path for database
return f"{category}/{filename}"
return None
# Background task for updating metal prices
def update_prices_periodically():
"""Background thread to update metal prices every 30 minutes"""
# Fetch prices immediately on first run
try:
start = time.time()
price_fetcher.fetch_all_prices()
except Exception as e:
print(f"[ERROR] Startup price check: {e}")
while True:
# Wait 30 minutes (1800 seconds)
time.sleep(1800)
try:
print("[INFO] Updating prices...")
price_fetcher.fetch_all_prices()
except Exception as e:
print(f"[ERROR] Updating prices: {e}")
# Start background thread
def start_price_updater():
# Start background thread (will fetch immediately, then every 30 min)
thread = threading.Thread(target=update_prices_periodically, daemon=True)
thread.start()
# Models
class Metal(db.Model):
id = db.Column(db.Integer, primary_key=True)
metal = db.Column(db.String(50), nullable=False)
form = db.Column(db.String(50), nullable=False)
count = db.Column(db.Integer, default=1)
weight_oz = db.Column(db.Float, nullable=False)
purity = db.Column(db.String(20), nullable=False)
year = db.Column(db.String(10))
total_cost = db.Column(db.Float, nullable=False)
current_value = db.Column(db.Float, default=0.0)
brand = db.Column(db.String(50))
notes = db.Column(db.Text)
image_filename = db.Column(db.String(255))
created_at = db.Column(db.DateTime, default=datetime.utcnow)
@property
def weight_display(self):
"""Convert decimal weight to fraction for common values"""
if not self.weight_oz:
return '-'
# Common fraction mappings
fractions = {
0.005: '1/200',
0.01: '1/100',
0.02: '1/50',
0.05: '1/20',
0.1: '1/10',
0.25: '1/4',
0.5: '1/2'
}
# Check if weight matches a common fraction (with small tolerance for float precision)
for decimal, fraction in fractions.items():
if abs(self.weight_oz - decimal) < 0.0001:
return fraction
# For whole numbers, display without decimals
if self.weight_oz == int(self.weight_oz):
return str(int(self.weight_oz))
# For other decimals, use trim_zeros equivalent
formatted = f'{self.weight_oz:.6f}'.rstrip('0').rstrip('.')
return formatted
@property
def calculated_value(self):
"""Calculate current value based on live metal prices and weight"""
if not self.weight_oz or not self.metal:
return 0.0
# Get current metal price
metal_type = self.metal.lower()
if metal_type not in ['gold', 'silver']:
return self.current_value # Fallback to stored value for other metals
price_per_oz = price_fetcher.get_price(metal_type)
if not price_per_oz:
return self.current_value # Fallback to stored value if price unavailable
# Calculate: price per oz × weight × count
total_oz = self.weight_oz * (self.count or 1)
return total_oz * price_per_oz
@property
def gain_loss(self):
return self.calculated_value - self.total_cost
@property
def gain_loss_percent(self):
if self.total_cost == 0 or self.total_cost is None:
return 0.0
return ((self.calculated_value - self.total_cost) / self.total_cost) * 100
class Coin(db.Model):
id = db.Column(db.Integer, primary_key=True)
material = db.Column(db.String(50)) # Gold, Silver, Copper, etc.
country = db.Column(db.String(100)) # Country of origin
year = db.Column(db.String(10)) # Year
weight = db.Column(db.String(50)) # Weight (any format)
denomination = db.Column(db.String(50)) # Face value/denomination
quantity = db.Column(db.Integer, default=1)
total_cost = db.Column(db.Float, default=0.0)
worth = db.Column(db.Float, default=0.0) # Current worth
worth_updated = db.Column(db.String(50)) # When worth was last updated
km = db.Column(db.String(50)) # KM catalog number (e.g., "131")
km_url = db.Column(db.String(500)) # URL to numista or other reference
notes = db.Column(db.Text)
image_filename = db.Column(db.String(255)) # Image filename
created_at = db.Column(db.DateTime, default=datetime.utcnow)
@property
def gain_loss(self):
return self.worth - self.total_cost
class Goldback(db.Model):
id = db.Column(db.Integer, primary_key=True)
state = db.Column(db.String(50)) # State
denomination = db.Column(db.Float) # Can be 1, 5, 10, 25, 50, 0.5, 0.25, etc.
year = db.Column(db.String(10)) # Year
count = db.Column(db.Integer, default=1)
alpha = db.Column(db.String(10), default='No') # Yes/No
serial = db.Column(db.String(100)) # Serial number
cost = db.Column(db.Float, default=0.0)
circulated = db.Column(db.String(10), default='No') # Yes/No
notes = db.Column(db.Text)
image_filename = db.Column(db.String(255)) # Image filename
created_at = db.Column(db.DateTime, default=datetime.utcnow)
@property
def gb_total(self):
"""Total goldbacks for this entry (denomination × count)"""
if self.denomination:
return self.denomination * self.count
return 0.0
@property
def denomination_display(self):
"""Convert decimal to fraction for display"""
if self.denomination == 0.5:
return '1/2'
elif self.denomination == 0.25:
return '1/4'
elif self.denomination and self.denomination == int(self.denomination):
return str(int(self.denomination))
else:
return str(self.denomination) if self.denomination else '-'
@property
def worth(self):
"""Calculate worth based on current gold price with 2x premium
Formula: (denomination / 1000) × gold_price × 2 × count
Each Goldback contains denomination/1000 oz of gold
Premium multiplier of 2x reflects market pricing
"""
if self.denomination:
# Get current gold price
gold_price = price_fetcher.get_price('gold')
if gold_price:
# Gold content in oz: denomination / 1000
# Market value: spot × 2 (100% premium)
gold_content_oz = self.denomination / 1000.0
return gold_content_oz * gold_price * 2.0 * self.count
return 0.0
return 0.0
@property
def gain_loss(self):
return self.worth - self.cost
# Routes
@app.route('/login', methods=['GET', 'POST'])
def login():
global login_state
# Check if currently locked out
remaining = 0
if login_state['lockout_until']:
if time.time() < login_state['lockout_until']:
remaining = int(login_state['lockout_until'] - time.time())
return render_template('login.html', lockout_remaining=remaining)
else:
# Lockout expired — reset tries only, keep total_fails
login_state['tries'] = 0
login_state['lockout_until'] = None
if request.method == 'POST':
# Recheck lockout (race condition guard)
if login_state['lockout_until'] and time.time() < login_state['lockout_until']:
remaining = int(login_state['lockout_until'] - time.time())
return render_template('login.html', lockout_remaining=remaining)
username = request.form.get('username')
password = request.form.get('password')
if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
# Success — reset everything
login_state.update({'tries': 0, 'total_fails': 0, 'lockout_until': None})
session['logged_in'] = True
session['username'] = username
return redirect(url_for('dashboard'))
else:
# Failed attempt
login_state['tries'] += 1
login_state['total_fails'] += 1
tries = login_state['tries']
total_fails = login_state['total_fails']
lockout_secs = get_lockout_seconds(total_fails)
lockout_until = (time.time() + lockout_secs) if lockout_secs > 0 else None
login_state['lockout_until'] = lockout_until
if lockout_until:
unlock_time = datetime.fromtimestamp(lockout_until).strftime('%H:%M:%S')
ip = request.headers.get('X-Forwarded-For', request.remote_addr).split(',')[0].strip()
print(f"[LOGIN] {ip} Too many login fails. Locked for {lockout_secs}s")
remaining = lockout_secs
return render_template('login.html', lockout_remaining=remaining)
else:
attempts_left = 3 - tries
ip = request.headers.get('X-Forwarded-For', request.remote_addr).split(',')[0].strip()
print(f"[LOGIN] {ip} Failed attempt {tries}/2")
flash(f'Invalid credentials. {attempts_left} attempt{"s" if attempts_left != 1 else ""} left.')
return render_template('login.html', lockout_remaining=0)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('login'))
@app.route('/')
@login_required
def index():
return redirect(url_for('dashboard'))
@app.route('/dashboard')
@login_required
def dashboard():
# Get all data
metals_list = Metal.query.all()
coins_list = Coin.query.all()
goldbacks_list = Goldback.query.all()
# Calculate individual category stats
metals_cost = sum(m.total_cost for m in metals_list)
metals_value = sum(m.calculated_value for m in metals_list)
metals_gain = metals_value - metals_cost
coins_cost = sum(c.total_cost for c in coins_list)
coins_value = sum(c.worth for c in coins_list)
coins_gain = coins_value - coins_cost
goldbacks_cost = sum(g.cost for g in goldbacks_list)
goldbacks_value = sum(g.worth for g in goldbacks_list)
goldbacks_gain = goldbacks_value - goldbacks_cost
goldbacks_total_gb = sum(g.gb_total for g in goldbacks_list)
# Calculate overall totals
total_cost = metals_cost + coins_cost + goldbacks_cost
total_value = metals_value + coins_value + goldbacks_value
total_gain = total_value - total_cost
total_gain_percent = ((total_gain / total_cost) * 100) if total_cost > 0 else 0
# Overall stats
overall_stats = {
'cost_basis': total_cost,
'current_value': total_value,
'gain_loss': total_gain,
'gain_loss_percent': total_gain_percent
}
# Category breakdowns (as list for sorting)
categories = [
{
'key': 'coins',
'name': 'Coins',
'count': len(coins_list),
'cost': coins_cost,
'value': coins_value,
'gain': coins_gain,
'gain_percent': ((coins_gain / coins_cost) * 100) if coins_cost > 0 else 0
},
{
'key': 'goldbacks',
'name': 'Goldbacks',
'count': len(goldbacks_list),
'cost': goldbacks_cost,
'value': goldbacks_value,
'gain': goldbacks_gain,
'gain_percent': ((goldbacks_gain / goldbacks_cost) * 100) if goldbacks_cost > 0 else 0,
'gb_total': goldbacks_total_gb,
'total_oz': sum((g.denomination / 1000.0) * g.count for g in goldbacks_list if g.denomination)
},
{
'key': 'metals',
'name': 'Metals',
'count': len(metals_list),
'cost': metals_cost,
'value': metals_value,
'gain': metals_gain,
'gain_percent': ((metals_gain / metals_cost) * 100) if metals_cost > 0 else 0,
'total_oz': sum((m.weight_oz or 0) * (m.count or 0) for m in metals_list),
'gold_oz': sum((m.weight_oz or 0) * (m.count or 0) for m in metals_list if m.metal and m.metal.lower() == 'gold'),
'silver_oz': sum((m.weight_oz or 0) * (m.count or 0) for m in metals_list if m.metal and m.metal.lower() == 'silver')
}
]
# Sort alphabetically by name
categories.sort(key=lambda x: x['name'])
# Calculate Gold vs Silver breakdown
gold_value = 0.0
gold_oz = 0.0
silver_value = 0.0
silver_oz = 0.0
# Metals-only tracking
gold_value_metals_only = 0.0
gold_oz_metals_only = 0.0
silver_value_metals_only = 0.0
silver_oz_metals_only = 0.0
# From Metals table
for metal in metals_list:
if metal.metal and metal.metal.lower() == 'gold':
gold_value += metal.calculated_value
gold_oz += (metal.weight_oz * metal.count) if metal.weight_oz else 0
gold_value_metals_only += metal.calculated_value
gold_oz_metals_only += (metal.weight_oz * metal.count) if metal.weight_oz else 0
elif metal.metal and metal.metal.lower() == 'silver':
silver_value += metal.calculated_value
silver_oz += (metal.weight_oz * metal.count) if metal.weight_oz else 0
silver_value_metals_only += metal.calculated_value
silver_oz_metals_only += (metal.weight_oz * metal.count) if metal.weight_oz else 0
# From Coins table (if material is specified)
for coin in coins_list:
if coin.material and coin.material.lower() == 'gold':
gold_value += coin.worth
# Coins don't have standardized oz weight, so we skip oz calculation
elif coin.material and coin.material.lower() == 'silver':
silver_value += coin.worth
# From Goldbacks (all gold-based)
for goldback in goldbacks_list:
gold_value += goldback.worth
# Each Goldback contains denomination/1000 oz of gold
if goldback.denomination:
gold_oz += (goldback.denomination / 1000.0) * goldback.count
metal_breakdown = {
'gold_value': gold_value,
'gold_oz': gold_oz,
'silver_value': silver_value,
'silver_oz': silver_oz,
'gold_value_metals_only': gold_value_metals_only,
'gold_oz_metals_only': gold_oz_metals_only,
'silver_value_metals_only': silver_value_metals_only,
'silver_oz_metals_only': silver_oz_metals_only
}
# Calculate form breakdown for metals only
form_breakdown = {}
for metal in metals_list:
form = metal.form if metal.form else 'Other'
if form not in form_breakdown:
form_breakdown[form] = 0
form_breakdown[form] += (metal.weight_oz or 0) * (metal.count or 1)
# Get top worth items for each category
top_coins = sorted(coins_list, key=lambda x: x.worth, reverse=True)[:10]
top_goldbacks = sorted(goldbacks_list, key=lambda x: x.worth, reverse=True)[:10]
top_metals = sorted(metals_list, key=lambda x: x.calculated_value, reverse=True)[:10]
return render_template('dashboard.html',
active_page='dashboard',
overall_stats=overall_stats,
categories=categories,
metal_breakdown=metal_breakdown,
form_breakdown=form_breakdown,
top_coins=top_coins,
top_goldbacks=top_goldbacks,
top_metals=top_metals)
@app.route('/coins')
@login_required
def coins():
coins_list = Coin.query.all()
# Sort by country (A-Z) then by year
coins_list.sort(key=lambda c: (c.country or '', c.year or ''))
# Calculate stats
total_cost = sum(c.total_cost for c in coins_list)
total_worth = sum(c.worth for c in coins_list)
total_gain_loss = total_worth - total_cost
total_gain_loss_percent = ((total_gain_loss / total_cost) * 100) if total_cost > 0 else 0
stats = {
'cost_basis': total_cost,
'current_value': total_worth,
'gain_loss': total_gain_loss,
'gain_loss_percent': total_gain_loss_percent
}
return render_template('coins.html', active_page='coins', coins=coins_list, stats=stats)
@app.route('/goldbacks')
@login_required
def goldbacks():
goldbacks_list = Goldback.query.all()
# Sort by state (A-Z) then by denomination (ascending)
goldbacks_list.sort(key=lambda g: (g.state or '', g.denomination or 0))
# Calculate stats
total_cost = sum(g.cost for g in goldbacks_list)
total_worth = sum(g.worth for g in goldbacks_list)
total_gb = sum(g.gb_total for g in goldbacks_list) # Total goldbacks (denomination × count)
total_gain_loss = total_worth - total_cost
total_gain_loss_percent = ((total_gain_loss / total_cost) * 100) if total_cost > 0 else 0
# Calculate GB rate (current value per 1 goldback)
# Using gold price: (1/1000 oz) × gold_price × 2
gold_price = price_fetcher.get_price('gold')
gb_rate = (gold_price / 1000.0 * 2.0) if gold_price else 0.0
stats = {
'cost_basis': total_cost,
'gb_total': total_gb,
'gb_rate': gb_rate,
'current_value': total_worth,
'gain_loss': total_gain_loss,
'gain_loss_percent': total_gain_loss_percent
}
return render_template('goldbacks.html', active_page='goldbacks', goldbacks=goldbacks_list, stats=stats)
@app.route('/metals')
@login_required
def metals():
metals_list = Metal.query.all()
# Custom sort: Metal (Gold first, then Silver), then Form (A-Z), then Weight (ascending), then Quantity (descending)
def metal_sort_key(m):
# Metal priority: Gold=0, Silver=1, others=2
metal_priority = 0 if m.metal == 'Gold' else (1 if m.metal == 'Silver' else 2)
# Negate count for descending order (high to low)
return (metal_priority, m.form or '', m.weight_oz or 0, -(m.count or 0))
metals_list.sort(key=metal_sort_key)
# Calculate stats
total_cost = sum(m.total_cost for m in metals_list)
total_current_value = sum(m.calculated_value for m in metals_list)
total_gain_loss = total_current_value - total_cost
total_gain_loss_percent = ((total_gain_loss / total_cost) * 100) if total_cost > 0 else 0
stats = {
'cost_basis': total_cost,
'current_value': total_current_value,
'gain_loss': total_gain_loss,
'gain_loss_percent': total_gain_loss_percent
}
return render_template('metals.html',
active_page='metals',
metals=metals_list,
stats=stats)
# API endpoints for CRUD operations
@app.route('/api/metals', methods=['GET'])
@login_required
def get_metals():
metals_list = Metal.query.all()
return jsonify([{
'id': m.id,
'metal': m.metal,
'form': m.form,
'count': m.count,
'weight_oz': m.weight_oz,
'purity': m.purity,
'year': m.year,
'total_cost': m.total_cost,
'current_value': m.calculated_value,
'gain_loss': m.gain_loss,
'brand': m.brand,
'notes': m.notes,
'image_filename': m.image_filename
} for m in metals_list])
@app.route('/api/metals', methods=['POST'])
@login_required
def add_metal():
try:
# Handle file upload if present
image_filename = None
if 'image' in request.files:
file = request.files['image']
if file.filename:
image_filename = save_upload_file(file, 'metals')
# Get form data
if request.content_type and 'multipart/form-data' in request.content_type:
data = request.form.to_dict()
data['count'] = int(data.get('count', 1))
data['weight_oz'] = parse_weight(data.get('weight_oz', '1'))
data['total_cost'] = float(data['total_cost'])
data['current_value'] = float(data.get('current_value', 0))
else:
data = request.json
if 'weight_oz' in data:
data['weight_oz'] = parse_weight(data['weight_oz'])
new_metal = Metal(
metal=data['metal'],
form=data['form'],
count=data.get('count', 1),
weight_oz=data['weight_oz'],
purity=data['purity'],
year=data.get('year', ''),
total_cost=data['total_cost'],
current_value=data.get('current_value', 0),
brand=data.get('brand', ''),
notes=data.get('notes', ''),
image_filename=image_filename
)
db.session.add(new_metal)
db.session.commit()
return jsonify({'success': True, 'id': new_metal.id}), 201
except Exception as e:
print(f"[ERROR] Adding metal: {e}")
db.session.rollback()
return jsonify({'success': False, 'error': str(e)}), 400
@login_required
@app.route('/api/metals/<int:id>', methods=['PUT'])
def update_metal(id):
metal = Metal.query.get_or_404(id)
data = request.json
metal.metal = data.get('metal', metal.metal)
metal.form = data.get('form', metal.form)
metal.count = data.get('count', metal.count)
if 'weight_oz' in data:
metal.weight_oz = parse_weight(data['weight_oz'])
metal.purity = data.get('purity', metal.purity)
metal.year = data.get('year', metal.year)
metal.total_cost = data.get('total_cost', metal.total_cost)
# current_value is now calculated dynamically, not stored
metal.brand = data.get('brand', metal.brand)
metal.notes = data.get('notes', metal.notes)
db.session.commit()
return jsonify({'success': True})
@login_required
@app.route('/api/metals/<int:id>', methods=['DELETE'])
def delete_metal(id):
metal = Metal.query.get_or_404(id)
# Delete associated image file if it exists
if metal.image_filename:
image_path = os.path.join(UPLOAD_FOLDER, metal.image_filename)
if os.path.exists(image_path):
os.remove(image_path)
db.session.delete(metal)
db.session.commit()
return jsonify({'success': True})
@login_required
@app.route('/api/metals/<int:id>/image', methods=['POST'])
def upload_metal_image(id):
try:
metal = Metal.query.get_or_404(id)
if 'image' not in request.files:
return jsonify({'success': False, 'error': 'No image file'}), 400
file = request.files['image']
if not file.filename:
return jsonify({'success': False, 'error': 'No file selected'}), 400
# Delete old image if exists
if metal.image_filename:
old_path = os.path.join(UPLOAD_FOLDER, metal.image_filename)
if os.path.exists(old_path):
os.remove(old_path)
# Save new image
image_filename = save_upload_file(file, 'metals')
if not image_filename:
return jsonify({'success': False, 'error': 'Failed to save image'}), 400
metal.image_filename = image_filename
db.session.commit()
return jsonify({'success': True})
except Exception as e:
print(f"[ERROR] Uploading image: {e}")
db.session.rollback()
return jsonify({'success': False, 'error': str(e)}), 400
# Goldbacks API Endpoints
@login_required
@app.route('/api/goldbacks', methods=['GET'])
def get_goldbacks():
goldbacks_list = Goldback.query.all()
return jsonify([{
'id': g.id,
'state': g.state,
'denomination': g.denomination,
'year': g.year,
'count': g.count,
'alpha': g.alpha,
'serial': g.serial,
'cost': g.cost,
'worth': g.worth,
'gain_loss': g.gain_loss,
'circulated': g.circulated,
'notes': g.notes,
'image_filename': g.image_filename
} for g in goldbacks_list])
def parse_weight(value):
"""Convert fraction strings like '1/200', '1/100' to decimals for weight"""
if not value or value == '':
return 1.0
value_str = str(value).strip()
# Check for fractions
if '/' in value_str:
try:
parts = value_str.split('/')
return float(parts[0]) / float(parts[1])
except:
return float(value_str)
else:
try:
return float(value_str)
except:
return 1.0
def parse_denomination(value):
"""Convert fraction strings like '1/2', '1/4' to decimals"""
if not value or value == '':
return 1.0
value_str = str(value).strip()
# Check for fractions
if '/' in value_str:
try:
parts = value_str.split('/')
return float(parts[0]) / float(parts[1])
except:
return float(value_str)
else:
try:
return float(value_str)
except:
return 1.0
@login_required
@app.route('/api/goldbacks', methods=['POST'])
def add_goldback():
try:
# Handle file upload if present
image_filename = None
if 'image' in request.files:
file = request.files['image']
if file.filename:
image_filename = save_upload_file(file, 'goldbacks')
# Get form data
if request.content_type and 'multipart/form-data' in request.content_type:
data = request.form.to_dict()
data['count'] = int(data.get('count', 1))
data['denomination'] = parse_denomination(data.get('denomination', '1'))
data['cost'] = float(data.get('cost', 0))
else:
data = request.json
if 'denomination' in data:
data['denomination'] = parse_denomination(data['denomination'])
new_goldback = Goldback(
state=data.get('state', ''),
denomination=data.get('denomination', 1),
year=data.get('year', ''),
count=data.get('count', 1),
alpha=data.get('alpha', 'No'),
serial=data.get('serial', ''),
cost=data.get('cost', 0),
circulated=data.get('circulated', 'No'),
notes=data.get('notes', ''),
image_filename=image_filename
)
db.session.add(new_goldback)
db.session.commit()
return jsonify({'success': True, 'id': new_goldback.id}), 201
except Exception as e:
print(f"[ERROR] Adding goldback: {e}")
db.session.rollback()
return jsonify({'success': False, 'error': str(e)}), 400
@login_required
@app.route('/api/goldbacks/<int:id>', methods=['PUT'])
def update_goldback(id):
goldback = Goldback.query.get_or_404(id)
data = request.json
goldback.state = data.get('state', goldback.state)
if 'denomination' in data:
goldback.denomination = parse_denomination(data['denomination'])
goldback.year = data.get('year', goldback.year)
goldback.count = data.get('count', goldback.count)
goldback.alpha = data.get('alpha', goldback.alpha)
goldback.serial = data.get('serial', goldback.serial)
goldback.cost = data.get('cost', goldback.cost)
goldback.circulated = data.get('circulated', goldback.circulated)
goldback.notes = data.get('notes', goldback.notes)
db.session.commit()
return jsonify({'success': True})
@login_required
@app.route('/api/goldbacks/<int:id>', methods=['DELETE'])
def delete_goldback(id):
goldback = Goldback.query.get_or_404(id)
# Delete associated image file if it exists
if goldback.image_filename:
image_path = os.path.join(UPLOAD_FOLDER, goldback.image_filename)
if os.path.exists(image_path):
os.remove(image_path)
db.session.delete(goldback)
db.session.commit()
return jsonify({'success': True})
@login_required
@app.route('/api/goldbacks/<int:id>/image', methods=['POST'])
def upload_goldback_image(id):
"""Upload or replace image for existing goldback entry"""
try:
goldback = Goldback.query.get_or_404(id)
if 'image' not in request.files:
return jsonify({'success': False, 'error': 'No image file'}), 400
file = request.files['image']
if not file.filename:
return jsonify({'success': False, 'error': 'No file selected'}), 400
# Delete old image if exists
if goldback.image_filename:
old_path = os.path.join(UPLOAD_FOLDER, goldback.image_filename)
if os.path.exists(old_path):
os.remove(old_path)
# Save new image
image_filename = save_upload_file(file, 'goldbacks')
if not image_filename:
return jsonify({'success': False, 'error': 'Failed to save image'}), 400
goldback.image_filename = image_filename
db.session.commit()
return jsonify({'success': True})
except Exception as e:
print(f"[ERROR] Uploading image: {e}")
db.session.rollback()
return jsonify({'success': False, 'error': str(e)}), 400
# Metal prices API endpoint
@login_required
@app.route('/api/prices', methods=['GET'])
def get_prices():
"""Get current metal prices"""
force_refresh = request.args.get('refresh', 'false').lower() == 'true'
if force_refresh:
thread = threading.Thread(target=price_fetcher.fetch_all_prices)
thread.start()
return jsonify(price_fetcher.get_prices())
# Coins API Endpoints
@login_required
@app.route('/api/coins', methods=['GET'])
def get_coins():
coins_list = Coin.query.all()
return jsonify([{
'id': c.id,
'material': c.material,
'country': c.country,
'year': c.year,
'weight': c.weight,
'denomination': c.denomination,
'quantity': c.quantity,
'total_cost': c.total_cost,
'worth': c.worth,
'gain_loss': c.gain_loss,
'worth_updated': c.worth_updated,
'km': c.km,
'km_url': c.km_url,
'notes': c.notes,
'image_filename': c.image_filename
} for c in coins_list])
@login_required
@app.route('/api/coins', methods=['POST'])
def add_coin():
try:
# Handle file upload if present
image_filename = None
if 'image' in request.files:
file = request.files['image']
if file.filename:
image_filename = save_upload_file(file, 'coins')
# Get form data
if request.content_type and 'multipart/form-data' in request.content_type:
data = request.form.to_dict()
data['quantity'] = int(data.get('quantity', 1))
data['total_cost'] = float(data.get('total_cost', 0))
data['worth'] = float(data.get('worth', 0))
else:
data = request.json
new_coin = Coin(
material=data.get('material', ''),
country=data.get('country', ''),
year=data.get('year', ''),
weight=data.get('weight', ''),
denomination=data.get('denomination', ''),
quantity=data.get('quantity', 1),
total_cost=data.get('total_cost', 0),
worth=data.get('worth', 0),
worth_updated=data.get('worth_updated', ''),
km=data.get('km', ''),
km_url=data.get('km_url', ''),
notes=data.get('notes', ''),
image_filename=image_filename
)
db.session.add(new_coin)
db.session.commit()
return jsonify({'success': True, 'id': new_coin.id}), 201
except Exception as e:
print(f"[ERROR] Adding coin: {e}")
import traceback
traceback.print_exc()
db.session.rollback()
return jsonify({'success': False, 'error': str(e)}), 400
@login_required
@app.route('/api/coins/<int:id>', methods=['PUT'])
def update_coin(id):
coin = Coin.query.get_or_404(id)
data = request.json
coin.material = data.get('material', coin.material)
coin.country = data.get('country', coin.country)
coin.year = data.get('year', coin.year)
coin.weight = data.get('weight', coin.weight)
coin.denomination = data.get('denomination', coin.denomination)
coin.quantity = data.get('quantity', coin.quantity)
coin.total_cost = data.get('total_cost', coin.total_cost)
coin.worth = data.get('worth', coin.worth)
coin.worth_updated = data.get('worth_updated', coin.worth_updated)
# Handle KM fields - allow empty strings to clear the values
if 'km' in data:
coin.km = data['km'] if data['km'] else None
if 'km_url' in data:
coin.km_url = data['km_url'] if data['km_url'] else None