Last active
August 29, 2015 14:05
-
-
Save jpadilla/0ab511e51090e37b83e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from app import db, Artist, Album, Song | |
if __name__ == '__main__': | |
fixture_path = 'sample.json' | |
print "Dropping tables..." | |
db.drop_all() | |
print "Creating tables..." | |
db.create_all() | |
with open(fixture_path) as fixture_file: | |
fixtures = json.load(fixture_file) | |
for artist in fixtures: | |
print 'Creating artist: {}'.format(artist['name']) | |
_artist = Artist( | |
name=artist['name'], | |
bio=artist['bio'] | |
) | |
db.session.add(_artist) | |
for album in artist['albums']: | |
print ' Creating album: {}'.format(album['name']) | |
_album = Album( | |
name=album['name'], | |
artwork_url=album['artwork_url'], | |
artist=_artist | |
) | |
db.session.add(_album) | |
for song in album['songs']: | |
print ' Creating song: {}'.format(song['name']) | |
_song = Song(name=song['name'], album=_album, | |
url=song['url'], duration=song['duration']) | |
db.session.add(_song) | |
db.session.commit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt | |
from passlib.context import CryptContext | |
password_context = CryptContext(['pbkdf2_sha256']) | |
def make_password(raw_password): | |
return password_context.encrypt(raw_password) | |
def check_password(raw_password, password): | |
return password_context.verify(raw_password, password) | |
def generate_token(payload, secret): | |
return jwt.encode(payload, secret) | |
def decode_token(token, secret): | |
try: | |
return jwt.decode(token, secret) | |
except (jwt.ExpiredSignature, jwt.DecodeError): | |
return None | |
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
email = db.Column(db.String(255), unique=True) | |
password = db.Column(db.String(255)) | |
def __init__(self, email, password): | |
self.email = email | |
self.set_password(password) | |
@classmethod | |
def create(cls, email, password): | |
user = User(email=email, password=password) | |
db.session.add(user) | |
db.session.commit() | |
return user | |
@classmethod | |
def find_by_email(cls, email): | |
return User.query.filter_by(email=email).first() | |
@classmethod | |
def find_by_token(cls, token): | |
payload = decode_token(token, app.config['SECRET_KEY']) | |
if not payload or 'id' not in payload: | |
return None | |
return User.query.filter_by(id=payload['id']).first() | |
def set_password(self, raw_password): | |
self.password = make_password(raw_password) | |
def check_password(self, raw_password): | |
return check_password(raw_password, self.password) | |
def get_auth_token(self): | |
payload = { | |
'id': self.id | |
} | |
return generate_token(payload, app.config['SECRET_KEY']) | |
class Artist(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String(80), unique=True) | |
bio = db.Column(db.Text()) | |
def __init__(self, name, bio): | |
self.name = name | |
self.bio = bio | |
@classmethod | |
def get_all(cls): | |
return Artist.query.all() | |
@classmethod | |
def get(cls, artist_id): | |
return Artist.query.filter_by(id=artist_id).first() | |
class Album(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String(80)) | |
artwork_url = db.Column(db.String(255)) | |
artist_id = db.Column(db.Integer, db.ForeignKey('artist.id')) | |
artist = db.relationship('Artist', | |
backref=db.backref('albums', lazy='dynamic')) | |
def __init__(self, name, artist, artwork_url=None): | |
self.name = name | |
self.artist = artist | |
if artwork_url: | |
self.artwork_url = artwork_url | |
@classmethod | |
def get_all(cls): | |
return Album.query.all() | |
@classmethod | |
def get(cls, album_id): | |
return Album.query.filter_by(id=album_id).first() | |
@classmethod | |
def total_count(cls): | |
return Album.query.count() | |
class Song(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String(80)) | |
url = db.Column(db.String(255)) | |
duration = db.Column(db.Integer) | |
album_id = db.Column(db.Integer, db.ForeignKey('album.id')) | |
album = db.relationship('Album', | |
backref=db.backref('songs', lazy='dynamic')) | |
def __init__(self, name, album, url=None, duration=None): | |
self.name = name | |
self.album = album | |
if url: | |
self.url = url | |
if duration: | |
self.duration = duration | |
@classmethod | |
def get_all(cls): | |
return Song.query.all() | |
@classmethod | |
def get_favorites(cls, user): | |
favorites = Favorite.query.filter_by(user=user) | |
song_ids = [favorite.song_id for favorite in favorites] | |
if song_ids: | |
return Song.filter_by_ids(song_ids) | |
return [] | |
@classmethod | |
def filter_by_ids(cls, song_ids): | |
return Song.query.filter(Song.id.in_(song_ids)) | |
@classmethod | |
def get(cls, song_id): | |
return Song.query.filter_by(id=song_id).first() | |
@classmethod | |
def total_count(cls): | |
return Song.query.count() | |
@classmethod | |
def total_duration(cls): | |
duration = 0 | |
for song in Song.get_all(): | |
duration += song.duration | |
return duration | |
def set_favorite(self, user, favorite): | |
if favorite is True: | |
return self.favorite(user) | |
if favorite is False: | |
return self.unfavorite(user) | |
def favorite(self, user): | |
favorite = Favorite.query.filter_by(song=self, user=user).first() | |
is_favorited = True | |
if not favorite: | |
favorite = Favorite(song=self, user=user) | |
db.session.add(favorite) | |
db.session.commit() | |
return is_favorited | |
def unfavorite(self, user): | |
favorite = Favorite.query.filter_by(song=self, user=user).first() | |
is_favorited = False | |
if favorite: | |
db.session.delete(favorite) | |
db.session.commit() | |
return is_favorited | |
def is_favorited(self, user): | |
favorite = Favorite.query.filter_by(song=self, user=user).first() | |
return True if favorite else False | |
class Favorite(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) | |
user = db.relationship('User', | |
backref=db.backref('favorites', lazy='dynamic')) | |
song_id = db.Column(db.Integer, db.ForeignKey('song.id')) | |
song = db.relationship('Song', | |
backref=db.backref('favorites', lazy='dynamic')) | |
def __init__(self, song, user): | |
self.song = song | |
self.user = user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"name": "Los Macuanos", | |
"bio": "", | |
"albums": [{ | |
"name": "Ritmo de Amor | Remixes", | |
"artwork_url": "http://s3.amazonaws.com/soundem-media/los-macuanos/artwork.png", | |
"songs": [{ | |
"name": "Ritmo de Amor", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_01_-_Ritmo_de_Amor.mp3", | |
"duration": 276 | |
}, { | |
"name": "Ritmo de Amor (Letter D Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_02_-_Ritmo_de_Amor_Letter_D_Remix.mp3", | |
"duration": 240 | |
}, { | |
"name": "Ritmo de Amor (Sheeqo Beat Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_03_-_Ritmo_de_Amor_Sheeqo_Beat_Remix.mp3", | |
"duration": 234 | |
}, { | |
"name": "Ritmo de Amor (Future Feelings Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_04_-_Ritmo_de_Amor_Future_Feelings_Remix.mp3", | |
"duration": 234 | |
}, { | |
"name": "Ritmo de Amor (DJ Teenage Wolf Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_05_-_Ritmo_de_Amor_DJ_Teenage_Wolf_Remix.mp3", | |
"duration": 251 | |
}, { | |
"name": "Ritmo de Amor (Santos Xtended Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_06_-_Ritmo_de_Amor_Santos_Xtended_Remix.mp3", | |
"duration": 395 | |
}, { | |
"name": "Ritmo de Amor (Pedro Infame Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_07_-_Ritmo_de_Amor_Pedro_Infame_Remix.mp3", | |
"duration": 317 | |
}, { | |
"name": "Ritmo de Amor (Colateral Soundtrack Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_08_-_Ritmo_de_Amor_Colateral_Soundtrack_Remix.mp3", | |
"duration": 198 | |
}, { | |
"name": "Ritmo de Amor (Malandro Remix)", | |
"url": "http://s3.amazonaws.com/soundem-media/los-macuanos/Los_Macuanos_-_09_-_Ritmo_de_Amor_Malandro_Remix.mp3", | |
"duration": 471 | |
}] | |
}] | |
}, { | |
"name": "Frank Guerrero", | |
"bio": "", | |
"albums": [{ | |
"name": "En Beta 0.1", | |
"artwork_url": "http://s3.amazonaws.com/soundem-media/frank-guerrero-y-su-grupo-ach/album-art.jpg", | |
"songs": [{ | |
"name": "Vamos A Tocar Sonero", | |
"url": "http://s3.amazonaws.com/soundem-media/frank-guerrero-y-su-grupo-ach/Frank_Guerrero_y_Su_Grupo_Ach_-_01_-_Vamos_A_Tocar_Sonero.mp3", | |
"duration": 220 | |
}, { | |
"name": "Fui Yo", | |
"url": "http://s3.amazonaws.com/soundem-media/frank-guerrero-y-su-grupo-ach/Frank_Guerrero_y_Su_Grupo_Ach_-_02_-_Fui_Yo.mp3", | |
"duration": 237 | |
}, { | |
"name": "Cupido", | |
"url": "http://s3.amazonaws.com/soundem-media/frank-guerrero-y-su-grupo-ach/Frank_Guerrero_y_Su_Grupo_Ach_-_03_-_Cupido.mp3", | |
"duration": 199 | |
}, { | |
"name": "Barranquilla Tiene Un Swing", | |
"url": "http://s3.amazonaws.com/soundem-media/frank-guerrero-y-su-grupo-ach/Frank_Guerrero_y_Su_Grupo_Ach_-_04_-_Barranquilla_Tiene_Un__Swing.mp3", | |
"duration": 235 | |
}] | |
}] | |
}, { | |
"name": "Tommy Tornado", | |
"bio": "", | |
"albums": [{ | |
"name": "Live at Pacific Parc", | |
"artwork_url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/album-art.png", | |
"songs": [{ | |
"name": "Mistery Woman", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_01_-_Mistery_Woman.mp3", | |
"duration": 298 | |
}, { | |
"name": "Gladiator", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_02_-_Gladiator.mp3", | |
"duration": 220 | |
}, { | |
"name": "Clear Vision feat. Soulmack", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_03_-_Clear_Vision_feat_Soulmack.mp3", | |
"duration": 221 | |
}, { | |
"name": "Cheatin feat. Soulmack", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_04_-_Cheatin_feat_Soulmack.mp3", | |
"duration": 185 | |
}, { | |
"name": "Door Peeper", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_05_-_Door_Peeper.mp3", | |
"duration": 199 | |
}, { | |
"name": "Ruff Tuff", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_06_-_Ruff_Tuff.mp3", | |
"duration": 244 | |
}, { | |
"name": "Skank it", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_07_-_Skank_it.mp3", | |
"duration": 159 | |
}, { | |
"name": "Mascarade", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_08_-_Mascarade.mp3", | |
"duration": 256 | |
}, { | |
"name": "Tight Spot/Rough Style feat. Osagyefo", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_09_-_Tight_SpotRough_Style_feat_Osagyefo.mp3", | |
"duration": 456 | |
}, { | |
"name": "Closing", | |
"url": "http://s3.amazonaws.com/soundem-media/tommy-tornado/Tommy_Tornado_-_10_-_Closing.mp3", | |
"duration": 431 | |
}] | |
}] | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment