From 52791f111e9ee885389377a91cab44faa61c32b5 Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Fri, 27 Nov 2015 19:24:36 +0000 Subject: Better library management --- models/album.py | 23 +++++++++++++++++++++++ models/artist.py | 4 ++-- models/track.py | 36 ++++++++++++++++++++---------------- 3 files changed, 45 insertions(+), 18 deletions(-) (limited to 'models') diff --git a/models/album.py b/models/album.py index 216b615..9ca3798 100644 --- a/models/album.py +++ b/models/album.py @@ -134,3 +134,26 @@ class Album(): ) return albums + + def all(order="album.id", direction="ASC", limit=None, offset=None): + db = DbManager() + cursor = db.cursor() + albums = [] + + select_string = """SELECT * FROM album LEFT JOIN album_artist ON + album_artist.album_id = album.id LEFT JOIN artist ON + album_artist.artist_id = artist.id ORDER BY %s %s""" % (order, + direction) + + if limit is not None and offset is not None: + select_string = " ".join((select_string, + "LIMIT %s OFFSET %s" % (limit, offset))) + + result = cursor.execute(select_string) + + for row in result: + albums.append( + Album(id=row[0], name=row[1], date=row[2]) + ) + + return albums diff --git a/models/artist.py b/models/artist.py index 2e69354..fee081c 100644 --- a/models/artist.py +++ b/models/artist.py @@ -141,8 +141,8 @@ class Artist: cursor = db.cursor() artists = [] - select_string = """SELECT * FROM artist ORDER BY %s %s""" % (order, - direction) + select_string = "SELECT * FROM artist ORDER BY %s %s" % (order, + direction) if limit is not None and offset is not None: select_string = " ".join((select_string, diff --git a/models/track.py b/models/track.py index 8b16d26..dead1f8 100644 --- a/models/track.py +++ b/models/track.py @@ -1,4 +1,5 @@ -import apsw +import logging +import sqlite3 from common import utils from db.db_manager import DbManager @@ -6,6 +7,9 @@ from models.artist import Artist from models.album import Album +logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) + + class Track: def __init__(self, id=None, **kwargs): @@ -136,7 +140,7 @@ class Track: musicbrainz_artistid)) artist = Artist( - id=db.last_insert_rowid(), name=artist_name, + id=c.lastrowid, name=artist_name, sortname=artistsort, musicbrainz_artistid=musicbrainz_artistid ) @@ -180,7 +184,7 @@ class Track: musicbrainz_albumid) VALUES (?,?,?)""", (album_name, album_date, mb_albumid)) - album = Album(id=db.last_insert_rowid(), name=album_name, + album = Album(id=c.lastrowid, name=album_name, date=album_date, musicbrainz_albumid=mb_albumid) elif album_name: @@ -197,7 +201,7 @@ class Track: c.execute("""INSERT INTO album (name, `date`) VALUES (?,?)""", (album_name, album_date)) - album = Album(id=db.last_insert_rowid(), name=album_name, + album = Album(id=c.lastrowid, name=album_name, date=album_date) if album: @@ -219,7 +223,7 @@ class Track: album_id) VALUES(?,?)""", (artist.id, album.id) ) - except apsw.ConstraintError: + except sqlite3.IntegrityError: pass track_number = None @@ -248,7 +252,7 @@ class Track: c.execute("""INSERT INTO album_track (album_id, track_id) VALUES(?,?)""", (album.id, self.id)) - except apsw.ConstraintError: + except sqlite3.IntegrityError: pass for artist in artists: @@ -256,10 +260,10 @@ class Track: c.execute("""INSERT INTO artist_track (artist_id, track_id) VALUES(?,?)""", (artist.id, self.id)) - except apsw.ConstraintError: + except sqlite3.IntegrityError: pass - c.execute("COMMIT TRANSACTION") + db.commit() return True @@ -411,7 +415,7 @@ class Track: musicbrainz_artistid)) artist = Artist( - id=db.last_insert_rowid(), name=artist_name, + id=c.lastrowid, name=artist_name, sortname=artistsort, musicbrainz_artistid=musicbrainz_artistid ) @@ -455,7 +459,7 @@ class Track: musicbrainz_albumid) VALUES (?,?,?)""", (album_name, album_date, mb_albumid)) - album = Album(id=db.last_insert_rowid(), name=album_name, + album = Album(id=c.lastrowid, name=album_name, date=album_date, musicbrainz_albumid=mb_albumid) elif album_name: @@ -473,7 +477,7 @@ class Track: c.execute("""INSERT INTO album (name, `date`) VALUES (?,?)""", (album_name, album_date)) - album = Album(id=db.last_insert_rowid(), name=album_name, + album = Album(id=c.lastrowid, name=album_name, date=album_date) for artist in artists: @@ -484,7 +488,7 @@ class Track: album_id) VALUES(?,?)""", (artist.id, album.id) ) - except apsw.ConstraintError: + except sqlite3.IntegrityError: pass track_number = None @@ -516,7 +520,7 @@ class Track: (track_number, track_name, track_grouping, filename)) - track = Track(id=db.last_insert_rowid(), tracknumber=track_number, + track = Track(id=c.lastrowid, tracknumber=track_number, name=track_name, grouping=track_grouping, filename=filename) @@ -525,7 +529,7 @@ class Track: c.execute("""INSERT INTO album_track (album_id, track_id) VALUES(?,?)""", (album.id, track.id)) - except apsw.ConstraintError: + except sqlite3.IntegrityError: pass for artist in artists: @@ -533,9 +537,9 @@ class Track: c.execute("""INSERT INTO artist_track (artist_id, track_id) VALUES(?,?)""", (artist.id, track.id)) - except apsw.ConstraintError: + except sqlite3.IntegrityError: pass - c.execute("COMMIT TRANSACTION") + db.commit() return True -- cgit v1.2.3