Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. def includeme(config):
  2. tables = config.registry['metadata'].tables
  3.  
  4. config.include('amnesia.modules.authentication.mapper')
  5. config.include('amnesia.modules.state.mapper')
  6. config.include('amnesia.modules.content_type.mapper')
  7. config.include('amnesia.modules.tag.mapper')
  8.  
  9. _count_alias = tables['content'].alias('_count_children')
  10.  
  11. orm.mapper(
  12. Content, tables['content'],
  13. polymorphic_on=tables['content'].c.content_type_id,
  14. properties={
  15.  
  16. # no need to load this column by default
  17. 'fts': orm.deferred(tables['content'].c.fts),
  18.  
  19. #################
  20. # RELATIONSHIPS #
  21. #################
  22.  
  23. 'owner': orm.relationship(
  24. Account,
  25. lazy='joined',
  26. innerjoin=True,
  27. backref=orm.backref('contents', lazy='dynamic',
  28. cascade='all, delete-orphan')
  29. ),
  30.  
  31. 'state': orm.relationship(
  32. State,
  33. lazy='joined',
  34. innerjoin=True
  35. ),
  36.  
  37. 'type': orm.relationship(
  38. ContentType,
  39. lazy='joined',
  40. innerjoin=True
  41. ),
  42.  
  43. 'tags': orm.relationship(
  44. Tag,
  45. secondary=tables['content_tag']
  46. ),
  47.  
  48. 'parent': orm.relationship(
  49. Folder,
  50. foreign_keys=tables['content'].c.container_id,
  51. innerjoin=True,
  52. uselist=False,
  53. backref=orm.backref('children', cascade='all, delete-orphan')
  54. ),
  55.  
  56. #####################
  57. # COLUMN PROPERTIES #
  58. #####################
  59.  
  60. 'position_in_container': orm.column_property(
  61. sql.func.row_number().
  62. over(partition_by=tables['content'].c.container_id,
  63. order_by=tables['content'].c.weight.desc()),
  64. deferred=True,
  65. group='window_func'
  66. ),
  67.  
  68. # FIXME: move to folder mapper with a LATERAL expression
  69. 'count_children' : orm.column_property(
  70. sql.select([sql.func.count()]).where(
  71. _count_alias.c.container_id == tables['content'].c.id
  72. ).correlate(tables['content']).label('count_children'),
  73. deferred = True
  74. ),
  75.  
  76. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement