소스 검색

updated get_last_active function

Guénaël Muller 5 년 전
부모
커밋
8cd77c362d
3개의 변경된 파일330개의 추가작업 그리고 25개의 파일을 삭제
  1. 34 24
      tracim/lib/core/content.py
  2. 16 0
      tracim/models/data.py
  3. 280 1
      tracim/tests/library/test_content_api.py

+ 34 - 24
tracim/lib/core/content.py 파일 보기

@@ -810,39 +810,49 @@ class ContentApi(object):
810 810
 
811 811
     def get_last_active(
812 812
             self,
813
-            parent_id: typing.Optional[int],
814
-            content_type: str,
815 813
             workspace: Workspace=None,
816 814
             limit: typing.Optional[int]=None,
817
-            offset: typing.Optional[int]= None,
815
+            before_datetime: typing.Optional[datetime.datetime]= None,
818 816
     ) -> typing.List[Content]:
817
+        """
818
+        get contents list sorted by last update
819
+        (last modification of content itself or one of this comment)
820
+        :param workspace: Workspace to check
821
+        :param limit: maximum number of elements to return
822
+        :param before_datetime: date from where we check older content.
823
+        :return: list of content
824
+        """
819 825
 
820 826
         resultset = self._get_all_query(
821
-            parent_id=parent_id,
822
-            content_type=content_type,
823 827
             workspace=workspace,
824 828
         )
825 829
         resultset = resultset.order_by(desc(Content.updated))
826
-        resultset = resultset.slice(start=offset, stop=limit)
827
-
828
-        # result = []
829
-        # for item in resultset:
830
-        #     new_item = None
831
-        #     if ContentType.Comment == item.type:
832
-        #         new_item = item.parent
833
-        #     else:
834
-        #         new_item = item
835
-        #
836
-        #     # INFO - D.A. - 2015-05-20
837
-        #     # We do not want to show only one item if the last 10 items are
838
-        #     # comments about one thread for example
839
-        #     if new_item not in result:
840
-        #         result.append(new_item)
841
-        #
842
-        #     if len(result) >= limit:
843
-        #         break
844 830
 
845
-        return resultset.all()
831
+        active_contents = []
832
+        too_recent_content = []
833
+        for content in resultset:
834
+            related_active_content = None
835
+            if ContentType.Comment == content.type:
836
+                related_active_content = content.parent
837
+            else:
838
+                related_active_content = content
839
+
840
+            if not before_datetime:
841
+                before_datetime = datetime.datetime.now()
842
+            # INFO - D.A. - 2015-05-20
843
+            # We do not want to show only one item if the last 10 items are
844
+            # comments about one thread for example
845
+            if related_active_content not in active_contents and related_active_content not in too_recent_content:  # nopep8
846
+                # we verify that content is old enough
847
+                if content.updated < before_datetime:
848
+                    active_contents.append(related_active_content)
849
+                else:
850
+                    too_recent_content.append(related_active_content)
851
+
852
+            if limit and len(active_contents) >= limit:
853
+                break
854
+
855
+        return active_contents
846 856
 
847 857
     def get_last_unread(self, parent_id: typing.Optional[int], content_type: str,
848 858
                         workspace: Workspace=None, limit=10) -> typing.List[Content]:

+ 16 - 0
tracim/models/data.py 파일 보기

@@ -1257,7 +1257,23 @@ class Content(DeclarativeBase):
1257 1257
     def get_last_action(self) -> ActionDescription:
1258 1258
         return ActionDescription(self.revision_type)
1259 1259
 
1260
+    def get_simple_last_activity_date(self) -> datetime_root.datetime:
1261
+        """
1262
+        Get last activity_date, comments_included. Do not search recursively
1263
+        in revision or children.
1264
+        :return:
1265
+        """
1266
+        last_revision_date = self.updated
1267
+        for comment in self.get_comments():
1268
+            if comment.updated > last_revision_date:
1269
+                last_revision_date = comment.updated
1270
+        return last_revision_date
1271
+
1260 1272
     def get_last_activity_date(self) -> datetime_root.datetime:
1273
+        """
1274
+        Get last activity date with complete recursive search
1275
+        :return:
1276
+        """
1261 1277
         last_revision_date = self.updated
1262 1278
         for revision in self.revisions:
1263 1279
             if revision.updated > last_revision_date:

+ 280 - 1
tracim/tests/library/test_content_api.py 파일 보기

@@ -1,5 +1,5 @@
1 1
 # -*- coding: utf-8 -*-
2
-
2
+import datetime
3 3
 import transaction
4 4
 import pytest
5 5
 
@@ -1977,6 +1977,285 @@ class TestContentApi(DefaultTest):
1977 1977
         eq_(ActionDescription.UNDELETION, updated2.revision_type)
1978 1978
         eq_(u1id, updated2.owner_id)
1979 1979
 
1980
+    def test_unit__get_last_active__ok__nominal_case(self):
1981
+        uapi = UserApi(
1982
+            session=self.session,
1983
+            config=self.app_config,
1984
+            current_user=None,
1985
+        )
1986
+        group_api = GroupApi(
1987
+            current_user=None,
1988
+            session=self.session,
1989
+            config=self.app_config,
1990
+        )
1991
+        groups = [group_api.get_one(Group.TIM_USER),
1992
+                  group_api.get_one(Group.TIM_MANAGER),
1993
+                  group_api.get_one(Group.TIM_ADMIN)]
1994
+
1995
+        user = uapi.create_minimal_user(email='this.is@user',
1996
+                                        groups=groups, save_now=True)
1997
+        workspace = WorkspaceApi(
1998
+            current_user=user,
1999
+            session=self.session,
2000
+            config=self.app_config,
2001
+        ).create_workspace(
2002
+            'test workspace',
2003
+            save_now=True
2004
+        )
2005
+        workspace2 = WorkspaceApi(
2006
+            current_user=user,
2007
+            session=self.session,
2008
+            config=self.app_config,
2009
+        ).create_workspace(
2010
+            'test workspace2',
2011
+            save_now=True
2012
+        )
2013
+
2014
+        api = ContentApi(
2015
+            current_user=user,
2016
+            session=self.session,
2017
+            config=self.app_config,
2018
+        )
2019
+        main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True)  # nopep8
2020
+        main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True)  # nopep8
2021
+        # creation order test
2022
+        firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2023
+        secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2024
+        # update order test
2025
+        firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2026
+        secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2027
+        with new_revision(
2028
+            session=self.session,
2029
+            tm=transaction.manager,
2030
+            content=firstly_created_but_recently_updated,
2031
+        ):
2032
+            firstly_created_but_recently_updated.description = 'Just an update'
2033
+        api.save(firstly_created_but_recently_updated)
2034
+        # comment change order
2035
+        firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2036
+        secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2037
+        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2038
+
2039
+        content_workspace_2 = api.create(ContentType.Page, workspace,main_folder_workspace2, 'content_workspace_2', '',True)  # nopep8
2040
+        last_actives = api.get_last_active()
2041
+        assert len(last_actives) == 9
2042
+        # workspace_2 content
2043
+        assert last_actives[0] == content_workspace_2
2044
+        # comment is newest than page2
2045
+        assert last_actives[1] == firstly_created_but_recently_commented
2046
+        assert last_actives[2] == secondly_created_but_not_commented
2047
+        # last updated content is newer than other one despite creation
2048
+        # of the other is more recent
2049
+        assert last_actives[3] == firstly_created_but_recently_updated
2050
+        assert last_actives[4] == secondly_created_but_not_updated
2051
+        # creation order is inverted here as last created is last active
2052
+        assert last_actives[5] == secondly_created
2053
+        assert last_actives[6] == firstly_created
2054
+        # folder subcontent modification does not change folder order
2055
+        assert last_actives[7] == main_folder
2056
+        # folder subcontent modification does not change folder order
2057
+        # (workspace2)
2058
+        assert last_actives[8] == main_folder_workspace2
2059
+
2060
+    def test_unit__get_last_active__ok__workspace_filter_workspace_full(self):
2061
+        uapi = UserApi(
2062
+            session=self.session,
2063
+            config=self.app_config,
2064
+            current_user=None,
2065
+        )
2066
+        group_api = GroupApi(
2067
+            current_user=None,
2068
+            session=self.session,
2069
+            config=self.app_config,
2070
+        )
2071
+        groups = [group_api.get_one(Group.TIM_USER),
2072
+                  group_api.get_one(Group.TIM_MANAGER),
2073
+                  group_api.get_one(Group.TIM_ADMIN)]
2074
+
2075
+        user = uapi.create_minimal_user(email='this.is@user',
2076
+                                        groups=groups, save_now=True)
2077
+        workspace = WorkspaceApi(
2078
+            current_user=user,
2079
+            session=self.session,
2080
+            config=self.app_config,
2081
+        ).create_workspace(
2082
+            'test workspace',
2083
+            save_now=True
2084
+        )
2085
+
2086
+        api = ContentApi(
2087
+            current_user=user,
2088
+            session=self.session,
2089
+            config=self.app_config,
2090
+        )
2091
+        main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True)  # nopep8
2092
+        # creation order test
2093
+        firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2094
+        secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2095
+        # update order test
2096
+        firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2097
+        secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2098
+        with new_revision(
2099
+            session=self.session,
2100
+            tm=transaction.manager,
2101
+            content=firstly_created_but_recently_updated,
2102
+        ):
2103
+            firstly_created_but_recently_updated.description = 'Just an update'
2104
+        api.save(firstly_created_but_recently_updated)
2105
+        # comment change order
2106
+        firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2107
+        secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2108
+        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2109
+
2110
+        last_actives = api.get_last_active(workspace=workspace)
2111
+        assert len(last_actives) == 7
2112
+        # comment is newest than page2
2113
+        assert last_actives[0] == firstly_created_but_recently_commented
2114
+        assert last_actives[1] == secondly_created_but_not_commented
2115
+        # last updated content is newer than other one despite creation
2116
+        # of the other is more recent
2117
+        assert last_actives[2] == firstly_created_but_recently_updated
2118
+        assert last_actives[3] == secondly_created_but_not_updated
2119
+        # creation order is inverted here as last created is last active
2120
+        assert last_actives[4] == secondly_created
2121
+        assert last_actives[5] == firstly_created
2122
+        # folder subcontent modification does not change folder order
2123
+        assert last_actives[6] == main_folder
2124
+
2125
+    def test_unit__get_last_active__ok__workspace_filter_workspace_limit_2_multiples_times(self):  # nopep8
2126
+        uapi = UserApi(
2127
+            session=self.session,
2128
+            config=self.app_config,
2129
+            current_user=None,
2130
+        )
2131
+        group_api = GroupApi(
2132
+            current_user=None,
2133
+            session=self.session,
2134
+            config=self.app_config,
2135
+        )
2136
+        groups = [group_api.get_one(Group.TIM_USER),
2137
+                  group_api.get_one(Group.TIM_MANAGER),
2138
+                  group_api.get_one(Group.TIM_ADMIN)]
2139
+
2140
+        user = uapi.create_minimal_user(email='this.is@user',
2141
+                                        groups=groups, save_now=True)
2142
+        workspace = WorkspaceApi(
2143
+            current_user=user,
2144
+            session=self.session,
2145
+            config=self.app_config,
2146
+        ).create_workspace(
2147
+            'test workspace',
2148
+            save_now=True
2149
+        )
2150
+
2151
+        api = ContentApi(
2152
+            current_user=user,
2153
+            session=self.session,
2154
+            config=self.app_config,
2155
+        )
2156
+        main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True)  # nopep8
2157
+        # creation order test
2158
+        firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2159
+        secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2160
+        # update order test
2161
+        firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2162
+        secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2163
+        with new_revision(
2164
+            session=self.session,
2165
+            tm=transaction.manager,
2166
+            content=firstly_created_but_recently_updated,
2167
+        ):
2168
+            firstly_created_but_recently_updated.description = 'Just an update'
2169
+        api.save(firstly_created_but_recently_updated)
2170
+        # comment change order
2171
+        firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2172
+        secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2173
+        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2174
+
2175
+        last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=datetime.datetime.now())  # nopep8
2176
+        assert len(last_actives) == 2
2177
+        # comment is newest than page2
2178
+        assert last_actives[0] == firstly_created_but_recently_commented
2179
+        assert last_actives[1] == secondly_created_but_not_commented
2180
+
2181
+        last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=last_actives[1].get_simple_last_activity_date())  # nopep8
2182
+        assert len(last_actives) == 2
2183
+        # last updated content is newer than other one despite creation
2184
+        # of the other is more recent
2185
+        assert last_actives[0] == firstly_created_but_recently_updated
2186
+        assert last_actives[1] == secondly_created_but_not_updated
2187
+
2188
+        last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=last_actives[1].get_simple_last_activity_date())  # nopep8
2189
+        assert len(last_actives) == 2
2190
+        # creation order is inverted here as last created is last active
2191
+        assert last_actives[0] == secondly_created
2192
+        assert last_actives[1] == firstly_created
2193
+
2194
+        last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=last_actives[1].get_simple_last_activity_date())  # nopep8
2195
+        assert len(last_actives) == 1
2196
+        # folder subcontent modification does not change folder order
2197
+        assert last_actives[0] == main_folder
2198
+
2199
+    def test_unit__get_last_active__ok__workspace_filter_workspace_empty(self):
2200
+        uapi = UserApi(
2201
+            session=self.session,
2202
+            config=self.app_config,
2203
+            current_user=None,
2204
+        )
2205
+        group_api = GroupApi(
2206
+            current_user=None,
2207
+            session=self.session,
2208
+            config=self.app_config,
2209
+        )
2210
+        groups = [group_api.get_one(Group.TIM_USER),
2211
+                  group_api.get_one(Group.TIM_MANAGER),
2212
+                  group_api.get_one(Group.TIM_ADMIN)]
2213
+
2214
+        user = uapi.create_minimal_user(email='this.is@user',
2215
+                                        groups=groups, save_now=True)
2216
+        workspace = WorkspaceApi(
2217
+            current_user=user,
2218
+            session=self.session,
2219
+            config=self.app_config,
2220
+        ).create_workspace(
2221
+            'test workspace',
2222
+            save_now=True
2223
+        )
2224
+        workspace2 = WorkspaceApi(
2225
+            current_user=user,
2226
+            session=self.session,
2227
+            config=self.app_config,
2228
+        ).create_workspace(
2229
+            'test workspace2',
2230
+            save_now=True
2231
+        )
2232
+        api = ContentApi(
2233
+            current_user=user,
2234
+            session=self.session,
2235
+            config=self.app_config,
2236
+        )
2237
+        main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True)  # nopep8
2238
+        # creation order test
2239
+        firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2240
+        secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2241
+        # update order test
2242
+        firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2243
+        secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2244
+        with new_revision(
2245
+            session=self.session,
2246
+            tm=transaction.manager,
2247
+            content=firstly_created_but_recently_updated,
2248
+        ):
2249
+            firstly_created_but_recently_updated.description = 'Just an update'
2250
+        api.save(firstly_created_but_recently_updated)
2251
+        # comment change order
2252
+        firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2253
+        secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2254
+        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2255
+
2256
+        last_actives = api.get_last_active(workspace=workspace2)
2257
+        assert len(last_actives) == 0
2258
+
1980 2259
     def test_search_in_label(self):
1981 2260
         # HACK - D.A. - 2015-03-09
1982 2261
         # This test is based on a bug which does NOT return results found