src/pyams_content/features/glossary/task.py
changeset 1195 9870e7467087
equal deleted inserted replaced
1194:d4ae54d8fe17 1195:9870e7467087
       
     1 #
       
     2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 from pyramid.events import subscriber
       
    16 from zope.interface import implementer
       
    17 from zope.lifecycleevent import IObjectAddedEvent, IObjectModifiedEvent
       
    18 
       
    19 from pyams_content.component.theme import ITagsManager
       
    20 from pyams_content.features.glossary import get_glossary_automaton
       
    21 from pyams_content.features.glossary.interfaces import IGlossaryUpdaterTask
       
    22 from pyams_content.root import ISiteRoot
       
    23 from pyams_scheduler.interfaces import IScheduler
       
    24 from pyams_scheduler.task import Task
       
    25 from pyams_thesaurus.interfaces.term import IThesaurusTerm
       
    26 from pyams_utils.registry import get_utility
       
    27 from pyams_utils.request import check_request
       
    28 from pyams_utils.traversing import get_parent
       
    29 
       
    30 
       
    31 @implementer(IGlossaryUpdaterTask)
       
    32 class GlossaryUpdaterTask(Task):
       
    33     """Glossary updater task"""
       
    34 
       
    35     settings_view_name = None
       
    36 
       
    37     def run(self, report):
       
    38         root = get_parent(self, ISiteRoot)
       
    39         get_glossary_automaton(root)
       
    40 
       
    41 
       
    42 @subscriber(IObjectAddedEvent, context_selector=IThesaurusTerm)
       
    43 @subscriber(IObjectModifiedEvent, context_selector=IThesaurusTerm)
       
    44 def handle_updated_thesaurus_term(event):
       
    45     """Reset glossary automaton on term update"""
       
    46     request = check_request()
       
    47     tags_manager = ITagsManager(request.root)
       
    48     if not tags_manager.enable_glossary:
       
    49         return
       
    50     scheduler = get_utility(IScheduler)
       
    51     for task in scheduler.values():
       
    52         if IGlossaryUpdaterTask.providedBy(task):
       
    53             task.launch()
       
    54             break