2015-01-12 10:53:00 +01:00

142 lines
4.8 KiB
Python

# This file is part of Indico.
# Copyright (C) 2002 - 2014 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from flask_pluginengine import depends, trim_docstring
from indico.core.plugins import IndicoPlugin, PluginCategory
from indico.util.date_time import now_utc
from indico.util.decorators import classproperty
from MaKaC.conference import CategoryManager
from indico_livesync.forms import AgentForm
from indico_livesync.models.queue import LiveSyncQueueEntry
from indico_livesync.plugin import LiveSyncPlugin
@depends('livesync')
class LiveSyncPluginBase(IndicoPlugin): # pragma: no cover
"""Base class for livesync plugins"""
#: dict containing the backend(s) provided by the plugin; the keys are unique identifiers
backend_classes = None
category = PluginCategory.synchronization
def init(self):
super(LiveSyncPluginBase, self).init()
for name, backend_class in self.backend_classes.iteritems():
assert backend_class.plugin is None
backend_class.plugin = type(self)
LiveSyncPlugin.instance.register_backend_class(name, backend_class)
class LiveSyncBackendBase(object):
"""Base class for livesync backends"""
#: the plugin containing the agent
plugin = None # set automatically when the agent is registered
#: the Uploader to use. only needed if run and run_initial_export are not overridden
uploader = None
#: the form used when creating/editing the agent
form = AgentForm
@classproperty
@classmethod
def title(cls):
parts = trim_docstring(cls.__doc__).split('\n', 1)
return parts[0].strip()
@classproperty
@classmethod
def description(cls):
parts = trim_docstring(cls.__doc__).split('\n', 1)
try:
return parts[1].strip()
except IndexError:
return 'no description available'
def __init__(self, agent, task=None):
"""
:param agent: a `LiveSyncAgent` instance
:param task: a `LiveSyncTask` instance if running as a task
"""
self.agent = agent
self.task = task
self.excluded_categories = self._get_excluded_categories()
def _get_excluded_categories(self):
todo = {x['id'] for x in LiveSyncPlugin.settings.get('excluded_categories')}
excluded = set()
while todo:
category_id = todo.pop()
try:
category = CategoryManager().getById(category_id)
except KeyError:
continue
excluded.add(category.getId())
todo.update(category.subcategories)
return excluded
def _is_entry_excluded(self, entry):
if entry.type == 'category':
return entry.category_id in self.excluded_categories
else:
obj = entry.object
if not obj:
return False
category = obj.getConference().getOwner() if obj.getConference() else None
if category:
return category.getId() in self.excluded_categories
return False
def fetch_records(self, count=None):
query = (self.agent.queue
.filter_by(processed=False)
.order_by(LiveSyncQueueEntry.timestamp)
.limit(count))
return [entry for entry in query if not self._is_entry_excluded(entry)]
def update_last_run(self):
"""Updates the last run timestamp.
Don't forget to call this if you implement your own `run` method!
"""
self.agent.last_run = now_utc()
def run(self):
"""Runs the livesync export"""
if self.uploader is None: # pragma: no cover
raise NotImplementedError
records = self.fetch_records()
uploader = self.uploader(self)
uploader.run(records)
self.update_last_run()
def run_initial_export(self, events):
"""Runs the initial export.
This process is expected to take a very long time.
:param events: iterable of all events in this indico instance
"""
if self.uploader is None: # pragma: no cover
raise NotImplementedError
uploader = self.uploader(self)
uploader.run_initial(events)