Skip to content

Commit

Permalink
Merge pull request #753 from c2corg/associations-history
Browse files Browse the repository at this point in the history
Add association history service
  • Loading branch information
cbeauchesne authored Nov 3, 2019
2 parents 0ab0a23 + c255ca9 commit 804c327
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Add index on AssociationLog
Revision ID: 85a5ed3c76a8
Revises: 83956b269661
Create Date: 2019-10-25 21:20:55.476560
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = '85a5ed3c76a8'
down_revision = '83956b269661'
branch_labels = None
depends_on = None


def upgrade():
op.create_index('association_log_child_document_id_idx',
'association_log', ['child_document_id'],
unique=False,
schema='guidebook')
op.create_index('association_log_parent_document_id_idx',
'association_log', ['parent_document_id'],
unique=False,
schema='guidebook')
op.create_index('association_log_user_id_idx',
'association_log', ['user_id'],
unique=False,
schema='guidebook')


def downgrade():
op.drop_index('association_log_parent_document_id_idx',
table_name='association_log',
schema='guidebook')
op.drop_index('association_log_child_document_id_idx',
table_name='association_log',
schema='guidebook')
op.drop_index('association_log_user_id_idx',
table_name='association_log',
schema='guidebook')
11 changes: 8 additions & 3 deletions c2corg_api/models/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,25 @@ class AssociationLog(Base):

parent_document_id = Column(
Integer, ForeignKey(schema + '.documents.document_id'),
nullable=False)
nullable=False,
index=True
)
parent_document = relationship(
Document, primaryjoin=parent_document_id == Document.document_id)
parent_document_type = Column(String(1), nullable=False)

child_document_id = Column(
Integer, ForeignKey(schema + '.documents.document_id'),
nullable=False)
nullable=False,
index=True
)
child_document = relationship(
Document, primaryjoin=child_document_id == Document.document_id)
child_document_type = Column(String(1), nullable=False)

user_id = Column(
Integer, ForeignKey(users_schema + '.user.id'), nullable=False)
Integer, ForeignKey(users_schema + '.user.id'), nullable=False,
index=True)
user = relationship(
User, primaryjoin=user_id == User.id, viewonly=True)

Expand Down
60 changes: 59 additions & 1 deletion c2corg_api/tests/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from c2corg_api.models.feed import DocumentChange
from c2corg_api.models.route import Route
from c2corg_api.models.user import User
from c2corg_api.models.user_profile import UserProfile
from c2corg_api.models.user_profile import UserProfile, USERPROFILE_TYPE
from c2corg_api.scripts.es.sync import sync_es
from c2corg_api.search import elasticsearch_config, search_documents
from c2corg_api.tests import BaseTestCase
Expand Down Expand Up @@ -1107,6 +1107,64 @@ def put_success_new_lang(

return (body, document)

def _get_association_logs(self, document):
url = '/associations-history?d={}'.format(
document.document_id
)

resp = self.app.get(url, status=200)
self.assertIsInstance(resp.json['count'], int)
associations = resp.json["associations"]

self.assertNotEqual(
len(associations),
0,
"Need at least one association")

for association in associations:
self._assert_association_log_structure(
association,
document.document_id)

return associations

def _assert_association_log_structure(self, log, document_id):
self.assertIsNotNone(log['written_at'])
self.assertIsInstance(log['is_creation'], bool)

user = log['user']
self.assertIsInstance(user['user_id'], int)
self.assertIsInstance(user['name'], str)
self.assertIsInstance(user['forum_username'], str)
self.assertIsInstance(user['robot'], bool)
self.assertIsInstance(user['moderator'], bool)
self.assertIsInstance(user['blocked'], bool)

child = log['child_document']
child_id = child['document_id']
self.assertIsInstance(child_id, int)
self.assertIsInstance(child['type'], str)
self.assertIsInstance(child['locales'], list)

parent = log['parent_document']
parent_id = parent['document_id']
self.assertIsInstance(parent_id, int)
self.assertIsInstance(parent['type'], str)
self.assertIsInstance(parent['locales'], list)

self.assertTrue(child_id == document_id or parent_id == document_id)

if parent["type"] == USERPROFILE_TYPE:
self.assertIsInstance(parent["name"], str)

if child["type"] == USERPROFILE_TYPE:
self.assertIsInstance(child["name"], str)

def _add_association(self, association, user_id):
""" used for setup """
self.session.add(association)
self.session.add(association.get_log(user_id, is_creation=True))


def get_locale(locales, lang):
return next(filter(lambda locale: locale['lang'] == lang, locales), None)
8 changes: 7 additions & 1 deletion c2corg_api/tests/views/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ def test_put_success_new_lang(self):

self.assertEquals(area.get_locale('es').title, 'Chartreuse')

def test_get_associations_history(self):
self._get_association_logs(self.area1)

def _assert_geometry(self, body):
self.assertIsNotNone(body.get('geometry'))
geometry = body.get('geometry')
Expand Down Expand Up @@ -510,5 +513,8 @@ def _add_test_data(self):
self.session.add(self.image)
self.session.flush()

self.session.add(Association.create(self.area1, self.image))
self._add_association(
Association.create(self.area1, self.image),
user_id
)
self.session.flush()
11 changes: 7 additions & 4 deletions c2corg_api/tests/views/test_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ def test_put_as_non_author(self):
self.assertEqual(len(body['errors']), 1)
self.assertEqual(body['errors'][0]['name'], 'Forbidden')

def test_get_associations_history(self):
self._get_association_logs(self.article1)

def _add_test_data(self):
self.article1 = Article(categories=['site_info'],
activities=['hiking'],
Expand Down Expand Up @@ -639,10 +642,10 @@ def _add_test_data(self):
self.session.add(self.waypoint2)
self.session.flush()

self.session.add(Association.create(
self._add_association(Association.create(
parent_document=self.article1,
child_document=self.article4))
self.session.add(Association.create(
child_document=self.article4), user_id)
self._add_association(Association.create(
parent_document=self.article3,
child_document=self.article1))
child_document=self.article1), user_id)
self.session.flush()
15 changes: 9 additions & 6 deletions c2corg_api/tests/views/test_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ def test_put_success_new_lang(self):

self.assertEquals(book1.get_locale('es').title, 'Escalades au Thaurac')

def test_get_associations_history(self):
self._get_association_logs(self.book1)

def _add_test_data(self):
self.book1 = Book(activities=['hiking'],
book_types=['biography'])
Expand Down Expand Up @@ -565,13 +568,13 @@ def _add_test_data(self):
self.session.add(self.route3)
self.session.flush()

# self.session.add(Association.create(
# self._add_association(Association.create(
# parent_document=self.book1,
# child_document=self.image2))
self.session.add(Association.create(
# child_document=self.image2), user_id)
self._add_association(Association.create(
parent_document=self.book1,
child_document=self.route3))
self.session.add(Association.create(
child_document=self.route3), user_id)
self._add_association(Association.create(
parent_document=self.book2,
child_document=self.waypoint2))
child_document=self.waypoint2), user_id)
self.session.flush()
31 changes: 19 additions & 12 deletions c2corg_api/tests/views/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
class BaseTestImage(BaseDocumentTestRest):

def _add_test_data(self):
user_id = self.global_userids['contributor']

self.image = Image(
filename='image.jpg',
activities=['paragliding'], height=1500,
Expand All @@ -54,19 +56,18 @@ def _add_test_data(self):
article_type='collab')
self.session.add(self.article1)
self.session.flush()
self.session.add(Association.create(
self._add_association(Association.create(
parent_document=self.article1,
child_document=self.image))
child_document=self.image), user_id)

self.book1 = Book(activities=['hiking'],
book_types=['biography'])
self.session.add(self.book1)
self.session.flush()
self.session.add(Association.create(
self._add_association(Association.create(
parent_document=self.book1,
child_document=self.image))
child_document=self.image), user_id)

user_id = self.global_userids['contributor']
DocumentRest.create_new_version(self.image, user_id)

self.image_version = self.session.query(DocumentVersion). \
Expand Down Expand Up @@ -95,9 +96,9 @@ def _add_test_data(self):
self.image3, self.global_userids['contributor2'])
DocumentRest.create_new_version(self.image4, user_id)

self.session.add(Association.create(
self._add_association(Association.create(
parent_document=self.image,
child_document=self.image2))
child_document=self.image2), user_id)

self.waypoint = Waypoint(
waypoint_type='summit', elevation=4,
Expand All @@ -120,7 +121,10 @@ def _add_test_data(self):
self.session.add(self.area)
self.session.flush()

self.session.add(Association.create(self.area, self.image))
self._add_association(
Association.create(self.area, self.image),
user_id
)
self.session.flush()

self.outing1 = Outing(
Expand All @@ -134,14 +138,14 @@ def _add_test_data(self):
)
self.session.add(self.outing1)
self.session.flush()
self.session.add(Association.create(
self._add_association(Association.create(
parent_document=self.outing1,
child_document=self.image))
self.session.add(Association(
child_document=self.image), user_id)
self._add_association(Association(
parent_document_id=self.global_userids['contributor'],
parent_document_type=USERPROFILE_TYPE,
child_document_id=self.outing1.document_id,
child_document_type=OUTING_TYPE))
child_document_type=OUTING_TYPE), user_id)
update_feed_document_create(self.outing1, user_id)
self.session.flush()

Expand Down Expand Up @@ -817,6 +821,9 @@ def test_change_image_type_non_collaborative(self):
self._prefix + '/' + str(self.image4.document_id), body,
headers=headers, status=200)

def test_get_associations_history(self):
self._get_association_logs(self.image)


class TestImageListRest(BaseTestImage):

Expand Down
62 changes: 44 additions & 18 deletions c2corg_api/tests/views/test_outing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,16 @@ def test_history_no_lang(self):
def test_history_no_doc(self):
self.app.get('/document/99999/history/es', status=404)

def test_get_associations_history(self):
logs = self._get_association_logs(self.outing)

self.assertEqual(len(logs), 4)

# Third association (starting from the youngest) is a user
log = logs[2]
parent = log['parent_document']
self.assertEqual(parent["type"], USERPROFILE_TYPE)

def _assert_geometry(self, body):
self.assertIsNotNone(body.get('geometry'))
geometry = body.get('geometry')
Expand Down Expand Up @@ -1144,22 +1154,38 @@ def _add_test_data(self):
gear='paraglider'))
self.session.add(self.route)
self.session.flush()
self.session.add(Association.create(
parent_document=self.waypoint,
child_document=self.route))
self.session.add(Association.create(
parent_document=self.route,
child_document=self.outing))

self.session.add(Association(
parent_document_id=user_id,
parent_document_type=USERPROFILE_TYPE,
child_document_id=self.outing.document_id,
child_document_type=OUTING_TYPE))
self.session.add(Association.create(
parent_document=self.outing,
child_document=self.image))
self.session.add(Association.create(
parent_document=self.outing,
child_document=self.article1))

self._add_association(
Association.create(
parent_document=self.waypoint,
child_document=self.route
),
user_id=user_id)
self._add_association(
Association.create(
parent_document=self.route,
child_document=self.outing
),
user_id=user_id)

self._add_association(
Association(
parent_document_id=user_id,
parent_document_type=USERPROFILE_TYPE,
child_document_id=self.outing.document_id,
child_document_type=OUTING_TYPE
),
user_id=user_id)
self._add_association(
Association.create(
parent_document=self.outing,
child_document=self.image
),
user_id=user_id)
self._add_association(
Association.create(
parent_document=self.outing,
child_document=self.article1
),
user_id=user_id)
self.session.flush()
Loading

0 comments on commit 804c327

Please sign in to comment.