src/pyams_content/workflow/task.py
changeset 128 6cdea4a15bfd
child 277 9649f8ce3b1c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/workflow/task.py	Mon Sep 11 14:43:43 2017 +0200
@@ -0,0 +1,67 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+from datetime import datetime
+
+# import interfaces
+from hypatia.interfaces import ICatalog
+from pyams_content.shared.common.interfaces import ISharedTool
+from pyams_content.workflow.interfaces import IContentArchiverTask
+from pyams_i18n.interfaces import II18n
+from pyams_sequence.interfaces import ISequentialIdInfo
+from pyams_workflow.interfaces import IWorkflow, IWorkflowInfo
+
+# import packages
+from hypatia.catalog import CatalogQuery
+from hypatia.query import Eq, Lt, Any
+from pyams_catalog.query import CatalogResultSet
+from pyams_scheduler.task import Task
+from pyams_utils.registry import get_utility, get_all_utilities_registered_for
+from pyams_utils.request import check_request
+from pyams_utils.timezone import gmtime
+from zope.interface import implementer
+
+from pyams_content import _
+
+
+@implementer(IContentArchiverTask)
+class ContentArchiverTask(Task):
+    """"Content archiver task"""
+
+    settings_view_name = None
+
+    def run(self, report):
+        request = check_request()
+        translate = request.localizer.translate
+        catalog = get_utility(ICatalog)
+        now = gmtime(datetime.utcnow())
+        has_retired = False
+        for tool in get_all_utilities_registered_for(ISharedTool):
+            workflow = IWorkflow(tool)
+            params = Eq(catalog['content_type'], tool.shared_content_type) & \
+                     Any(catalog['workflow_state'], workflow.published_states) & \
+                     Lt(catalog['expiration_date'], now)
+            for content in CatalogResultSet(CatalogQuery(catalog).query(params)):
+                if not has_retired:
+                    report.write(translate(_("Automatic contents withdrawal:\n")))
+                    has_retired = True
+                info = IWorkflowInfo(content)
+                info.fire_transition_toward(workflow.auto_retired_state,
+                                            check_security=False,
+                                            principal='system:internal')
+                info.fire_automatic()
+                report.write("- {0} ({1})\n".format(II18n(content).query_attribute('title', request=request),
+                                                    ISequentialIdInfo(content).hex_oid))