64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
#! /usr/bin/env python3
|
|
from hashlib import md5
|
|
|
|
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, Table
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import backref, relationship
|
|
|
|
from schmeckels import helper
|
|
|
|
Base = declarative_base()
|
|
|
|
association_table = Table(
|
|
"association",
|
|
Base.metadata,
|
|
Column("transaction_id", Integer, ForeignKey("Transaction.id")),
|
|
Column("tag_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 __repr__(self):
|
|
return f"Transaction {self.date}/{self.name}"
|
|
|
|
def is_positive(self):
|
|
return self.amount > 0
|
|
|
|
def pretty_amount(self):
|
|
return helper.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)
|
|
reporting = Column(Boolean, default=True)
|
|
description = Column(String)
|
|
transactions = relationship("Transaction", secondary=association_table, back_populates="tags")
|
|
|
|
def __repr__(self):
|
|
return f"<Tag {self.name}>"
|
|
|
|
def toggle_reporting(self):
|
|
self.reporting = not self.reporting
|
|
|
|
def color(self):
|
|
hash = md5(self.name.encode()).hexdigest()[-6:]
|
|
return f"#{hash}"
|