66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import re
|
|
from datetime import datetime
|
|
|
|
from flask_login import UserMixin
|
|
|
|
from extensions import db, bcrypt
|
|
|
|
#Users
|
|
class User(UserMixin, db.Model):
|
|
__tablename__ = "user"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
username = db.Column(db.String(100), unique=True, nullable= False)
|
|
email = db.Column(db.String(120), unique=True, nullable = False)
|
|
password_hash = db.Column(db.String(200), nullable=False)
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
|
|
|
playlist = db.relationship("Playlist", back_populates="user", lazy="dynamic")
|
|
|
|
def set_password(self, plain_password: str):
|
|
if not plain_password or len(plain_password) < 8:
|
|
raise ValueError("Password must be at least 8 characters!")
|
|
if not re.search(r"[A-Z]", plain_password):
|
|
raise ValueError("Password must include one uppercase letter")
|
|
if not re.search(r"[\d]", plain_password):
|
|
raise ValueError("Password must include one number")
|
|
|
|
self.password_hash = (
|
|
bcrypt.generate_password_hash(
|
|
plain_password
|
|
).decode("utf-8")
|
|
)
|
|
|
|
def check_password(self, plain_password: str) -> bool:
|
|
return bcrypt.check_password_hash(self.password_hash, plain_password)
|
|
def __repr__(self):
|
|
return f"<User id={self.id} username={self.username!r}>"
|
|
#Saves the playlist that the users makes
|
|
class Playlist(db.Model):
|
|
id= db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(100))
|
|
description = db.Column(db.String(255))
|
|
|
|
spotify_playlist_id = db.Column(db.String(120))
|
|
spotify_url = db.Column(db.String(300))
|
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
user = db.relationship("User", backref="playlists")
|
|
tracks = db.relationship(
|
|
"Tracks",
|
|
backref ="playlist",
|
|
cascade="all, delete"
|
|
)
|
|
|
|
#Saves every song in the playlist
|
|
class Tracks(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
name = db.Column(db.String(255))
|
|
artist = db.Column(db.String(255))
|
|
|
|
playlist_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey("playlist.id")
|
|
)
|
|
|