Parcourir la source

bugfix: set childre: False in treeview json api for documents

Damien ACCORSI il y a 10 ans
Parent
révision
f8790b2b93

+ 1 - 1
tracim/tracim/model/data.py Voir le fichier

@@ -422,7 +422,7 @@ class Content(DeclarativeBase):
422 422
     def get_child_nb(self, content_type: ContentType, content_status = ''):
423 423
         child_nb = 0
424 424
         for child in self.valid_children:
425
-            if child.type==content_type:
425
+            if child.type == content_type or content_type == ContentType.Any:
426 426
                 if not content_status:
427 427
                     child_nb = child_nb+1
428 428
                 elif content_status==child.status:

+ 6 - 2
tracim/tracim/model/serializers.py Voir le fichier

@@ -283,11 +283,15 @@ def serialize_content_for_menu_api(content: Content, context: Context):
283 283
     content_id = content.content_id
284 284
     workspace_id = content.workspace_id
285 285
 
286
+    has_children = False
287
+    if content.type == ContentType.Folder:
288
+        has_children = content.get_child_nb(ContentType.Any) > 0
289
+
286 290
     result = DictLikeClass(
287 291
         id = CST.TREEVIEW_MENU.ID_TEMPLATE__FULL.format(workspace_id, content_id),
288
-        children = True, # TODO: make this dynamic
292
+        children = has_children,
289 293
         text = content.get_label(),
290
-        a_attr = { 'href' : context.url(ContentType.fill_url(content)) },
294
+        a_attr = { 'href' : context.url(ContentType.fill_url(content))},
291 295
         li_attr = { 'title': content.get_label(), 'class': 'tracim-tree-item-is-a-folder' },
292 296
         type = content.type,
293 297
         state = { 'opened': True if ContentType.Folder!=content.type else False, 'selected': False }

+ 37 - 1
tracim/tracim/tests/library/test_serializers.py Voir le fichier

@@ -8,6 +8,8 @@ from sqlalchemy.orm.exc import NoResultFound
8 8
 
9 9
 import transaction
10 10
 
11
+from tracim.model import DBSession
12
+
11 13
 from tg.util import LazyString
12 14
 from tracim.model.data import Content
13 15
 from tracim.model.data import ContentType
@@ -222,6 +224,40 @@ class TestSerializers(TestStandard):
222 224
         eq_(3, s2.subitems_nb)
223 225
         eq_(3, len(s2.subitems))
224 226
 
227
+        eq_(2, len(s2))
225 228
 
226 229
 
227
-        eq_(2, len(s2))
230
+    def test_serializer_content__menui_api_context__children(self):
231
+        self.app.get('/_test_vars')  # Allow to create fake context
232
+
233
+        folder_without_child = Content()
234
+        folder_without_child.type = ContentType.Folder
235
+        res = Context(CTX.MENU_API).toDict(folder_without_child)
236
+        eq_(False, res['children'])
237
+
238
+        folder_with_child = Content()
239
+        folder_with_child.type = ContentType.Folder
240
+        folder_without_child.parent = folder_with_child
241
+        DBSession.add(folder_with_child)
242
+        DBSession.add(folder_without_child)
243
+        DBSession.flush()
244
+
245
+        res = Context(CTX.MENU_API).toDict(folder_with_child)
246
+        eq_(True, res['children'])
247
+
248
+        for curtype in ContentType.all():
249
+            print( 'iteration: ', curtype)
250
+            if curtype not in (ContentType.Folder, ContentType.Comment):
251
+                item = Content()
252
+                item.type = curtype
253
+
254
+                fake_child = Content()
255
+                fake_child.type = curtype
256
+                fake_child.parent = item
257
+
258
+                DBSession.add(item)
259
+                DBSession.add(fake_child)
260
+                DBSession.flush()
261
+
262
+                res = Context(CTX.MENU_API).toDict(item)
263
+                eq_(False, res['children'])