| | 48 | |
|---|
| | 49 | def follow_foreign_key(column): |
|---|
| | 50 | columns = util.Set([column]) |
|---|
| | 51 | while column.foreign_key: |
|---|
| | 52 | column = column.foreign_key.column |
|---|
| | 53 | if column in columns: |
|---|
| | 54 | break |
|---|
| | 55 | else: |
|---|
| | 56 | columns.add(column) |
|---|
| | 57 | return column |
|---|
| | 58 | |
|---|
| | 59 | class Node(object): |
|---|
| | 60 | def __repr__(self): |
|---|
| | 61 | cols = self.__class__.c.keys() |
|---|
| | 62 | return "%s(%s)" % ( |
|---|
| | 63 | self.__class__.__name__, |
|---|
| | 64 | ", ".join(["%s=%r" % (col, getattr(self, col)) for col in cols]) |
|---|
| | 65 | ) |
|---|
| | 66 | |
|---|
| | 67 | @classmethod |
|---|
| | 68 | def get_content_type_by_url(cls, url, parent=None): |
|---|
| | 69 | try: |
|---|
| | 70 | parent = parent.content_id |
|---|
| | 71 | except AttributeError: |
|---|
| | 72 | pass |
|---|
| | 73 | |
|---|
| | 74 | latest_revision = func.max(cls.c.revision_id).label('latest_revision') |
|---|
| | 75 | content_id = follow_foreign_key(cls.c.content_id) |
|---|
| | 76 | content_table = content_id.table |
|---|
| | 77 | query = cls.query().add_column(content_table.c.content_type) |
|---|
| | 78 | node, content_type = query.selectone(and_( |
|---|
| | 79 | cls.c.url == url, cls.c.parent_id == parent, |
|---|
| | 80 | content_table.c.content_id == cls.c.content_id |
|---|
| | 81 | )) |
|---|
| | 82 | return content_type |
|---|
| | 83 | |
|---|
| | 84 | assign_mapper(session, Node, node_table) |
|---|