schmeckels/models.py
Felix Breidenstein db555fa3b3 Fix autosort
2020-10-18 20:11:17 +02:00

49 lines
1.4 KiB
Python

#! /usr/bin/env python3
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Table
from sqlalchemy.orm import relationship, backref
Base = declarative_base()
association_table = Table(
"association",
Base.metadata,
Column("left_id", Integer, ForeignKey("Transaction.id")),
Column("right_id", Integer, ForeignKey("Tag.id")),
)
class Transaction(Base):
__tablename__ = "Transaction"
id = Column(Integer, primary_key=True)
name = Column(String)
date = Column(DateTime)
iban = Column(String)
amount = Column(Integer)
description = Column(String)
tags = relationship("Tag", secondary=association_table, back_populates="transactions")
def is_positive(self):
return self.amount > 0
def pretty_amount(self):
return format_amount(self.amount)
def get_date(self, format):
if format == "iso":
return self.date.strftime("%Y-%m-%d")
elif format == "de":
return self.date.strftime("%d.%m.%Y")
else:
return "UNKNOWN FORMAT"
class Tag(Base):
__tablename__ = "Tag"
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
description = Column(String)
transactions = relationship("Transaction", secondary=association_table, back_populates="tags")
def __repr__(self):
return f"<Tag {self.name}>"