2012-10-13 09:29:48 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import config
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-06-25 20:07:49 +00:00
|
|
|
from sqlalchemy import create_engine, Table, Column, ForeignKey, func
|
2012-10-20 16:05:37 +00:00
|
|
|
from sqlalchemy import Integer, String, Boolean, DateTime
|
2012-10-13 12:53:09 +00:00
|
|
|
from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
|
2012-10-13 09:29:48 +00:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
2013-07-15 18:30:33 +00:00
|
|
|
from sqlalchemy.types import TypeDecorator, BINARY
|
2013-07-16 09:30:19 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID as pgUUID
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-06-14 16:46:01 +00:00
|
|
|
import uuid, datetime, time
|
2012-10-20 16:05:37 +00:00
|
|
|
import os.path
|
2013-11-01 18:46:16 +00:00
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
def _unique(session, cls, hashfunc, queryfunc, constructor, arg, kw):
|
|
|
|
cache = getattr(session, '_unique_cache', None)
|
|
|
|
if cache is None:
|
|
|
|
session._unique_cache = cache = {}
|
|
|
|
|
|
|
|
key = (cls, hashfunc(*arg, **kw))
|
|
|
|
if key in cache:
|
|
|
|
return cache[key]
|
|
|
|
else:
|
|
|
|
with session.no_autoflush:
|
|
|
|
q = session.query(cls)
|
|
|
|
q = queryfunc(q, *arg, **kw)
|
|
|
|
obj = q.first()
|
|
|
|
if not obj:
|
|
|
|
obj = constructor(*arg, **kw)
|
|
|
|
session.add(obj)
|
|
|
|
cache[key] = obj
|
|
|
|
return obj
|
|
|
|
|
|
|
|
class UniqueMixin(object):
|
|
|
|
@classmethod
|
|
|
|
def unique_hash(cls, *arg, **kw):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def unique_filter(cls, query, *arg, **kw):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def as_unique(cls, session, *arg, **kw):
|
|
|
|
return _unique(
|
|
|
|
session,
|
|
|
|
cls,
|
|
|
|
cls.unique_hash,
|
|
|
|
cls.unique_filter,
|
|
|
|
cls,
|
|
|
|
arg, kw
|
|
|
|
)
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
class UUID(TypeDecorator):
|
2013-07-16 09:30:19 +00:00
|
|
|
"""Platform-somewhat-independent UUID type
|
|
|
|
|
|
|
|
Uses Postgresql's UUID type, otherwise uses BINARY(16),
|
|
|
|
should be more efficient than a CHAR(32).
|
|
|
|
|
|
|
|
Mix of http://stackoverflow.com/a/812363
|
|
|
|
and http://www.sqlalchemy.org/docs/core/types.html#backend-agnostic-guid-type
|
|
|
|
"""
|
|
|
|
|
2012-10-13 09:29:48 +00:00
|
|
|
impl = BINARY
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-07-16 09:30:19 +00:00
|
|
|
def load_dialect_impl(self, dialect):
|
|
|
|
if dialect.name == 'postgresql':
|
|
|
|
return dialect.type_descriptor(pgUUID())
|
|
|
|
else:
|
|
|
|
return dialect.type_descriptor(BINARY(16))
|
2012-10-13 09:29:48 +00:00
|
|
|
|
2013-07-16 09:30:19 +00:00
|
|
|
def process_bind_param(self, value, dialect):
|
2012-10-13 09:29:48 +00:00
|
|
|
if value and isinstance(value, uuid.UUID):
|
2013-07-16 09:30:19 +00:00
|
|
|
if dialect.name == 'postgresql':
|
|
|
|
return str(value)
|
2012-10-13 09:29:48 +00:00
|
|
|
return value.bytes
|
|
|
|
if value and not isinstance(value, uuid.UUID):
|
|
|
|
raise ValueError, 'value %s is not a valid uuid.UUID' % value
|
|
|
|
return None
|
|
|
|
|
2013-07-16 09:30:19 +00:00
|
|
|
def process_result_value(self, value, dialect):
|
2012-10-13 09:29:48 +00:00
|
|
|
if value:
|
2013-07-16 09:30:19 +00:00
|
|
|
if dialect.name == 'postgresql':
|
|
|
|
return uuid.UUID(value)
|
2012-10-13 09:29:48 +00:00
|
|
|
return uuid.UUID(bytes = value)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def gen_id_column():
|
|
|
|
return Column(UUID, primary_key = True, default = uuid.uuid4)
|
|
|
|
|
2012-12-01 18:52:41 +00:00
|
|
|
def now():
|
|
|
|
return datetime.datetime.now().replace(microsecond = 0)
|
|
|
|
|
2013-07-15 12:37:51 +00:00
|
|
|
engine = create_engine(config.get('base', 'database_uri'), convert_unicode = True)
|
2013-11-03 20:46:19 +00:00
|
|
|
session = scoped_session(sessionmaker(autoflush = False, bind = engine))
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
Base = declarative_base()
|
2012-10-13 10:29:37 +00:00
|
|
|
Base.query = session.query_property()
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
class User(Base):
|
2012-10-13 12:53:09 +00:00
|
|
|
__tablename__ = 'user'
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
2013-07-15 18:30:33 +00:00
|
|
|
name = Column(String(64), unique = True)
|
|
|
|
mail = Column(String(256))
|
2012-10-13 09:29:48 +00:00
|
|
|
password = Column(String(40))
|
|
|
|
salt = Column(String(6))
|
2012-11-16 15:59:09 +00:00
|
|
|
admin = Column(Boolean, default = False)
|
2012-11-22 15:36:03 +00:00
|
|
|
lastfm_session = Column(String(32), nullable = True)
|
|
|
|
lastfm_status = Column(Boolean, default = True) # True: ok/unlinked, False: invalid session
|
2012-10-13 09:29:48 +00:00
|
|
|
|
2013-06-10 18:56:20 +00:00
|
|
|
last_play_id = Column(UUID, ForeignKey('track.id'), nullable = True)
|
|
|
|
last_play = relationship('Track')
|
|
|
|
last_play_date = Column(DateTime, nullable = True)
|
|
|
|
|
2013-06-18 14:12:35 +00:00
|
|
|
def as_subsonic_user(self):
|
|
|
|
return {
|
|
|
|
'username': self.name,
|
|
|
|
'email': self.mail,
|
|
|
|
'scrobblingEnabled': self.lastfm_session is not None and self.lastfm_status,
|
|
|
|
'adminRole': self.admin,
|
|
|
|
'settingsRole': True,
|
|
|
|
'downloadRole': True,
|
|
|
|
'uploadRole': False,
|
2013-06-25 20:18:54 +00:00
|
|
|
'playlistRole': True,
|
2013-06-18 14:12:35 +00:00
|
|
|
'coverArtRole': False,
|
|
|
|
'commentRole': False,
|
|
|
|
'podcastRole': False,
|
|
|
|
'streamRole': True,
|
|
|
|
'jukeboxRole': False,
|
|
|
|
'shareRole': False
|
|
|
|
}
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
class Folder(Base):
|
2012-10-13 12:53:09 +00:00
|
|
|
__tablename__ = 'folder'
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
2012-10-21 14:18:35 +00:00
|
|
|
root = Column(Boolean, default = False)
|
2013-07-15 18:30:33 +00:00
|
|
|
name = Column(String(256))
|
|
|
|
path = Column(String(4096)) # should be unique, but mysql don't like such large columns
|
2012-12-01 18:52:41 +00:00
|
|
|
created = Column(DateTime, default = now)
|
2012-11-11 20:39:26 +00:00
|
|
|
has_cover_art = Column(Boolean, default = False)
|
2013-06-14 16:46:01 +00:00
|
|
|
last_scan = Column(Integer, default = 0)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
parent_id = Column(UUID, ForeignKey('folder.id'), nullable = True)
|
|
|
|
children = relationship('Folder', backref = backref('parent', remote_side = [ id ]))
|
|
|
|
|
2013-06-13 16:44:56 +00:00
|
|
|
def as_subsonic_child(self, user):
|
2012-11-10 23:01:52 +00:00
|
|
|
info = {
|
|
|
|
'id': str(self.id),
|
|
|
|
'isDir': True,
|
|
|
|
'title': self.name,
|
2013-06-12 19:29:42 +00:00
|
|
|
'album': self.name,
|
2012-12-01 18:52:41 +00:00
|
|
|
'created': self.created.isoformat()
|
2012-11-10 23:01:52 +00:00
|
|
|
}
|
|
|
|
if not self.root:
|
|
|
|
info['parent'] = str(self.parent_id)
|
|
|
|
info['artist'] = self.parent.name
|
2012-11-11 20:39:26 +00:00
|
|
|
if self.has_cover_art:
|
|
|
|
info['coverArt'] = str(self.id)
|
2012-11-10 23:01:52 +00:00
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
starred = StarredFolder.query.get((user.id, self.id))
|
2013-06-13 16:44:56 +00:00
|
|
|
if starred:
|
|
|
|
info['starred'] = starred.date.isoformat()
|
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
rating = RatingFolder.query.get((user.id, self.id))
|
|
|
|
if rating:
|
|
|
|
info['userRating'] = rating.rating
|
|
|
|
avgRating = RatingFolder.query.filter(RatingFolder.rated_id == self.id).value(func.avg(RatingFolder.rating))
|
|
|
|
if avgRating:
|
|
|
|
info['averageRating'] = avgRating
|
|
|
|
|
2012-11-10 23:01:52 +00:00
|
|
|
return info
|
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
class Artist(UniqueMixin, Base):
|
2012-10-13 12:53:09 +00:00
|
|
|
__tablename__ = 'artist'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
2013-11-03 20:46:19 +00:00
|
|
|
name = Column(String(256), unique = True, nullable=False)
|
2012-10-13 22:37:06 +00:00
|
|
|
albums = relationship('Album', backref = 'artist')
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
@classmethod
|
|
|
|
def unique_hash(cls, name):
|
|
|
|
return name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def unique_filter(cls, query, name):
|
|
|
|
return query.filter(Artist.name == name)
|
|
|
|
|
2013-06-13 16:44:56 +00:00
|
|
|
def as_subsonic_artist(self, user):
|
|
|
|
info = {
|
2012-12-01 21:58:17 +00:00
|
|
|
'id': str(self.id),
|
|
|
|
'name': self.name,
|
|
|
|
# coverArt
|
|
|
|
'albumCount': len(self.albums)
|
|
|
|
}
|
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
starred = StarredArtist.query.get((user.id, self.id))
|
2013-06-13 16:44:56 +00:00
|
|
|
if starred:
|
|
|
|
info['starred'] = starred.date.isoformat()
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
class Album(Base):
|
|
|
|
__tablename__ = 'album'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
2013-07-15 18:30:33 +00:00
|
|
|
name = Column(String(256))
|
2012-10-13 12:53:09 +00:00
|
|
|
artist_id = Column(UUID, ForeignKey('artist.id'))
|
2012-10-13 22:37:06 +00:00
|
|
|
tracks = relationship('Track', backref = 'album')
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2013-06-13 16:44:56 +00:00
|
|
|
def as_subsonic_album(self, user):
|
2012-12-01 21:58:17 +00:00
|
|
|
info = {
|
|
|
|
'id': str(self.id),
|
|
|
|
'name': self.name,
|
|
|
|
'artist': self.artist.name,
|
|
|
|
'artistId': str(self.artist_id),
|
|
|
|
'songCount': len(self.tracks),
|
|
|
|
'duration': sum(map(lambda t: t.duration, self.tracks)),
|
|
|
|
'created': min(map(lambda t: t.created, self.tracks)).isoformat()
|
|
|
|
}
|
|
|
|
if self.tracks[0].folder.has_cover_art:
|
|
|
|
info['coverArt'] = str(self.tracks[0].folder_id)
|
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
starred = StarredAlbum.query.get((user.id, self.id))
|
2013-06-13 16:44:56 +00:00
|
|
|
if starred:
|
|
|
|
info['starred'] = starred.date.isoformat()
|
|
|
|
|
2012-12-01 21:58:17 +00:00
|
|
|
return info
|
|
|
|
|
|
|
|
def sort_key(self):
|
|
|
|
year = min(map(lambda t: t.year if t.year else 9999, self.tracks))
|
|
|
|
return '%i%s' % (year, self.name.lower())
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
class Track(Base):
|
|
|
|
__tablename__ = 'track'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
|
|
|
disc = Column(Integer)
|
|
|
|
number = Column(Integer)
|
2013-07-15 18:30:33 +00:00
|
|
|
title = Column(String(256))
|
2012-10-20 16:05:37 +00:00
|
|
|
year = Column(Integer, nullable = True)
|
2013-07-15 18:30:33 +00:00
|
|
|
genre = Column(String(256), nullable = True)
|
2012-10-20 16:05:37 +00:00
|
|
|
duration = Column(Integer)
|
2012-10-13 12:53:09 +00:00
|
|
|
album_id = Column(UUID, ForeignKey('album.id'))
|
2012-10-20 16:05:37 +00:00
|
|
|
bitrate = Column(Integer)
|
2012-10-13 09:29:48 +00:00
|
|
|
|
2013-07-15 18:30:33 +00:00
|
|
|
path = Column(String(4096)) # should be unique, but mysql don't like such large columns
|
2013-10-14 16:36:45 +00:00
|
|
|
content_type = Column(String(32))
|
2012-12-01 18:52:41 +00:00
|
|
|
created = Column(DateTime, default = now)
|
|
|
|
last_modification = Column(Integer)
|
|
|
|
|
2013-06-07 18:35:21 +00:00
|
|
|
play_count = Column(Integer, default = 0)
|
|
|
|
last_play = Column(DateTime, nullable = True)
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
root_folder_id = Column(UUID, ForeignKey('folder.id'))
|
|
|
|
root_folder = relationship('Folder', primaryjoin = Folder.id == root_folder_id)
|
2012-10-14 11:07:02 +00:00
|
|
|
folder_id = Column(UUID, ForeignKey('folder.id'))
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = relationship('Folder', primaryjoin = Folder.id == folder_id, backref = 'tracks')
|
2012-10-14 11:07:02 +00:00
|
|
|
|
2013-06-13 16:44:56 +00:00
|
|
|
def as_subsonic_child(self, user):
|
2012-10-20 16:05:37 +00:00
|
|
|
info = {
|
|
|
|
'id': str(self.id),
|
2012-10-21 14:18:35 +00:00
|
|
|
'parent': str(self.folder.id),
|
2012-10-20 16:05:37 +00:00
|
|
|
'isDir': False,
|
|
|
|
'title': self.title,
|
|
|
|
'album': self.album.name,
|
|
|
|
'artist': self.album.artist.name,
|
|
|
|
'track': self.number,
|
|
|
|
'size': os.path.getsize(self.path),
|
2013-10-14 16:36:45 +00:00
|
|
|
'contentType': self.content_type,
|
|
|
|
'suffix': self.suffix(),
|
2012-10-20 16:05:37 +00:00
|
|
|
'duration': self.duration,
|
|
|
|
'bitRate': self.bitrate,
|
2012-10-21 14:18:35 +00:00
|
|
|
'path': self.path[len(self.root_folder.path) + 1:],
|
2012-10-20 16:05:37 +00:00
|
|
|
'isVideo': False,
|
|
|
|
'discNumber': self.disc,
|
2012-12-01 18:52:41 +00:00
|
|
|
'created': self.created.isoformat(),
|
2012-10-20 16:05:37 +00:00
|
|
|
'albumId': str(self.album.id),
|
|
|
|
'artistId': str(self.album.artist.id),
|
|
|
|
'type': 'music'
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.year:
|
|
|
|
info['year'] = self.year
|
|
|
|
if self.genre:
|
|
|
|
info['genre'] = self.genre
|
2012-11-11 20:39:26 +00:00
|
|
|
if self.folder.has_cover_art:
|
|
|
|
info['coverArt'] = str(self.folder_id)
|
2012-10-20 16:05:37 +00:00
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
starred = StarredTrack.query.get((user.id, self.id))
|
2013-06-13 16:44:56 +00:00
|
|
|
if starred:
|
|
|
|
info['starred'] = starred.date.isoformat()
|
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
rating = RatingTrack.query.get((user.id, self.id))
|
|
|
|
if rating:
|
|
|
|
info['userRating'] = rating.rating
|
|
|
|
avgRating = RatingTrack.query.filter(RatingTrack.rated_id == self.id).value(func.avg(RatingTrack.rating))
|
|
|
|
if avgRating:
|
|
|
|
info['averageRating'] = avgRating
|
|
|
|
|
2013-11-03 20:46:19 +00:00
|
|
|
if self.suffix() == 'flac':
|
|
|
|
info['transcodedContentType'] = 'audio/ogg'
|
|
|
|
info['transcodedSuffix'] = 'ogg'
|
2012-10-20 16:05:37 +00:00
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
def duration_str(self):
|
|
|
|
ret = '%02i:%02i' % ((self.duration % 3600) / 60, self.duration % 60)
|
|
|
|
if self.duration >= 3600:
|
|
|
|
ret = '%02i:%s' % (self.duration / 3600, ret)
|
|
|
|
return ret
|
|
|
|
|
2013-10-14 16:36:45 +00:00
|
|
|
def suffix(self):
|
|
|
|
return os.path.splitext(self.path)[1][1:].lower()
|
|
|
|
|
2012-11-10 23:01:52 +00:00
|
|
|
def sort_key(self):
|
2013-11-03 20:46:19 +00:00
|
|
|
return (self.album.artist.name + self.album.name + ("%02i" % self.disc) + ("%02i" % self.number) + str(self.title)).lower()
|
2012-11-10 23:01:52 +00:00
|
|
|
|
2013-06-12 19:29:42 +00:00
|
|
|
class StarredFolder(Base):
|
|
|
|
__tablename__ = 'starred_folder'
|
|
|
|
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'), primary_key = True)
|
|
|
|
starred_id = Column(UUID, ForeignKey('folder.id'), primary_key = True)
|
|
|
|
date = Column(DateTime, default = now)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
starred = relationship('Folder')
|
|
|
|
|
|
|
|
class StarredArtist(Base):
|
|
|
|
__tablename__ = 'starred_artist'
|
|
|
|
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'), primary_key = True)
|
|
|
|
starred_id = Column(UUID, ForeignKey('artist.id'), primary_key = True)
|
|
|
|
date = Column(DateTime, default = now)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
starred = relationship('Artist')
|
|
|
|
|
|
|
|
class StarredAlbum(Base):
|
|
|
|
__tablename__ = 'starred_album'
|
|
|
|
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'), primary_key = True)
|
|
|
|
starred_id = Column(UUID, ForeignKey('album.id'), primary_key = True)
|
|
|
|
date = Column(DateTime, default = now)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
starred = relationship('Album')
|
|
|
|
|
|
|
|
class StarredTrack(Base):
|
|
|
|
__tablename__ = 'starred_track'
|
|
|
|
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'), primary_key = True)
|
|
|
|
starred_id = Column(UUID, ForeignKey('track.id'), primary_key = True)
|
|
|
|
date = Column(DateTime, default = now)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
starred = relationship('Track')
|
|
|
|
|
2013-06-14 10:25:15 +00:00
|
|
|
class RatingFolder(Base):
|
|
|
|
__tablename__ = 'rating_folder'
|
|
|
|
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'), primary_key = True)
|
|
|
|
rated_id = Column(UUID, ForeignKey('folder.id'), primary_key = True)
|
|
|
|
rating = Column(Integer)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
rated = relationship('Folder')
|
|
|
|
|
|
|
|
class RatingTrack(Base):
|
|
|
|
__tablename__ = 'rating_track'
|
|
|
|
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'), primary_key = True)
|
|
|
|
rated_id = Column(UUID, ForeignKey('track.id'), primary_key = True)
|
|
|
|
rating = Column(Integer)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
rated = relationship('Track')
|
|
|
|
|
2013-06-14 16:46:01 +00:00
|
|
|
class ChatMessage(Base):
|
|
|
|
__tablename__ = 'chat_message'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'))
|
|
|
|
time = Column(Integer, default = lambda: int(time.time()))
|
2013-07-15 18:30:33 +00:00
|
|
|
message = Column(String(512))
|
2013-06-14 16:46:01 +00:00
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
|
|
|
|
def responsize(self):
|
|
|
|
return {
|
|
|
|
'username': self.user.name,
|
|
|
|
'time': self.time * 1000,
|
|
|
|
'message': self.message
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:07:49 +00:00
|
|
|
playlist_track_assoc = Table('playlist_track', Base.metadata,
|
|
|
|
Column('playlist_id', UUID, ForeignKey('playlist.id')),
|
|
|
|
Column('track_id', UUID, ForeignKey('track.id'))
|
|
|
|
)
|
|
|
|
|
|
|
|
class Playlist(Base):
|
|
|
|
__tablename__ = 'playlist'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
|
|
|
user_id = Column(UUID, ForeignKey('user.id'))
|
2013-07-15 18:30:33 +00:00
|
|
|
name = Column(String(256))
|
|
|
|
comment = Column(String(256), nullable = True)
|
2013-06-25 20:07:49 +00:00
|
|
|
public = Column(Boolean, default = False)
|
|
|
|
created = Column(DateTime, default = now)
|
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
tracks = relationship('Track', secondary = playlist_track_assoc)
|
|
|
|
|
2013-06-28 10:39:46 +00:00
|
|
|
def as_subsonic_playlist(self, user):
|
2013-06-25 20:07:49 +00:00
|
|
|
info = {
|
|
|
|
'id': str(self.id),
|
2013-06-28 10:39:46 +00:00
|
|
|
'name': self.name if self.user_id == user.id else '[%s] %s' % (self.user.name, self.name),
|
2013-06-25 20:07:49 +00:00
|
|
|
'owner': self.user.name,
|
|
|
|
'public': self.public,
|
|
|
|
'songCount': len(self.tracks),
|
|
|
|
'duration': sum(map(lambda t: t.duration, self.tracks)),
|
|
|
|
'created': self.created.isoformat()
|
|
|
|
}
|
|
|
|
if self.comment:
|
|
|
|
info['comment'] = self.comment
|
|
|
|
return info
|
|
|
|
|
2012-10-13 09:29:48 +00:00
|
|
|
def init_db():
|
2012-10-13 10:29:37 +00:00
|
|
|
Base.metadata.create_all(bind = engine)
|
|
|
|
|
|
|
|
def recreate_db():
|
2012-10-13 09:29:48 +00:00
|
|
|
Base.metadata.drop_all(bind = engine)
|
|
|
|
Base.metadata.create_all(bind = engine)
|