Skip to content

Commit

Permalink
Purge project history on deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Glandos committed Mar 5, 2022
1 parent 09e583b commit a71f154
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ihatemoney/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,12 @@ def get_history(project, human_readable_names=True):
history.append(common_properties)

return sorted(history, key=history_sort_key, reverse=True)


def purge_history(project):
"""
Erase history linked to a project.
You must commit the purge after calling this function.
"""
for query in get_history_queries(project):
query.delete(synchronize_session="fetch")
5 changes: 5 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ def remove_member(self, member_id):
return person

def remove_project(self):
# We can't import at top level without circular dependencies
from ihatemoney.history import purge_history

db.session.delete(self)
# Purge AFTER delete to be sure to purge the deletion from history
purge_history(self)
db.session.commit()

def generate_token(self, token_type="auth"):
Expand Down
32 changes: 32 additions & 0 deletions ihatemoney/tests/history_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,38 @@ def test_double_bill_double_person_edit_second_no_web(self):
self.assertNotIn("owers", entry["prop_changed"])
self.assertEqual(len(history_list), 6)

def test_delete_history_with_project(self):
self.post_project("raclette", password="party")

# add participants
self.client.post("/raclette/members/add", data={"name": "zorglub"})

# add bill
self.client.post(
"/raclette/add",
data={
"date": "2016-12-31",
"what": "fromage à raclette",
"payer": 1,
"payed_for": [1],
"amount": "10",
"original_currency": "EUR",
},
)

# Delete project
self.client.post(
"/raclette/delete",
data={"password": "party"},
)

# Recreate it
self.post_project("raclette", password="party")

# History should be equal to project creation
history_list = history.get_history(self.get_project("raclette"))
assert len(history_list) == 1


if __name__ == "__main__":
unittest.main()
5 changes: 2 additions & 3 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
ResetPasswordForm,
get_billform_for,
)
from ihatemoney.history import get_history, get_history_queries
from ihatemoney.history import get_history, get_history_queries, purge_history
from ihatemoney.models import Bill, LoggingMode, Person, Project, db
from ihatemoney.utils import (
LoginThrottler,
Expand Down Expand Up @@ -808,8 +808,7 @@ def erase_history():
)
return redirect(url_for(".history"))

for query in get_history_queries(g.project):
query.delete(synchronize_session="fetch")
purge_history(g.project)

db.session.commit()
flash(_("Deleted project history."))
Expand Down

0 comments on commit a71f154

Please sign in to comment.