import datetime import uuid from peewee import * DATABASE = 'budget.db' instance = SqliteDatabase(DATABASE, pragmas=[('foreign_keys', 'on')]) def create_tables(): """Helper function to create database tables. Should be called manually.""" with instance: instance.create_tables([User, TransactionCategory, Transaction]) def add_user(): # Make my user. instance.connect() User.create(username='ciphercules') instance.close() def add_default_categories(): """Helper function to add default categories. Should be called manually.""" categories = [ ("food", ["snacks", "fast_food", "groceries", "restaurant"]), ("clothing", []), ("event", ["event_food", "birthday", "movie_theater"]), ("finance", ["interest"]), ("hobby", ["gym", "game_development", "projects", "education"]), ("home_improvement", []), ("pet", ["dog_health", "dog_food"]), ("media", ["book", "music", "television", "video_game"]), ("health", ["medicine"]), ("transit", ["car_insurance", "car_registration", "gas", "parking", "taxi", "car_maintenance"]), ("utilities", ["electricity", "natural_gas", "laundry", "cell_phone", "trash", "water"]), ("rent", []), ] instance.connect() with instance.atomic(): for category in categories: parent, children = category parent_db = TransactionCategory.create(name=parent) for child in children: TransactionCategory.create(name=child, parent=parent_db) class BaseModel(Model): class Meta: database = instance class User(BaseModel): uuid = UUIDField(unique=True, primary_key=True, default=uuid.uuid4()) username = CharField(unique=True) class TransactionCategory(BaseModel): primary_key = AutoField(primary_key=True) name = CharField(unique=True) parent = ForeignKeyField('self', null=True, backref='children') class Transaction(BaseModel): # Metadata primary_key = AutoField(primary_key=True) created_date = DateTimeField(default=datetime.datetime.now) source_file=CharField(null=True) type = IntegerField() user = ForeignKeyField(User, backref='transactions') # Real data transaction_date = DateTimeField() description = CharField() amount = FloatField() subcategory = ForeignKeyField(TransactionCategory, backref='+', null=True) notes = CharField(null=True) if __name__ == "__main__": create_tables() add_user() add_default_categories()