class User(db.Model, UserMixin): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(75), nullable=True) lname = db.Column(db.String(75), nullable=True) username = db.Column(db.String(32), unique=True, nullable=False) email = db.Column(db.String(128), unique=True, nullable=False) image_file = db.Column(db.String(32), nullable=True, default='default.jpg') preflang = db.Column(db.String(20), nullable=True) password = db.Column(db.String(60), nullable=False) #searches = db.relationship('user_search_history', backref=db.backref('user')) #playlists = db.relationship('user_song_playlist', backref=db.backref('user')) #songs = db.relationship('Song', secondary=listened) posts = db.relationship('Post', backref='author', lazy=True) podcasts = db.relationship('Podcast', secondary=subscribed, backref='pod_art', lazy=True) listen = db.relationship('Song', secondary=listened, backref=db.backref('listener', lazy=True)) artist_id = db.Column(db.Integer, db.ForeignKey('artist.id'), nullable=True) #playlist_id = db.Column(db.Integer, db.ForeignKey('playlist.id')) playlist_id = db.Column(db.Integer, db.ForeignKey('playlist.id'), nullable=True) def __repr__(self): return f"User('{self.username}', '{self.name}', '{self.email}', '{self.image_file}')"
class Songs(db.Model): """ A class to map the songs table using SQLAlchemy ... Attributes ------- song_id : Integer database column Holds the id of the song song_title : String databse column Holds the song name seconds : String databse column Holds the duration in seconds thumbnail_url: String databse column Holds the thumbnail url for song album_id : Integer database column Holds the foreign key for albums table Methods ------- __repr__() Method to represent the class object """ song_id = db.Column(db.Integer, primary_key=True) song_title = db.Column(db.String(60), nullable=False) seconds = db.Column(db.Integer, nullable=False) thumbnail_url = db.Column(db.String(200), default='thumbnail.png') album_id = db.Column(db.Integer, db.ForeignKey('albums.album_id'), nullable=False) def __repr__(self): return f"Songs('{self.song_title}', '{self.seconds}', {self.album_id})"
class Albums(db.Model): """ A class to map the artists table using SQLAlchemy ... Attributes ------- album_id : Integer database column Holds the id of the album album_title : String databse column Holds the albums's title artist_id : Foreign key for artists table songs : Relationship A relationship to songs table Methods ------- __repr__() Method to represent the class object """ album_id = db.Column(db.Integer, primary_key=True) album_title = db.Column(db.String(60), nullable=False) artist_id = db.Column(db.Integer, db.ForeignKey('artists.artist_id'), nullable=False) songs = db.relationship('Songs', backref='songs', lazy=True) def __repr__(self): return f"Albums('{self.album_title}', {self.artist_id})"
class Playlist(db.Model): """ Model for storing playlist information belonging to a specific user """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), unique=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('playlists', lazy='dynamic')) tracks = db.relationship('Track', secondary=tracks, backref=db.backref('playlists', lazy='dynamic')) def __init__(self, title, user): self.title = title self.user = user def __repr__(self): return '<Playlist %r>' % (self.title) @property def serialize(self): """Return object data in easily serializeable format""" return { 'id': self.id, 'title': self.title, 'user': self.user.serialize, 'tracks': [item.serialize for item in self.tracks] }
class AccessToken(db.Model): """ Model for storing OAuth2 token data for a given user """ @classmethod def from_json(cls, data): token_dict = json.loads(data) return cls(token_dict['access_token'], token_dict['token_type'], token_dict['expires_in'], token_dict['refresh_token']) id = db.Column(db.Integer, primary_key=True) access_token = db.Column(db.String(255)) token_type = db.Column(db.String(50)) expires_in = db.Column(db.Integer) refresh_token = db.Column(db.String(255)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('access_token', lazy='dynamic')) def __init__(self, access_token, token_type, expires_in, refresh_token): self.access_token = access_token self.token_type = token_type self.expires_in = expires_in self.refresh_token = refresh_token
class EventStatus(db.Model, UserMixin): time = db.Column(db.Integer, db.ForeignKey('event.time')) begin_date = db.Column(db.String(100), primary_key=True) end_date = db.Column(db.String(100), primary_key=True) status = db.Column(db.String(100), nullable=True) def __repr__(self): return f"EventStatus('{self.status})"
class City(db.Model, UserMixin): pincode = db.Column(db.Integer, db.ForeignKey('place.pincode'), primary_key=True) name = db.Column(db.String(75), nullable=False) state = db.Column(db.String(75), nullable=True) def __repr__(self): return f"City('{self.name}', '{self.state}')"
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=True) date_posted = db.Column(db.DateTime, nullable=True, default=datetime.utcnow) content = db.Column(db.Text, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class Artist(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(75), nullable=False) email = db.Column(db.String(128), unique=True) likes = db.Column(db.Integer, default=0) dislikes = db.Column(db.Integer, default=0) rating = db.Column(db.Integer, default=2) song = db.relationship('Song', backref='singer', lazy=True) user = db.relationship('User', backref='user_art', uselist=False) podcast = db.relationship('Podcast', backref='podcast', lazy=True, uselist=False) label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
class Podcast(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=True) title = db.Column(db.String(75), nullable=False) description = db.Column(db.String, default='No description') category = db.Column(db.String, nullable=False) release_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) pod_file = db.Column(db.String(200), nullable=False) image_file = db.Column(db.String(32), nullable=True, default='default.jpg') rating = db.Column(db.Integer, default=2) likes = db.Column(db.Integer, default=0) dislikes = db.Column(db.Integer, default=0) artist_id = db.Column(db.Integer, db.ForeignKey('artist.id'))
class Song(db.Model, UserMixin): #__searchable__ = ['name', 'genre'] id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(75), nullable=False) album = db.Column(db.String(75), nullable=False) genre = db.Column(db.String(75), nullable=True) release_date = db.Column(db.DateTime, nullable=True, default=datetime.utcnow) rating = db.Column(db.Integer, default=2) likes = db.Column(db.Integer, default=0) dislikes = db.Column(db.Integer, default=0) song_file = db.Column(db.String(200), nullable=False) image_file = db.Column(db.String(32), nullable=True, default='default.jpg') artist_id = db.Column(db.Integer, db.ForeignKey('artist.id')) contains = db.relationship('Contains', backref='contains', uselist=False)
class Review(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=True) date_posted = db.Column(db.DateTime, nullable=True, default=datetime.utcnow) suggestions = db.Column(db.Text, nullable=True) event_id = db.Column(db.Integer, db.ForeignKey('event.id'), primary_key=True) #__table_args__ = ( #PrimaryKeyConstraint('field2', 'field1'), #{}, #) def __repr__(self): return f"Label('{self.title})"
expires_in = db.Column(db.Integer) refresh_token = db.Column(db.String(255)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('access_token', lazy='dynamic')) def __init__(self, access_token, token_type, expires_in, refresh_token): self.access_token = access_token self.token_type = token_type self.expires_in = expires_in self.refresh_token = refresh_token tracks = db.Table( 'tracks', db.Column('track_id', db.Integer, db.ForeignKey('track.id')), db.Column('playlist_id', db.Integer, db.ForeignKey('playlist.id'))) class Playlist(db.Model): """ Model for storing playlist information belonging to a specific user """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), unique=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('playlists', lazy='dynamic')) tracks = db.relationship('Track',
class user_search_history(db.Model): id = db.Column(db.Integer, primary_key=True) datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) searchno = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class Contains(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) song_id = db.Column(db.Integer, db.ForeignKey('song.id')) playlist_id = db.Column(db.Integer, db.ForeignKey('playlist.id'))
from datetime import datetime from music_app import app, db, login_manager from flask_login import UserMixin #from flask_whooshalchemy import wa @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) listened = db.Table("Listened", db.Column("song_id", db.Integer, db.ForeignKey("song.id")), db.Column("user_id", db.Integer, db.ForeignKey("user.id"))) subscribed = db.Table( 'subscribed', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('podcast_id', db.Integer, db.ForeignKey('podcast.id'))) performance = db.Table( 'performance', db.Column('event_id', db.Integer, db.ForeignKey('event.id')), db.Column('artist_id', db.Integer, db.ForeignKey('artist.id'))) held_in = db.Table( 'held_in', db.Column('event_id', db.Integer, db.ForeignKey('event.id')), db.Column('place_id', db.Integer, db.ForeignKey('place.id'))) class User(db.Model, UserMixin): __table_args__ = {'extend_existing': True}