-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
26 lines (21 loc) · 1.1 KB
/
models.py
File metadata and controls
26 lines (21 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True, nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
expenses = db.relationship('Expense', backref='user', lazy=True)
def set_password(self, password):
"""Hashes the password before storing it"""
self.password_hash = generate_password_hash(password)
def check_password(self, password):
"""Checks if the entered password matches the stored hash"""
return check_password_hash(self.password_hash, password)
class Expense(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
category = db.Column(db.String(50), nullable=False)
amount = db.Column(db.Float, nullable=False)
date = db.Column(db.Date, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # Ensures expenses are linked to users