class User(db.Model, UserMixin): """create the user table""" __tablename__='user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) actual_posts = db.relationship('ActualPost', backref='actual_author', lazy=True) def get_reset_token(self, expires_sec=1800): """generate a reset token for the reset password mail""" s = Serializer(current_app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')" # returns the data in a readable format
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) portfolio = db.relationship('Post_Portfolio', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}','{self.email}')"
class Post_Portfolio(db.Model): id = db.Column(db.Integer, primary_key=True) name_inv = db.Column(db.String(20), nullable=False) category_inv = db.Column(db.String(20), nullable=False) amount_inv = db.Column(db.String, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post_Portfolio('{self.name_inv}', '{self.category_inv}','{self.amount_inv}')"
class ActualPost(db.Model): """create the Actuals table""" __tablename__ = 'actualpost' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title_actual = db.Column(db.String(30), nullable=False, default='actual') category_actual = db.Column(db.String(30), nullable=False, default=None) actual_amount_name = db.Column(db.String(30), nullable=True) actual_amount = db.Column(db.Float, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) comments = db.Column(db.Text, nullable=True) def __repr__(self): return f"ActualPost('{self.title_actual}, '{self.category_actual}'\
class Post(db.Model): """create the Planned Budget table""" __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(30), nullable=False, default='planned') category = db.Column(db.String(30), nullable=False, default=None) name = db.Column(db.String(30), nullable=True) planned_amount_month = db.Column(db.Float, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) date_period = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) comments = db.Column(db.Text, nullable=True) def __repr__(self): return f"Post('{self.title}, '{self.category}'\
class User(db.Model): """ Represents a Budget app user. """ __tablename__ = 'users' # Basic user info id = db.Column(db.Integer, primary_key=True, autoincrement=True, index=True) first_name = db.Column(db.String(50), nullable=False) middle_name = db.Column(db.String(50), nullable=True) last_name = db.Column(db.String(50), nullable=False) email = db.Column(db.String(50), unique=True, nullable=False, index=True) username = db.Column(db.String(120), unique=True, nullable=False, index=True) password = db.Column(db.String(255), nullable=False) # Relationships budgets = db.relationship('Budget', backref='User', cascade="all, delete-orphan") # Tracking information last_login = db.Column(db.DateTime, server_default=func.now()) last_updated = db.Column(db.DateTime, onupdate=func.now()) def __init__(self, first_name, last_name, email, username, password, middle_name=None, last_login=None, last_updated=None): self.first_name = first_name self.middle_name = middle_name self.last_name = last_name self.email = email self.username = username self.password = password self.last_login = last_login self.last_updated = last_updated def __str__(self): return '<%s>' % self def create(self): db.session.add(self) db.session.commit() return self def update(self): db.session.add(self) db.session.commit() db.session.refresh(self) return self def delete(self): db.session.delete(self) db.session.commit() return self
class Budget(db.Model): """ Represents a budget app user's budget. """ __tablename__ = 'budgets' # Basic user info id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50), nullable=False) description = db.Column(db.Text) # The total amount allocated for a budget amount = db.Column(db.Integer, default=0) # This can be expanded upon as it's own model # i.e. "Monthly Budget", "House Repairs", etc. category = db.Column(db.String(50)) # Relationships budget_items = db.relationship('BudgetItem', backref='Budget', cascade="all, delete-orphan") created_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) # timestamps created_at = db.Column(db.DateTime, server_default=func.now()) updated_at = db.Column(db.DateTime, onupdate=func.now()) def __init__(self, name, created_by, amount=0, description=None, category=None, created_at=None, updated_at=None): self.name = name self.description = description self.category = category self.amount = amount self.created_by = created_by self.created_at = created_at self.updated_at = updated_at def __str__(self): return '<%s>' % self # TODO: move into a parent model class as it's shared functionality def create(self): db.session.add(self) db.session.commit() return self def update(self): db.session.add(self) db.session.commit() db.session.refresh(self) return self def delete(self): db.session.delete(self) db.session.commit() return self
class BudgetItem(db.Model): """ Represents a budget item. """ __tablename__ = 'budget_items' # Basic info id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50), nullable=False) description = db.Column(db.Text) # This represents a ledger amount (12.45) or 12.45 # Think in accounting terms # This value will be used to determine if the user is going over budget amount = db.Column(db.Integer) # This can be expanded upon as it's own model # i.e. "Monthly Budget", "House Repairs", etc. category = db.Column(db.String(50)) # Relationships budget_id = db.Column(db.Integer, db.ForeignKey('budgets.id'), nullable=False) # User that created the item, open for collaboration if # multiple users want to work on the same budget created_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) # timestamps created_at = db.Column(db.DateTime, server_default=func.now()) updated_at = db.Column(db.DateTime, onupdate=func.now()) def __init__(self, name, budget_id, created_by, amount=0, description=None, category=None, created_at=None, updated_at=None): self.name = name self.description = description self.category = category self.amount = amount self.budget_id = budget_id self.created_by = created_by self.created_at = created_at self.updated_at = updated_at def __str__(self): return '<%s>' % self # TODO: move into a parent model class as it's shared functionality def create(self): db.session.add(self) db.session.commit() return self def update(self): db.session.add(self) db.session.commit() db.session.refresh(self) return self def delete(self): db.session.delete(self) db.session.commit() return self