You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2020/08/21 20:08:30 UTC

[allura] 02/03: [#8374] script/task for clearing old notifications

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8374
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 1dcc9e291520d968bba7d26d060df91a1f200da1
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Fri Aug 21 16:07:34 2020 -0400

    [#8374] script/task for clearing old notifications
---
 Allura/allura/scripts/clear_old_notifications.py | 53 ++++++++++++++++++++++++
 Allura/allura/tests/scripts/test_misc_scripts.py | 47 +++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/Allura/allura/scripts/clear_old_notifications.py b/Allura/allura/scripts/clear_old_notifications.py
new file mode 100644
index 0000000..5a2218a
--- /dev/null
+++ b/Allura/allura/scripts/clear_old_notifications.py
@@ -0,0 +1,53 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+from __future__ import absolute_import, division, print_function, unicode_literals
+
+from datetime import datetime, timedelta
+
+import logging
+
+import argparse
+
+from allura.scripts import ScriptTask
+from allura import model as M
+
+log = logging.getLogger('allura.scripts.clear_old_notifications')
+
+
+class ClearOldNotifications(ScriptTask):
+
+    @classmethod
+    def parser(cls):
+        parser = argparse.ArgumentParser(description="Remove old temporary notifications")
+        parser.add_argument('--back-days', dest='back_days', type=float, default=60,
+                            help='How many days back to clear from (keeps newer notifications)')
+        return parser
+
+    @classmethod
+    def execute(cls, options):
+        before = datetime.utcnow() - timedelta(days=options.back_days)
+        M.Notification.query.remove({
+            'pubdate': {'$lt': before}
+        })
+
+
+def get_parser():
+    return ClearOldNotifications.parser()
+
+
+if __name__ == '__main__':
+    ClearOldNotifications.main()
diff --git a/Allura/allura/tests/scripts/test_misc_scripts.py b/Allura/allura/tests/scripts/test_misc_scripts.py
new file mode 100644
index 0000000..7075c8a
--- /dev/null
+++ b/Allura/allura/tests/scripts/test_misc_scripts.py
@@ -0,0 +1,47 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+from __future__ import unicode_literals
+from __future__ import absolute_import
+
+from bson import ObjectId
+from nose.tools import assert_equal
+
+from allura.scripts.clear_old_notifications import ClearOldNotifications
+from alluratest.controller import setup_basic_test
+from allura import model as M
+from ming.odm import session
+
+
+class TestClearOldNotifications(object):
+
+    def setUp(self):
+        setup_basic_test()
+
+    def run_script(self, options):
+        cls = ClearOldNotifications
+        opts = cls.parser().parse_args(options)
+        cls.execute(opts)
+
+    def test(self):
+        n = M.Notification(app_config_id=ObjectId(), neighborhood_id=ObjectId(), project_id=ObjectId(),
+                           tool_name='blah')
+        session(n).flush(n)
+        assert_equal(M.Notification.query.find().count(), 1)
+        self.run_script(['--back-days', '7'])
+        assert_equal(M.Notification.query.find().count(), 1)
+        self.run_script(['--back-days', '0'])
+        assert_equal(M.Notification.query.find().count(), 0)