mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 17:06:17 +00:00
Relationship between track and folder
This commit is contained in:
parent
d34d54fd50
commit
143098ee9a
7
db.py
7
db.py
@ -3,7 +3,7 @@
|
|||||||
import config
|
import config
|
||||||
|
|
||||||
from sqlalchemy import create_engine, Column, ForeignKey
|
from sqlalchemy import create_engine, Column, ForeignKey
|
||||||
from sqlalchemy import Integer, String, Boolean, Date, Time
|
from sqlalchemy import Integer, String, Boolean, DateTime, Time
|
||||||
from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
|
from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ class MusicFolder(Base):
|
|||||||
id = UUID.gen_id_column()
|
id = UUID.gen_id_column()
|
||||||
name = Column(String, unique = True)
|
name = Column(String, unique = True)
|
||||||
path = Column(String)
|
path = Column(String)
|
||||||
last_scan = Column(Date, nullable = True)
|
last_scan = Column(DateTime, nullable = True)
|
||||||
|
|
||||||
class Artist(Base):
|
class Artist(Base):
|
||||||
__tablename__ = 'artist'
|
__tablename__ = 'artist'
|
||||||
@ -88,6 +88,9 @@ class Track(Base):
|
|||||||
album_id = Column(UUID, ForeignKey('album.id'))
|
album_id = Column(UUID, ForeignKey('album.id'))
|
||||||
path = Column(String, unique = True)
|
path = Column(String, unique = True)
|
||||||
|
|
||||||
|
folder_id = Column(UUID, ForeignKey('folder.id'))
|
||||||
|
folder = relationship('MusicFolder')
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
Base.metadata.create_all(bind = engine)
|
Base.metadata.create_all(bind = engine)
|
||||||
|
|
||||||
|
@ -27,12 +27,13 @@ class Scanner:
|
|||||||
for root, subfolders, files in os.walk(folder.path):
|
for root, subfolders, files in os.walk(folder.path):
|
||||||
for f in files:
|
for f in files:
|
||||||
if f.endswith('.mp3'):
|
if f.endswith('.mp3'):
|
||||||
self.__scan_file(os.path.join(root, f))
|
self.__scan_file(os.path.join(root, f), folder)
|
||||||
|
folder.last_scan = datetime.datetime.now()
|
||||||
|
|
||||||
def prune(self, folder):
|
def prune(self, folder):
|
||||||
for artist in db.Artist.query.all():
|
for artist in db.Artist.query.all():
|
||||||
for album in artist.albums[:]:
|
for album in artist.albums[:]:
|
||||||
for track in album.tracks[:]:
|
for track in filter(lambda t: t.folder.id == folder.id, album.tracks):
|
||||||
if not os.path.exists(track.path):
|
if not os.path.exists(track.path):
|
||||||
album.tracks.remove(track)
|
album.tracks.remove(track)
|
||||||
self.__session.delete(track)
|
self.__session.delete(track)
|
||||||
@ -45,14 +46,14 @@ class Scanner:
|
|||||||
self.__session.delete(artist)
|
self.__session.delete(artist)
|
||||||
self.__deleted_artists += 1
|
self.__deleted_artists += 1
|
||||||
|
|
||||||
def __scan_file(self, path):
|
def __scan_file(self, path, folder):
|
||||||
tag = eyeD3.Tag()
|
tag = eyeD3.Tag()
|
||||||
tag.link(path)
|
tag.link(path)
|
||||||
|
|
||||||
al = self.__find_album(tag.getArtist(), tag.getAlbum())
|
al = self.__find_album(tag.getArtist(), tag.getAlbum())
|
||||||
tr = filter(lambda t: t.path == path, al.tracks)
|
tr = filter(lambda t: t.path == path, al.tracks)
|
||||||
if not tr:
|
if not tr:
|
||||||
tr = db.Track(path = path)
|
tr = db.Track(path = path, folder = folder)
|
||||||
self.__added_tracks += 1
|
self.__added_tracks += 1
|
||||||
else:
|
else:
|
||||||
tr = tr[0]
|
tr = tr[0]
|
||||||
|
13
web.py
13
web.py
@ -131,8 +131,19 @@ def del_folder(id):
|
|||||||
flash('No such folder')
|
flash('No such folder')
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
|
# delete associated tracks and prune empty albums/artists
|
||||||
|
for artist in db.Artist.query.all():
|
||||||
|
for album in artist.albums[:]:
|
||||||
|
for track in filter(lambda t: t.folder.id == folder.id, album.tracks):
|
||||||
|
album.tracks.remove(track)
|
||||||
|
db.session.delete(track)
|
||||||
|
if len(album.tracks) == 0:
|
||||||
|
artist.albums.remove(album)
|
||||||
|
db.session.delete(album)
|
||||||
|
if len(artist.albums) == 0:
|
||||||
|
db.session.delete(artist)
|
||||||
db.session.delete(folder)
|
db.session.delete(folder)
|
||||||
# TODO delete associated tracks
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash("Deleted folder '%s'" % folder.name)
|
flash("Deleted folder '%s'" % folder.name)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user