serializers.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. import tg
  3. from pod.model import data as pmd
  4. def node_to_dict(node: pmd.PBNode, children_content, new_item_state):
  5. """
  6. children_content may be boolean or a list containing json values
  7. """
  8. url = tg.url('/document/', dict(node_id=node.node_id)) ## FIXME - 2014-05-27 - Make this more flexible
  9. return dict(
  10. id = node.node_id,
  11. children = children_content,
  12. text = node.data_label,
  13. a_attr = { "href" : url },
  14. li_attr = { "title": node.data_label },
  15. type = node.node_type, # this property is understandable by jstree (through "types" plugin)
  16. state = new_item_state,
  17. node_status = node.getStatus().getId() # this is not jstree understandable data. This requires a JS 'success' callback
  18. )
  19. def PBNodeForMenu(func):
  20. def process_item(item: pmd.PBNode):
  21. """ convert given item into a dictionnary """
  22. return node_to_dict(item, item.getChildNb()>0, None)
  23. def pre_serialize(*args, **kws):
  24. initial_result = func(*args, **kws)
  25. real_result = None
  26. print("DEBUG ===================>", initial_result)
  27. if isinstance(initial_result, list):
  28. real_result = list()
  29. for value_item in initial_result:
  30. real_result.append(process_item(value_item))
  31. else:
  32. # We suppose here that we have an object only
  33. real_result = process_item(initial_result)
  34. return dict(d = real_result)
  35. return pre_serialize
  36. def NodeTreeItemForMenu(func):
  37. """ works with structure NodeTreeItem """
  38. def process_item(structure_item: pmd.NodeTreeItem, current_node_id=None):
  39. """ convert given item into a dictionnary """
  40. item = structure_item.node
  41. children = []
  42. for child_item in structure_item.children:
  43. children.append(process_item(child_item, current_node_id))
  44. children_field_value = None
  45. if len(children)>0:
  46. children_field_value = children
  47. elif item.getChildNb()>0:
  48. children_field_value = True
  49. else:
  50. children_field_value = False
  51. new_item_state = dict(
  52. opened = item.getChildNb()<=0 or len(children)>0,
  53. selected = current_node_id!=None and item.node_id==current_node_id,
  54. )
  55. return node_to_dict(item, children_field_value, new_item_state)
  56. def pre_serialize(*args, **kws):
  57. initial_result = func(*args, **kws)
  58. real_result = None
  59. current_node_id = None
  60. if "current_node_id" in kws:
  61. current_node_id = int(kws['current_node_id'])
  62. if isinstance(initial_result, list):
  63. real_result = list()
  64. for value_item in initial_result:
  65. real_result.append(process_item(value_item, current_node_id))
  66. else:
  67. # We suppose here that we have an object only
  68. real_result = process_item(initial_result, current_node_id)
  69. return dict(d = real_result)
  70. return pre_serialize