1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-09 19:52:16 +00:00

Relationship between track and folder

This commit is contained in:
Alban 2012-10-14 13:07:02 +02:00
parent d34d54fd50
commit 143098ee9a
3 changed files with 22 additions and 7 deletions

7
db.py
View File

@ -3,7 +3,7 @@
import config
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.ext.declarative import declarative_base
@ -60,7 +60,7 @@ class MusicFolder(Base):
id = UUID.gen_id_column()
name = Column(String, unique = True)
path = Column(String)
last_scan = Column(Date, nullable = True)
last_scan = Column(DateTime, nullable = True)
class Artist(Base):
__tablename__ = 'artist'
@ -88,6 +88,9 @@ class Track(Base):
album_id = Column(UUID, ForeignKey('album.id'))
path = Column(String, unique = True)
folder_id = Column(UUID, ForeignKey('folder.id'))
folder = relationship('MusicFolder')
def init_db():
Base.metadata.create_all(bind = engine)

View File

@ -27,12 +27,13 @@ class Scanner:
for root, subfolders, files in os.walk(folder.path):
for f in files:
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):
for artist in db.Artist.query.all():
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):
album.tracks.remove(track)
self.__session.delete(track)
@ -45,14 +46,14 @@ class Scanner:
self.__session.delete(artist)
self.__deleted_artists += 1
def __scan_file(self, path):
def __scan_file(self, path, folder):
tag = eyeD3.Tag()
tag.link(path)
al = self.__find_album(tag.getArtist(), tag.getAlbum())
tr = filter(lambda t: t.path == path, al.tracks)
if not tr:
tr = db.Track(path = path)
tr = db.Track(path = path, folder = folder)
self.__added_tracks += 1
else:
tr = tr[0]

13
web.py
View File

@ -131,8 +131,19 @@ def del_folder(id):
flash('No such folder')
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)
# TODO delete associated tracks
db.session.commit()
flash("Deleted folder '%s'" % folder.name)