[odrs-web/oscp] Ensure votes get deleted when the review is deleted



commit 2380df8d21cfaf24f028be4be7a334c94d6b27e3
Author: Richard Hughes <richard hughsie com>
Date:   Wed Jul 3 12:13:33 2019 +0100

    Ensure votes get deleted when the review is deleted

 app_data/cron.py                 |  4 ++--
 app_data/odrs/models.py          |  3 +++
 app_data/odrs/tests/odrs_test.py | 30 ++++++++++++++++++++++++------
 3 files changed, 29 insertions(+), 8 deletions(-)
---
diff --git a/app_data/cron.py b/app_data/cron.py
index 009bfb9..31fd7fc 100755
--- a/app_data/cron.py
+++ b/app_data/cron.py
@@ -16,9 +16,9 @@ from odrs import db
 from odrs.models import Review
 from odrs.util import _get_rating_for_app_id
 
-def _auto_delete():
+def _auto_delete(days=31):
 
-    since = datetime.datetime.now() - datetime.timedelta(days=31)
+    since = datetime.datetime.now() - datetime.timedelta(days=days)
     reviews = db.session.query(Review).\
                     filter(Review.date_deleted != None).\
                     filter(Review.date_deleted < since).\
diff --git a/app_data/odrs/models.py b/app_data/odrs/models.py
index 040b6c6..7d9303d 100644
--- a/app_data/odrs/models.py
+++ b/app_data/odrs/models.py
@@ -117,6 +117,9 @@ class Review(db.Model):
     reported = Column(Integer, default=0)
 
     user = relationship('User', back_populates='reviews')
+    votes = relationship('Vote',
+                         back_populates='review',
+                         cascade='all,delete-orphan')
 
     def __init__(self):
         self.app_id = None
diff --git a/app_data/odrs/tests/odrs_test.py b/app_data/odrs/tests/odrs_test.py
index 28b7ca9..e739d4c 100644
--- a/app_data/odrs/tests/odrs_test.py
+++ b/app_data/odrs/tests/odrs_test.py
@@ -238,13 +238,21 @@ class OdrsTest(unittest.TestCase):
         assert b'Incorrect password' in rv.data, rv.data
 
     @staticmethod
-    def run_cron(fn='test.json'):
+    def run_cron_regenerate_ratings(fn='test.json'):
 
         from odrs import app
         from cron import _regenerate_ratings
         with app.test_request_context():
             _regenerate_ratings(fn)
 
+    @staticmethod
+    def run_cron_auto_delete():
+
+        from odrs import app
+        from cron import _auto_delete
+        with app.test_request_context():
+            _auto_delete(0)
+
     def test_nologin_required(self):
 
         # all these are viewable without being logged in
@@ -382,6 +390,13 @@ class OdrsTest(unittest.TestCase):
         rv = self._review_fetch(app_id='inkscape.desktop')
         assert b'vote_id": 1' in rv.data, rv.data
 
+        # delete review, hopefully deleting vote too
+        rv = self._api_review_delete()
+        assert b'removed review #1' in rv.data, rv.data
+        self.run_cron_auto_delete()
+        rv = self._review_fetch(app_id='inkscape.desktop')
+        assert b'vote_id' not in rv.data, rv.data
+
     def test_api_report(self):
 
         # submit and verify
@@ -424,6 +439,13 @@ class OdrsTest(unittest.TestCase):
         assert b'star5": 2' in rv.data, rv.data
         assert b'total": 2' in rv.data, rv.data
 
+    def _api_review_delete(self):
+        data = {'review_id': 1,
+                'app_id': 'inkscape.desktop',
+                'user_hash': self.user_hash,
+                'user_skey': _get_user_key(self.user_hash, 'inkscape.desktop')}
+        return self.app.post('/1.0/reviews/api/remove', data=json.dumps(data))
+
     def test_api_remove(self):
 
         self.review_submit()
@@ -453,11 +475,7 @@ class OdrsTest(unittest.TestCase):
         assert b'invalid user_skey' in rv.data, rv.data
 
         # delete a review
-        data = {'review_id': 1,
-                'app_id': 'inkscape.desktop',
-                'user_hash': self.user_hash,
-                'user_skey': _get_user_key(self.user_hash, 'inkscape.desktop')}
-        rv = self.app.post('/1.0/reviews/api/remove', data=json.dumps(data))
+        rv = self._api_review_delete()
         assert b'removed review #1' in rv.data, rv.data
 
     def test_api_submit(self):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]