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.

109 lines
2.7 KiB

14 years ago
from couchpotato import get_session
14 years ago
from couchpotato.api import addApiView
14 years ago
from couchpotato.core.event import addEvent
14 years ago
from couchpotato.core.helpers.encoding import toUnicode
14 years ago
from couchpotato.core.helpers.request import jsonified
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
14 years ago
from couchpotato.core.settings.model import Status
log = CPLog(__name__)
class StatusPlugin(Plugin):
14 years ago
statuses = {
'needs_update': 'Needs update',
14 years ago
'active': 'Active',
'done': 'Done',
'downloaded': 'Downloaded',
'wanted': 'Wanted',
'snatched': 'Snatched',
'failed': 'Failed',
14 years ago
'deleted': 'Deleted',
'ignored': 'Ignored',
'available': 'Available',
14 years ago
}
def __init__(self):
addEvent('status.add', self.add)
addEvent('status.get', self.add) # Alias for .add
14 years ago
addEvent('status.get_by_id', self.getById)
14 years ago
addEvent('status.all', self.all)
addEvent('app.initialize', self.fill)
14 years ago
addApiView('status.list', self.list, docs = {
'desc': 'Check for available update',
'return': {'type': 'object', 'example': """{
'success': True,
'list': array, statuses
}"""}
})
14 years ago
def list(self):
return jsonified({
'success': True,
'list': self.all()
})
14 years ago
def getById(self, id):
db = get_session()
status = db.query(Status).filter_by(id = id).first()
status_dict = status.to_dict()
#db.close()
return status_dict
14 years ago
14 years ago
def all(self):
db = get_session()
statuses = db.query(Status).all()
temp = []
for status in statuses:
s = status.to_dict()
temp.append(s)
#db.close()
14 years ago
return temp
def add(self, identifier):
db = get_session()
s = db.query(Status).filter_by(identifier = identifier).first()
if not s:
s = Status(
identifier = identifier,
label = toUnicode(identifier.capitalize())
14 years ago
)
db.add(s)
db.commit()
status_dict = s.to_dict()
#db.close()
return status_dict
14 years ago
def fill(self):
db = get_session()
for identifier, label in self.statuses.iteritems():
s = db.query(Status).filter_by(identifier = identifier).first()
if not s:
log.info('Creating status: %s', label)
14 years ago
s = Status(
identifier = identifier,
label = toUnicode(label)
)
db.add(s)
s.label = toUnicode(label)
14 years ago
db.commit()
#db.close()