2012-10-13 09:29:48 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import config
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
from sqlalchemy import create_engine, Column, ForeignKey
|
2012-10-14 11:07:02 +00:00
|
|
|
from sqlalchemy import Integer, String, Boolean, DateTime, Time
|
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
|
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
from sqlalchemy.types import TypeDecorator
|
2012-10-13 09:29:48 +00:00
|
|
|
from sqlalchemy import BINARY
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2012-10-14 16:06:48 +00:00
|
|
|
import uuid, datetime
|
2012-10-13 09:29:48 +00:00
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
class UUID(TypeDecorator):
|
2012-10-13 09:29:48 +00:00
|
|
|
impl = BINARY
|
2012-10-13 12:53:09 +00:00
|
|
|
|
2012-10-13 09:29:48 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.impl.length = 16
|
2012-10-13 12:53:09 +00:00
|
|
|
TypeDecorator.__init__(self, length = self.impl.length)
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
def process_bind_param(self, value, dialect = None):
|
|
|
|
if value and isinstance(value, uuid.UUID):
|
|
|
|
return value.bytes
|
|
|
|
if value and not isinstance(value, uuid.UUID):
|
|
|
|
raise ValueError, 'value %s is not a valid uuid.UUID' % value
|
|
|
|
return None
|
|
|
|
|
|
|
|
def process_result_value(self, value, dialect = None):
|
|
|
|
if value:
|
|
|
|
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)
|
|
|
|
|
|
|
|
engine = create_engine(config.get('DATABASE_URI'), convert_unicode = True)
|
2012-10-13 10:29:37 +00:00
|
|
|
session = scoped_session(sessionmaker(autocommit = False, 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()
|
|
|
|
name = Column(String, unique = True)
|
|
|
|
mail = Column(String)
|
|
|
|
password = Column(String(40))
|
|
|
|
salt = Column(String(6))
|
|
|
|
admin = Column(Boolean)
|
|
|
|
|
|
|
|
class MusicFolder(Base):
|
2012-10-13 12:53:09 +00:00
|
|
|
__tablename__ = 'folder'
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
|
|
|
name = Column(String, unique = True)
|
|
|
|
path = Column(String)
|
2012-10-14 16:06:48 +00:00
|
|
|
last_scan = Column(DateTime, default = datetime.datetime.min)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
class Artist(Base):
|
|
|
|
__tablename__ = 'artist'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
2012-10-13 22:37:06 +00:00
|
|
|
name = Column(String, unique = True)
|
|
|
|
albums = relationship('Album', backref = 'artist')
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
class Album(Base):
|
|
|
|
__tablename__ = 'album'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
|
|
|
name = Column(String)
|
|
|
|
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
|
|
|
|
|
|
|
class Track(Base):
|
|
|
|
__tablename__ = 'track'
|
|
|
|
|
|
|
|
id = UUID.gen_id_column()
|
|
|
|
disc = Column(Integer)
|
|
|
|
number = Column(Integer)
|
|
|
|
title = Column(String)
|
|
|
|
duration = Column(Time)
|
|
|
|
album_id = Column(UUID, ForeignKey('album.id'))
|
2012-10-13 22:37:06 +00:00
|
|
|
path = Column(String, unique = True)
|
2012-10-13 09:29:48 +00:00
|
|
|
|
2012-10-14 11:07:02 +00:00
|
|
|
folder_id = Column(UUID, ForeignKey('folder.id'))
|
|
|
|
folder = relationship('MusicFolder')
|
|
|
|
|
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)
|
|
|
|
|