You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.4 KiB

from couchpotato.core.helpers.encoding import toUnicode
from elixir.entity import Entity
from elixir.fields import Field
from elixir.options import options_defaults, using_options
from elixir.relationships import ManyToMany, OneToMany, ManyToOne
13 years ago
from sqlalchemy.types import Integer, Unicode, UnicodeText, Boolean, String, \
TypeDecorator
import json
import time
options_defaults["shortnames"] = True
# We would like to be able to create this schema in a specific database at
# will, so we can test it easily.
# Make elixir not bind to any session to make this possible.
#
# http://elixir.ematia.de/trac/wiki/Recipes/MultipleDatabasesOneMetadata
__session__ = None
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
14 years ago
class JsonType(TypeDecorator):
impl = UnicodeText
def process_bind_param(self, value, dialect):
return toUnicode(json.dumps(value, cls = SetEncoder))
def process_result_value(self, value, dialect):
return json.loads(value if value else '{}')
14 years ago
class Movie(Entity):
"""Movie Resource a movie could have multiple releases
The files belonging to the movie object are global for the whole movie
such as trailers, nfo, thumbnails"""
last_edit = Field(Integer, default = lambda: int(time.time()))
14 years ago
14 years ago
library = ManyToOne('Library')
status = ManyToOne('Status')
14 years ago
profile = ManyToOne('Profile')
13 years ago
releases = OneToMany('Release', cascade = 'all, delete-orphan')
files = ManyToMany('File', cascade = 'all, delete-orphan', single_parent = True)
class Library(Entity):
14 years ago
""""""
year = Field(Integer)
identifier = Field(String(20), index = True)
plot = Field(UnicodeText)
tagline = Field(UnicodeText(255))
info = Field(JsonType)
14 years ago
status = ManyToOne('Status')
13 years ago
movies = OneToMany('Movie', cascade = 'all, delete-orphan')
titles = OneToMany('LibraryTitle', cascade = 'all, delete-orphan')
files = ManyToMany('File', cascade = 'all, delete-orphan', single_parent = True)
14 years ago
class LibraryTitle(Entity):
""""""
using_options(order_by = '-default')
14 years ago
title = Field(Unicode)
simple_title = Field(Unicode, index = True)
default = Field(Boolean, index = True)
14 years ago
language = OneToMany('Language')
libraries = ManyToOne('Library')
class Language(Entity):
""""""
identifier = Field(String(20), index = True)
14 years ago
label = Field(Unicode)
titles = ManyToOne('LibraryTitle')
class Release(Entity):
"""Logically groups all files that belong to a certain release, such as
14 years ago
parts of a movie, subtitles."""
identifier = Field(String(100), index = True)
14 years ago
movie = ManyToOne('Movie')
status = ManyToOne('Status')
quality = ManyToOne('Quality')
13 years ago
files = ManyToMany('File', cascade = 'all, delete-orphan', single_parent = True)
history = OneToMany('History', cascade = 'all, delete-orphan')
info = OneToMany('ReleaseInfo', cascade = 'all, delete-orphan')
class ReleaseInfo(Entity):
"""Properties that can be bound to a file for off-line usage"""
identifier = Field(String(50), index = True)
value = Field(Unicode(255), nullable = False)
release = ManyToOne('Release')
14 years ago
class Status(Entity):
"""The status of a release, such as Downloaded, Deleted, Wanted etc"""
identifier = Field(String(20), unique = True)
14 years ago
label = Field(Unicode(20))
14 years ago
releases = OneToMany('Release')
14 years ago
movies = OneToMany('Movie')
14 years ago
class Quality(Entity):
"""Quality name of a release, DVD, 720P, DVD-Rip etc"""
14 years ago
using_options(order_by = 'order')
14 years ago
identifier = Field(String(20), unique = True)
14 years ago
label = Field(Unicode(20))
order = Field(Integer, index = True)
14 years ago
size_min = Field(Integer)
size_max = Field(Integer)
14 years ago
releases = OneToMany('Release')
14 years ago
profile_types = OneToMany('ProfileType')
14 years ago
14 years ago
class Profile(Entity):
""""""
14 years ago
using_options(order_by = 'order')
14 years ago
label = Field(Unicode(50))
order = Field(Integer, index = True)
14 years ago
core = Field(Boolean)
hide = Field(Boolean)
14 years ago
movie = OneToMany('Movie')
14 years ago
types = OneToMany('ProfileType', cascade = 'all, delete-orphan')
14 years ago
14 years ago
class ProfileType(Entity):
""""""
14 years ago
using_options(order_by = 'order')
14 years ago
order = Field(Integer, index = True)
14 years ago
finish = Field(Boolean)
14 years ago
wait_for = Field(Integer)
14 years ago
quality = ManyToOne('Quality')
14 years ago
profile = ManyToOne('Profile')
class File(Entity):
"""File that belongs to a release."""
14 years ago
path = Field(Unicode(255), nullable = False, unique = True)
14 years ago
part = Field(Integer, default = 1)
available = Field(Boolean)
14 years ago
type = ManyToOne('FileType')
14 years ago
properties = OneToMany('FileProperty')
14 years ago
history = OneToMany('RenameHistory')
movie = ManyToMany('Movie')
release = ManyToMany('Release')
library = ManyToMany('Library')
class FileType(Entity):
"""Types could be trailer, subtitle, movie, partial movie etc."""
14 years ago
identifier = Field(String(20), unique = True)
14 years ago
type = Field(Unicode(20))
14 years ago
name = Field(Unicode(50), nullable = False)
files = OneToMany('File')
14 years ago
class FileProperty(Entity):
"""Properties that can be bound to a file for off-line usage"""
identifier = Field(String(20), index = True)
14 years ago
value = Field(Unicode(255), nullable = False)
file = ManyToOne('File')
class History(Entity):
"""History of actions that are connected to a certain release,
such as, renamed to, downloaded, deleted, download subtitles etc"""
added = Field(Integer, default = lambda: int(time.time()))
message = Field(UnicodeText)
type = Field(Unicode(50))
14 years ago
release = ManyToOne('Release')
class RenameHistory(Entity):
"""Remembers from where to where files have been moved."""
14 years ago
14 years ago
old = Field(Unicode(255))
new = Field(Unicode(255))
14 years ago
file = ManyToOne('File')
class Notification(Entity):
using_options(order_by = 'added')
added = Field(Integer, default = lambda: int(time.time()))
read = Field(Boolean, default = False)
message = Field(Unicode(255))
data = Field(JsonType)
class Folder(Entity):
"""Renamer destination folders."""
path = Field(Unicode(255))
label = Field(Unicode(255))
13 years ago
class Properties(Entity):
identifier = Field(String(50), index = True)
value = Field(Unicode(255), nullable = False)
def setup():
"""Setup the database and create the tables that don't exists yet"""
from elixir import setup_all, create_all
from couchpotato import get_engine
setup_all()
create_all(get_engine())