| 161 | | def _get_group_node(self, group_key, id): |
| 162 | | """ |
| 163 | | activate a coherent group, which have the same value in the 'group_key'. |
| 164 | | returns the first node with a other group value. |
| 165 | | """ |
| 166 | | self.deactivate_all() |
| 167 | | |
| 168 | | node = self.nodes[id] |
| 169 | | group_value = node.data[group_key] |
| 170 | | |
| 171 | | # go to the first parent node how has a other group value: |
| 172 | | old_node = node |
| 173 | | while 1: |
| 174 | | if node.parent == None: |
| 175 | | break |
| 176 | | node = node.parent |
| 177 | | if node.data[group_key] != group_value: |
| 178 | | break |
| 179 | | old_node = node |
| 180 | | first_node = old_node |
| 181 | | print "first node with the same group value:\n\t", first_node.data |
| 182 | | |
| 183 | | # expand/activate the group: |
| 184 | | for subnode in first_node.subnodes: |
| 185 | | print "subnode:", subnode.data |
| 186 | | subnode.activate_expanded(group_key, group_value) |
| 187 | | |
| 188 | | return first_node |
| 189 | | |
| 190 | | def get_group_dict(self, group_key, id): |
| 191 | | """ |
| 192 | | returns a coherent group as a dict |
| 193 | | """ |
| 194 | | node = self._get_group_node(group_key, id) |
| 195 | | return node.to_dict()#["subitems"] |
| 196 | | |
| 197 | | def get_group_list(self, group_key, id): |
| 198 | | """ |
| 199 | | returns a coherent group as a flat list. |
| 200 | | """ |
| 201 | | node = self._get_group_node(group_key, id) |
| 202 | | return node.get_flat_list()#[1:] |
| | 164 | # def _get_group_node(self, group_key, id, debug): |
| | 165 | # """ |
| | 166 | # activate a coherent group, which have the same value in the 'group_key'. |
| | 167 | # returns the first node with a other group value. |
| | 168 | # """ |
| | 169 | # if debug: |
| | 170 | # print "debug _get_group_node() id=%s" % id |
| | 171 | # |
| | 172 | # self.deactivate_all() |
| | 173 | # |
| | 174 | # node = self.nodes[id] |
| | 175 | # group_value = node.data[group_key] |
| | 176 | # |
| | 177 | # # go to the first parent node how has a other group value: |
| | 178 | # old_node = node |
| | 179 | # while True: |
| | 180 | # if node.parent == None: |
| | 181 | # break |
| | 182 | # node = node.parent |
| | 183 | # if node.data[group_key] != group_value: |
| | 184 | # break |
| | 185 | # old_node = node |
| | 186 | # first_node = old_node |
| | 187 | # if debug: |
| | 188 | # print "first node with the same group value:\n\t", first_node.data |
| | 189 | # |
| | 190 | # # expand/activate the group: |
| | 191 | # for subnode in first_node.subnodes: |
| | 192 | # if debug: |
| | 193 | # print "subnode:", subnode.data |
| | 194 | # subnode.activate_expanded(group_key, group_value) |
| | 195 | # |
| | 196 | # return first_node |
| | 197 | # |
| | 198 | # def get_group_dict(self, group_key, id, debug=False): |
| | 199 | # """ |
| | 200 | # returns a coherent group as a dict |
| | 201 | # """ |
| | 202 | # node = self._get_group_node(group_key, id, debug) |
| | 203 | # return node.to_dict()#["subitems"] |
| | 204 | # |
| | 205 | # def get_group_list(self, group_key, id, debug=False): |
| | 206 | # """ |
| | 207 | # returns a coherent group as a flat list. |
| | 208 | # """ |
| | 209 | # node = self._get_group_node(group_key, id, debug) |
| | 210 | # return node.get_flat_list()#[1:] |
| 320 | | 'parent': None, |
| 321 | | 'subitems': [{'group': 'two', |
| 322 | | 'id': 2, |
| 323 | | 'level': 2, |
| 324 | | 'name': '1.1. BBB', |
| 325 | | 'parent': 1}, |
| 326 | | {'group': 'two', |
| 327 | | 'id': 3, |
| 328 | | 'level': 2, |
| 329 | | 'name': '1.2. BBB', |
| 330 | | 'parent': 1}]}, |
| 331 | | {'group': 'one', 'id': 6, 'level': 1, 'name': '2. DDD', 'parent': None}] |
| 332 | | check(result, must_be, no=3) |
| 333 | | |
| 334 | | |
| 335 | | # |
| 336 | | # Activate the sixth menu point. |
| 337 | | # |
| 338 | | result = tree.get_menu_tree(6) |
| 339 | | must_be = [ |
| 340 | | {'group': 'one', 'id': 1, 'level': 1, 'name': '1. AAA', 'parent': None}, |
| 341 | | {'group': 'one', |
| 342 | | 'id': 6, |
| 343 | | 'level': 1, |
| 344 | | 'name': '2. DDD', |
| 345 | | 'parent': None, |
| 346 | | 'subitems': [{'group': 'two', |
| 347 | | 'id': 7, |
| 348 | | 'level': 2, |
| 349 | | 'name': '2.1. EEE', |
| 350 | | 'parent': 6}, |
| 351 | | {'group': 'two', |
| 352 | | 'id': 8, |
| 353 | | 'level': 2, |
| 354 | | 'name': '2.2. EEE', |
| 355 | | 'parent': 6}]}] |
| 356 | | check(result, must_be, no=4) |
| 357 | | |
| 358 | | |
| 359 | | # |
| 360 | | # a flat list for all current activated pages |
| 361 | | # |
| 362 | | result = tree.get_flat_list() |
| 363 | | must_be = [ |
| 364 | | {'group': 'one', 'id': 1, 'level': 1, 'name': '1. AAA', 'parent': None}, |
| 365 | | {'group': 'one', 'id': 6, 'level': 1, 'name': '2. DDD', 'parent': None}, |
| 366 | | {'group': 'two', 'id': 7, 'level': 2, 'name': '2.1. EEE', 'parent': 6}, |
| 367 | | {'group': 'two', 'id': 8, 'level': 2, 'name': '2.2. EEE', 'parent': 6} |
| 368 | | ] |
| | 424 | 'parent': None}, |
| | 425 | {'active': True, |
| | 426 | 'group': 'one', |
| | 427 | 'id': 6, |
| | 428 | 'is_current': True, |
| | 429 | 'level': 1, |
| | 430 | 'name': '2. DDD', |
| | 431 | 'parent': None}, |
| | 432 | {'active': False, |
| | 433 | 'group': 'two', |
| | 434 | 'id': 7, |
| | 435 | 'level': 2, |
| | 436 | 'name': '2.1. EEE', |
| | 437 | 'parent': 6}, |
| | 438 | {'active': False, |
| | 439 | 'group': 'two', |
| | 440 | 'id': 8, |
| | 441 | 'level': 2, |
| | 442 | 'name': '2.2. EEE', |
| | 443 | 'parent': 6}] |
| 377 | | must_be = [ |
| 378 | | {'group': 'one', 'id': 1, 'level': 1, 'name': '1. AAA', 'parent': None}, |
| 379 | | {'group': 'two', 'id': 2, 'level': 2, 'name': '1.1. BBB', 'parent': 1}, |
| 380 | | {'group': 'two', 'id': 3, 'level': 2, 'name': '1.2. BBB', 'parent': 1}, |
| 381 | | {'group': 'two', 'id': 4, 'level': 3, 'name': '1.2.1. CCC', 'parent': 3}, |
| 382 | | {'group': 'two', 'id': 5, 'level': 3, 'name': '1.2.2. CCC', 'parent': 3}, |
| 383 | | {'group': 'one', 'id': 6, 'level': 1, 'name': '2. DDD', 'parent': None}, |
| 384 | | {'group': 'two', 'id': 7, 'level': 2, 'name': '2.1. EEE', 'parent': 6}, |
| 385 | | {'group': 'two', 'id': 8, 'level': 2, 'name': '2.2. EEE', 'parent': 6} |
| 386 | | ] |
| | 452 | must_be = [{'active': True, |
| | 453 | 'group': 'one', |
| | 454 | 'id': 1, |
| | 455 | 'level': 1, |
| | 456 | 'name': '1. AAA', |
| | 457 | 'parent': None}, |
| | 458 | {'active': True, |
| | 459 | 'group': 'two', |
| | 460 | 'id': 2, |
| | 461 | 'is_current': True, |
| | 462 | 'level': 2, |
| | 463 | 'name': '1.1. BBB', |
| | 464 | 'parent': 1}, |
| | 465 | {'active': False, |
| | 466 | 'group': 'two', |
| | 467 | 'id': 3, |
| | 468 | 'level': 2, |
| | 469 | 'name': '1.2. BBB', |
| | 470 | 'parent': 1}, |
| | 471 | {'active': False, |
| | 472 | 'group': 'two', |
| | 473 | 'id': 4, |
| | 474 | 'level': 3, |
| | 475 | 'name': '1.2.1. CCC', |
| | 476 | 'parent': 3}, |
| | 477 | {'active': False, |
| | 478 | 'group': 'two', |
| | 479 | 'id': 5, |
| | 480 | 'level': 3, |
| | 481 | 'name': '1.2.2. CCC', |
| | 482 | 'parent': 3}, |
| | 483 | {'active': True, |
| | 484 | 'group': 'one', |
| | 485 | 'id': 6, |
| | 486 | 'is_current': True, |
| | 487 | 'level': 1, |
| | 488 | 'name': '2. DDD', |
| | 489 | 'parent': None}, |
| | 490 | {'active': False, |
| | 491 | 'group': 'two', |
| | 492 | 'id': 7, |
| | 493 | 'level': 2, |
| | 494 | 'name': '2.1. EEE', |
| | 495 | 'parent': 6}, |
| | 496 | {'active': False, |
| | 497 | 'group': 'two', |
| | 498 | 'id': 8, |
| | 499 | 'level': 2, |
| | 500 | 'name': '2.2. EEE', |
| | 501 | 'parent': 6}] |
| 390 | | # |
| 391 | | # a coherent group, which have the same value in the 'group_key' |
| 392 | | # |
| 393 | | result = tree.get_group_dict(group_key="group", id=5) |
| 394 | | must_be = [ |
| 395 | | {'group': 'two', 'id': 2, 'level': 1, 'name': '1.1. BBB', 'parent': 1}, |
| 396 | | {'group': 'two', |
| 397 | | 'id': 3, |
| 398 | | 'level': 1, |
| 399 | | 'name': '1.2. BBB', |
| 400 | | 'parent': 1, |
| 401 | | 'subitems': [{'group': 'two', |
| 402 | | 'id': 4, |
| 403 | | 'level': 2, |
| 404 | | 'name': '1.2.1. CCC', |
| 405 | | 'parent': 3}, |
| 406 | | {'group': 'two', |
| 407 | | 'id': 5, |
| 408 | | 'level': 2, |
| 409 | | 'name': '1.2.2. CCC', |
| 410 | | 'parent': 3}]} |
| 411 | | ] |
| 412 | | check(result, must_be, no=7) |
| 413 | | |
| 414 | | result = tree.get_group_list(group_key="group", id=5) |
| 415 | | must_be = [ |
| 416 | | {'group': 'two', 'id': 2, 'level': 1, 'name': '1.1. BBB', 'parent': 1}, |
| 417 | | {'group': 'two', 'id': 3, 'level': 1, 'name': '1.2. BBB', 'parent': 1}, |
| 418 | | {'group': 'two', 'id': 4, 'level': 2, 'name': '1.2.1. CCC', 'parent': 3}, |
| 419 | | {'group': 'two', 'id': 5, 'level': 2, 'name': '1.2.2. CCC', 'parent': 3} |
| 420 | | ] |
| 421 | | check(result, must_be, no=8) |
| 422 | | |
| 423 | | |
| 424 | | result = tree.get_group_dict(group_key="group", id=8) |
| 425 | | must_be = [ |
| 426 | | {'group': 'two', 'id': 7, 'level': 1, 'name': '2.1. EEE', 'parent': 6}, |
| 427 | | {'group': 'two', 'id': 8, 'level': 1, 'name': '2.2. EEE', 'parent': 6} |
| 428 | | ] |
| 429 | | check(result, must_be, no=9) |
| 430 | | |
| 431 | | result = tree.get_group_dict(group_key="parent", id=3) |
| 432 | | must_be = [ |
| 433 | | {'group': 'two', 'id': 2, 'level': 1, 'name': '1.1. BBB', 'parent': 1}, |
| 434 | | {'group': 'two', 'id': 3, 'level': 1, 'name': '1.2. BBB', 'parent': 1}, |
| 435 | | ] |
| 436 | | check(result, must_be, no=10) |
| | 505 | # # |
| | 506 | # # a coherent group, which have the same value in the 'group_key' |
| | 507 | # # |
| | 508 | # result = tree.get_group_dict(group_key="group", id=5, debug=True) |
| | 509 | # must_be = {'active': False, |
| | 510 | # 'group': 'two', |
| | 511 | # 'id': 3, |
| | 512 | # 'level': 0, |
| | 513 | # 'name': '1.2. BBB', |
| | 514 | # 'parent': 1, |
| | 515 | # 'subitems': [{'active': False, |
| | 516 | # 'group': 'two', |
| | 517 | # 'id': 4, |
| | 518 | # 'level': 1, |
| | 519 | # 'name': '1.2.1. CCC', |
| | 520 | # 'parent': 3}, |
| | 521 | # {'active': False, |
| | 522 | # 'group': 'two', |
| | 523 | # 'id': 5, |
| | 524 | # 'level': 1, |
| | 525 | # 'name': '1.2.2. CCC', |
| | 526 | # 'parent': 3}]} |
| | 527 | # check(result, must_be, no=7) |
| | 528 | # |
| | 529 | # result = tree.get_group_dict(group_key="group", id=8, debug=True) |
| | 530 | # must_be = {'active': False, |
| | 531 | # 'group': 'two', |
| | 532 | # 'id': 8, |
| | 533 | # 'level': 0, |
| | 534 | # 'name': '2.2. EEE', |
| | 535 | # 'parent': 6} |
| | 536 | # check(result, must_be, no=8) |
| | 537 | # |
| | 538 | # result = tree.get_group_dict(group_key="parent", id=3, debug=True) |
| | 539 | # must_be = {'active': False, |
| | 540 | # 'group': 'two', |
| | 541 | # 'id': 3, |
| | 542 | # 'level': 0, |
| | 543 | # 'name': '1.2. BBB', |
| | 544 | # 'parent': 1} |
| | 545 | # check(result, must_be, no=9) |
| 444 | | from pprint import pprint |
| 445 | | # |
| | 553 | from pprint import pprint, pformat |
| | 554 | |
| | 555 | data = [ |
| | 556 | {'id': 1, 'parent': None, 'group': 'one', 'name': '1. AAA'}, |
| | 557 | {'id': 2, 'parent': 1, 'group': 'two', 'name': '1.1. BBB'}, |
| | 558 | {'id': 3, 'parent': 1, 'group': 'two', 'name': '1.2. BBB'}, |
| | 559 | {'id': 4, 'parent': 3, 'group': 'two', 'name': '1.2.1. CCC'}, |
| | 560 | {'id': 5, 'parent': 3, 'group': 'two', 'name': '1.2.2. CCC'}, |
| | 561 | {'id': 6, 'parent': None, 'group': 'one', 'name': '2. DDD'}, |
| | 562 | {'id': 7, 'parent': 6, 'group': 'two', 'name': '2.1. EEE'}, |
| | 563 | {'id': 8, 'parent': 6, 'group': 'two', 'name': '2.2. EEE'}, |
| | 564 | ] |
| | 565 | print "Source data:" |
| | 566 | pprint(data) |
| | 567 | |
| | 568 | tree = TreeGenerator(data) |
| | 569 | _test_generator(tree, display_result=False) |
| | 570 | # _test_generator(tree, display_result=True) |
| | 571 | |
| | 572 | print "\nmodule test - END" |
| | 573 | |
| | 574 | |
| | 575 | |
| 447 | | # {'id': 1, 'parent': None, 'group': 'one', 'name': '1. AAA'}, |
| 448 | | # {'id': 2, 'parent': 1, 'group': 'two', 'name': '1.1. BBB'}, |
| 449 | | # {'id': 3, 'parent': 1, 'group': 'two', 'name': '1.2. BBB'}, |
| 450 | | # {'id': 4, 'parent': 3, 'group': 'two', 'name': '1.2.1. CCC'}, |
| 451 | | # {'id': 5, 'parent': 3, 'group': 'two', 'name': '1.2.2. CCC'}, |
| 452 | | # {'id': 6, 'parent': None, 'group': 'one', 'name': '2. DDD'}, |
| 453 | | # {'id': 7, 'parent': 6, 'group': 'two', 'name': '2.1. EEE'}, |
| 454 | | # {'id': 8, 'parent': 6, 'group': 'two', 'name': '2.2. EEE'}, |
| | 577 | # {'id': 1, 'parent': None, 'shortcut': u'index', 'template': 1}, |
| | 578 | # {'id': 4, 'parent': None, 'shortcut': u'SiteMap', 'template': 1}, |
| | 579 | # {'id': 5, 'parent': 1, 'shortcut': u'3dsmax1', 'template': 6}, |
| | 580 | # {'id': 6, 'parent': 5, 'shortcut': u'Geschichte', 'template': 6}, |
| | 581 | # {'id': 7, 'parent': 6, 'shortcut': u'3D-Studio', 'template': 6}, |
| | 582 | # {'id': 8, 'parent': 5, 'shortcut': u'Einsatzgebiete', 'template': 6}, |
| | 583 | # {'id': 9, 'parent': 8, 'shortcut': u'Firmen', 'template': 6}, |
| | 584 | # {'id': 10, 'parent': 8, 'shortcut': u'Spiele', 'template': 6}, |
| | 585 | # {'id': 12, 'parent': 5, 'shortcut': u'Eigenschaften', 'template': 6}, |
| | 586 | # {'id': 13, 'parent': 6, 'shortcut': u'Historie', 'template': 6}, |
| | 587 | # {'id': 14, 'parent': 5, 'shortcut': u'3D-Allgemein', 'template': 6}, |
| | 588 | # {'id': 15, 'parent': 14, 'shortcut': u'Workflow', 'template': 6}, |
| | 589 | # {'id': 16, 'parent': 14, 'shortcut': u'Einsatzgebiete-f-r-3D', 'template': 6}, |
| | 590 | # {'id': 17, 'parent': 14, 'shortcut': u'Warum-3D', 'template': 6}, |
| | 591 | # {'id': 18, 'parent': 12, 'shortcut': u'Produkteigenschaften', 'template': 6}, |
| | 592 | # {'id': 19, 'parent': 12, 'shortcut': u'Modeling', 'template': 6}, |
| | 593 | # {'id': 20, 'parent': 12, 'shortcut': u'Features', 'template': 6}, |
| | 594 | # {'id': 21, 'parent': 5, 'shortcut': u'3D-Software', 'template': 6}, |
| | 595 | # {'id': 22, 'parent': 21, 'shortcut': u'Cinema4D', 'template': 6}, |
| | 596 | # {'id': 23, 'parent': 21, 'shortcut': u'Maya', 'template': 6}, |
| | 597 | # {'id': 24, 'parent': 21, 'shortcut': u'Blender', 'template': 6}, |
| | 598 | # {'id': 25, 'parent': 14, 'shortcut': u'Grenzen', 'template': 6}, |
| | 599 | # {'id': 26, 'parent': 14, 'shortcut': u'Aufteilung', 'template': 6}, |
| | 600 | # {'id': 27, 'parent': 1, 'shortcut': u'test-Pr-sentation', 'template': 6}, |
| | 601 | # {'id': 28, 'parent': 27, 'shortcut': u'1', 'template': 6}, |
| | 602 | # {'id': 29, 'parent': 28, 'shortcut': u'1-1', 'template': 6}, |
| | 603 | # {'id': 30, 'parent': 28, 'shortcut': u'1-2', 'template': 6}, |
| | 604 | # {'id': 31, 'parent': 27, 'shortcut': u'2', 'template': 6}, |
| | 605 | # {'id': 32, 'parent': 31, 'shortcut': u'2-1', 'template': 6}, |
| | 606 | # {'id': 33, 'parent': 31, 'shortcut': u'2-2', 'template': 6} |
| 460 | | # _test_generator(tree, display_result=False) |
| 461 | | ## _test_generator(tree, display_result=True) |
| 462 | | # |
| 463 | | # print "\nmodule test - END" |
| 464 | | |
| 465 | | |
| 466 | | |
| 467 | | data = [ |
| 468 | | {'id': 1, 'parent': None, 'shortcut': u'index', 'template': 1}, |
| 469 | | {'id': 4, 'parent': None, 'shortcut': u'SiteMap', 'template': 1}, |
| 470 | | {'id': 5, 'parent': 1, 'shortcut': u'3dsmax1', 'template': 6}, |
| 471 | | {'id': 6, 'parent': 5, 'shortcut': u'Geschichte', 'template': 6}, |
| 472 | | {'id': 7, 'parent': 6, 'shortcut': u'3D-Studio', 'template': 6}, |
| 473 | | {'id': 8, 'parent': 5, 'shortcut': u'Einsatzgebiete', 'template': 6}, |
| 474 | | {'id': 9, 'parent': 8, 'shortcut': u'Firmen', 'template': 6}, |
| 475 | | {'id': 10, 'parent': 8, 'shortcut': u'Spiele', 'template': 6}, |
| 476 | | {'id': 12, 'parent': 5, 'shortcut': u'Eigenschaften', 'template': 6}, |
| 477 | | {'id': 13, 'parent': 6, 'shortcut': u'Historie', 'template': 6}, |
| 478 | | {'id': 14, 'parent': 5, 'shortcut': u'3D-Allgemein', 'template': 6}, |
| 479 | | {'id': 15, 'parent': 14, 'shortcut': u'Workflow', 'template': 6}, |
| 480 | | {'id': 16, 'parent': 14, 'shortcut': u'Einsatzgebiete-f-r-3D', 'template': 6}, |
| 481 | | {'id': 17, 'parent': 14, 'shortcut': u'Warum-3D', 'template': 6}, |
| 482 | | {'id': 18, 'parent': 12, 'shortcut': u'Produkteigenschaften', 'template': 6}, |
| 483 | | {'id': 19, 'parent': 12, 'shortcut': u'Modeling', 'template': 6}, |
| 484 | | {'id': 20, 'parent': 12, 'shortcut': u'Features', 'template': 6}, |
| 485 | | {'id': 21, 'parent': 5, 'shortcut': u'3D-Software', 'template': 6}, |
| 486 | | {'id': 22, 'parent': 21, 'shortcut': u'Cinema4D', 'template': 6}, |
| 487 | | {'id': 23, 'parent': 21, 'shortcut': u'Maya', 'template': 6}, |
| 488 | | {'id': 24, 'parent': 21, 'shortcut': u'Blender', 'template': 6}, |
| 489 | | {'id': 25, 'parent': 14, 'shortcut': u'Grenzen', 'template': 6}, |
| 490 | | {'id': 26, 'parent': 14, 'shortcut': u'Aufteilung', 'template': 6}, |
| 491 | | {'id': 27, 'parent': 1, 'shortcut': u'test-Pr-sentation', 'template': 6}, |
| 492 | | {'id': 28, 'parent': 27, 'shortcut': u'1', 'template': 6}, |
| 493 | | {'id': 29, 'parent': 28, 'shortcut': u'1-1', 'template': 6}, |
| 494 | | {'id': 30, 'parent': 28, 'shortcut': u'1-2', 'template': 6}, |
| 495 | | {'id': 31, 'parent': 27, 'shortcut': u'2', 'template': 6}, |
| 496 | | {'id': 32, 'parent': 31, 'shortcut': u'2-1', 'template': 6}, |
| 497 | | {'id': 33, 'parent': 31, 'shortcut': u'2-2', 'template': 6} |
| 498 | | ] |
| 499 | | pprint(data) |
| 500 | | print "="*60 |
| 501 | | tree = TreeGenerator(data) |
| 502 | | result = tree.get_group_list(group_key="template", id=30) |
| 503 | | pprint(result) |
| | 611 | # result = tree.get_group_list(group_key="template", id=30) |
| | 612 | # pprint(result) |