Files
battery-tracker-app/models.py
T
iterminate 604d7bb699 Add optional battery metadata fields
New nullable columns on Battery: size, chemistry, capacity_mah,
tested_capacity_mah, tested_date, charge_cycles, purchase_date.

Battery detail page shows all populated fields and a full edit form
with select dropdowns for size and chemistry (with Other fallback).
Capacity health % shown in green/orange/red when both nominal and
tested capacity are set. Dashboard gains a Size column.
2026-04-12 14:57:22 -05:00

62 lines
2.2 KiB
Python

from sqlalchemy import Column, Integer, String, Text, ForeignKey
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
class Device(Base):
__tablename__ = "device"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100), nullable=False, unique=True)
battery_slots = Column(Integer, nullable=False, default=1)
notes = Column(Text, nullable=True)
batteries = relationship("Battery", back_populates="device")
def installed_count(self):
return sum(1 for b in self.batteries if b.status == "installed")
def installed_brands(self):
return set(b.brand for b in self.batteries if b.status == "installed")
def has_mixed_brands(self):
return len(self.installed_brands()) > 1
def __repr__(self):
return f"<Device {self.name}>"
class Battery(Base):
__tablename__ = "battery"
id = Column(Integer, primary_key=True, autoincrement=True)
label = Column(String(50), nullable=False)
brand = Column(String(100), nullable=False)
status = Column(String(20), nullable=False, default="available")
device_id = Column(Integer, ForeignKey("device.id", ondelete="SET NULL"), nullable=True)
notes = Column(Text, nullable=True)
# Optional metadata
size = Column(String(20), nullable=True) # AA, AAA, 18650, CR2032 …
chemistry = Column(String(20), nullable=True) # NiMH, Alkaline, Li-ion …
capacity_mah = Column(Integer, nullable=True) # nominal capacity in mAh
tested_capacity_mah = Column(Integer, nullable=True) # last measured capacity in mAh
tested_date = Column(String(10), nullable=True) # YYYY-MM-DD of last test
charge_cycles = Column(Integer, nullable=True) # number of charge cycles
purchase_date = Column(String(10), nullable=True) # YYYY-MM-DD when purchased
device = relationship("Device", back_populates="batteries")
def is_available(self):
return self.status == "available"
def is_retired(self):
return self.status == "retired"
def is_installed(self):
return self.status == "installed"
def __repr__(self):
return f"<Battery {self.label}>"