from app import db
from app.mod_auth.model import User

class Category(db.Document):
    # The name of the category.
    name = db.StringField(required = True)

class Entry(db.Document):
    # The amount of the entry.
    amount = db.DecimalField(precision = 2, required = True)

    # A short description for the entry.
    description = db.StringField(required = True)

    # The owner of the entry.
    # Should the owner be deleted, we also want to delete all of his entries.
    owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)

    # The category of this entry.
    category = db.ReferenceField(Category, required = True)

class CategoryBudget(db.Document):
    # The amount of the budget.
    amount = db.DecimalField(precision = 2, required = True)

    # The category.
    category = db.ReferenceField(Category, required = True)

def sumEntries():
    return sum([entry.amount for entry in Entry.objects if entry.amount > 0])

from app import db
from app.mod_auth.model import User

class Category(db.Document):
    # The name of the category.
    name = db.StringField(required = True)

class Income(db.Document):
    # The amount of the entry.
    amount = db.DecimalField(precision = 2, required = True)

    # A short description for the entry.
    description = db.StringField(required = True)

    # The owner of the entry.
    # Should the owner be deleted, we also want to delete all of his entries.
    owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)

    # The category of this entry.
    category = db.ReferenceField(Category, required = True)

class Expense(db.Document):
    # The amount of the entry.
    amount = db.DecimalField(precision = 2, required = True)

    # A short description for the expense.
    description = db.StringField(required = True)

    # The owner of the expense.
    # Should the owner be deleted, we also want to delete all of his entries.
    owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)

    # The category of this entry.
    category = db.ReferenceField(Category, required = True)

class CategoryBudget(db.Document):
    # The amount of the budget.
    amount = db.DecimalField(precision = 2, required = True)

    # The category.
    category = db.ReferenceField(Category, required = True)

def sumEntries():
    return sum([entry.amount for entry in Entry.objects if entry.amount > 0])

Split Entry into Income and Expense schemes
Split Entry into Income and Expense schemes

Splitting the Entry schema into two seperate schemes allows
us to use different collections to store them, which in turn makes
our work easier later on.

  from app import db
  from app.mod_auth.model import User
  
  class Category(db.Document):
      # The name of the category.
      name = db.StringField(required = True)
  
- class Entry(db.Document):
+ class Income(db.Document):
      # The amount of the entry.
      amount = db.DecimalField(precision = 2, required = True)
  
      # A short description for the entry.
      description = db.StringField(required = True)
  
      # The owner of the entry.
+     # Should the owner be deleted, we also want to delete all of his entries.
+     owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)
+ 
+     # The category of this entry.
+     category = db.ReferenceField(Category, required = True)
+ 
+ class Expense(db.Document):
+     # The amount of the entry.
+     amount = db.DecimalField(precision = 2, required = True)
+ 
+     # A short description for the expense.
+     description = db.StringField(required = True)
+ 
+     # The owner of the expense.
      # Should the owner be deleted, we also want to delete all of his entries.
      owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)
  
      # The category of this entry.
      category = db.ReferenceField(Category, required = True)
  
  class CategoryBudget(db.Document):
      # The amount of the budget.
      amount = db.DecimalField(precision = 2, required = True)
  
      # The category.
      category = db.ReferenceField(Category, required = True)
  
  def sumEntries():
      return sum([entry.amount for entry in Entry.objects if entry.amount > 0])
  
Split Entry into Income and Expense schemes
## Code Before:
from app import db
from app.mod_auth.model import User

class Category(db.Document):
    # The name of the category.
    name = db.StringField(required = True)

class Entry(db.Document):
    # The amount of the entry.
    amount = db.DecimalField(precision = 2, required = True)

    # A short description for the entry.
    description = db.StringField(required = True)

    # The owner of the entry.
    # Should the owner be deleted, we also want to delete all of his entries.
    owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)

    # The category of this entry.
    category = db.ReferenceField(Category, required = True)

class CategoryBudget(db.Document):
    # The amount of the budget.
    amount = db.DecimalField(precision = 2, required = True)

    # The category.
    category = db.ReferenceField(Category, required = True)

def sumEntries():
    return sum([entry.amount for entry in Entry.objects if entry.amount > 0])

## Instruction:
Split Entry into Income and Expense schemes
## Code After:
from app import db
from app.mod_auth.model import User

class Category(db.Document):
    # The name of the category.
    name = db.StringField(required = True)

class Income(db.Document):
    # The amount of the entry.
    amount = db.DecimalField(precision = 2, required = True)

    # A short description for the entry.
    description = db.StringField(required = True)

    # The owner of the entry.
    # Should the owner be deleted, we also want to delete all of his entries.
    owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)

    # The category of this entry.
    category = db.ReferenceField(Category, required = True)

class Expense(db.Document):
    # The amount of the entry.
    amount = db.DecimalField(precision = 2, required = True)

    # A short description for the expense.
    description = db.StringField(required = True)

    # The owner of the expense.
    # Should the owner be deleted, we also want to delete all of his entries.
    owner = db.ReferenceField(User, reverse_delete_rule = db.CASCADE, required = True)

    # The category of this entry.
    category = db.ReferenceField(Category, required = True)

class CategoryBudget(db.Document):
    # The amount of the budget.
    amount = db.DecimalField(precision = 2, required = True)

    # The category.
    category = db.ReferenceField(Category, required = True)

def sumEntries():
    return sum([entry.amount for entry in Entry.objects if entry.amount > 0])
