Skip to content

Commit

Permalink
Remove tags as well when deleting a document
Browse files Browse the repository at this point in the history
  • Loading branch information
asaunier committed Apr 28, 2020
1 parent 9aee16c commit ff94d33
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions c2corg_api/tests/views/test_document_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get_available_langs)
from c2corg_api.models.document_history import DocumentVersion
from c2corg_api.models.document_topic import DocumentTopic
from c2corg_api.models.document_tag import DocumentTag, DocumentTagLog
from c2corg_api.models.feed import (
DocumentChange, update_feed_document_create, update_feed_images_upload)
from c2corg_api.models.image import Image, ArchiveImage
Expand Down Expand Up @@ -153,6 +154,7 @@ def setUp(self): # noqa
self._add_association(self.waypoint1, self.route3)
self._add_association(self.waypoint2, self.route3)
self._add_association(self.waypoint3, self.route3)
self._add_tag(self.route3)
self.session.flush()

outing1_geometry = DocumentGeometry(
Expand Down Expand Up @@ -393,6 +395,19 @@ def _add_association(self, parent_document, child_document):
self.session.add(association.get_log(
self.global_userids['contributor']))

def _add_tag(self, document):
document_id = document.document_id
document_type = document.type
user_id = self.global_userids['contributor']
tag = DocumentTag(
document_id=document_id, document_type=document_type,
user_id=user_id)
self.session.add(tag)
log = DocumentTagLog(
document_id=document_id, document_type=document_type,
user_id=user_id, is_creation=True)
self.session.add(log)

def _delete(self, document_id, expected_status):
headers = self.add_authorization_header(username='moderator')
return self.app.delete_json(
Expand Down Expand Up @@ -562,6 +577,27 @@ def test_delete_route(self):
self.check_cache_version(self.xreport1.document_id, 2)
self.check_cache_version(self.image1.document_id, 2)

def _count_tags(self, document_id):
nb_tags = self.session.query(DocumentTag). \
filter(DocumentTag.document_id == document_id).count()
nb_logs = self.session.query(DocumentTagLog). \
filter(DocumentTagLog.document_id == document_id).count()
return nb_tags, nb_logs

def test_delete_route_with_tags(self):
nb_tags, nb_logs = self._count_tags(self.route3.document_id)
self.assertEqual(nb_tags, 1)
self.assertEqual(nb_logs, 1)

self._test_delete(
self.route3.document_id,
Route, RouteLocale, ArchiveRoute, ArchiveRouteLocale)

# Check related tags have been removed
nb_tags, nb_logs = self._count_tags(self.route3.document_id)
self.assertEqual(nb_tags, 0)
self.assertEqual(nb_logs, 0)

def test_delete_outing(self):
# outing1b redirects to outing1 => 2 documents to delete
self._test_delete(
Expand Down
9 changes: 9 additions & 0 deletions c2corg_api/views/document_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get_available_langs)
from c2corg_api.models.document_history import HistoryMetaData, \
DocumentVersion, has_been_created_by, is_less_than_24h_old
from c2corg_api.models.document_tag import DocumentTag, DocumentTagLog
from c2corg_api.models.document_topic import DocumentTopic
from c2corg_api.models.es_sync import ESDeletedDocument, ESDeletedLocale
from c2corg_api.models.feed import DocumentChange, update_langs_of_changes
Expand Down Expand Up @@ -235,6 +236,7 @@ def _delete_document(self, document_id, document_type, redirecting=False):
# Remove associations and update cache of formerly associated docs
update_cache_version_full(document_id, document_type)
_remove_associations(document_id)
_remove_tags(document_id)

clazz, clazz_locale, archive_clazz, archive_clazz_locale = _get_models(
document_type)
Expand Down Expand Up @@ -535,6 +537,13 @@ def _remove_associations(document_id):
filter(AreaAssociation.document_id == document_id).delete()


def _remove_tags(document_id):
DBSession.query(DocumentTag). \
filter(DocumentTag.document_id == document_id).delete()
DBSession.query(DocumentTagLog). \
filter(DocumentTagLog.document_id == document_id).delete()


def update_deleted_documents_list(document_id, document_type):
DBSession.add(ESDeletedDocument(
document_id=document_id, type=document_type))
Expand Down

0 comments on commit ff94d33

Please sign in to comment.