SHOW:
|
|
- or go back to the newest paste.
| 1 | @login_required() | |
| 2 | def browse(self, id): | |
| 3 | schema = validation.FolderBrowse() | |
| 4 | try: | |
| 5 | data = schema.to_python(request.GET) | |
| 6 | except formencode.Invalid, error: | |
| 7 | abort(500) | |
| 8 | ||
| 9 | # Count how much children we have for a given container | |
| 10 | t = meta.table.content | |
| 11 | q_count_children = sql.select([ | |
| 12 | t.c.container_id, sql.func.count('*').label('cpt')
| |
| 13 | ]).group_by(t.c.container_id).alias('count_children')
| |
| 14 | ||
| 15 | # Filter results | |
| 16 | sort_by = data['sort'] | |
| 17 | if sort_by == 'date': | |
| 18 | sort_by = sql.func.coalesce(model.Content.updated, | |
| 19 | model.Content.added) | |
| 20 | elif sort_by == 'type': | |
| 21 | sort_by = model.Content.content_type_id | |
| 22 | else: | |
| 23 | sort_by = sql.column(sort_by) | |
| 24 | ||
| 25 | if data['order'] == 'desc': | |
| 26 | sort_by = sort_by.desc() | |
| 27 | ||
| 28 | # Query database | |
| 29 | c.results = meta.Session.query(model.Content, q_count_children.c.cpt).\ | |
| 30 | outerjoin(q_count_children).\ | |
| 31 | filter(model.Content.container_id == id).\ | |
| 32 | order_by(sort_by).offset(data['offset']).\ | |
| 33 | limit(data['limit']) | |
| 34 | ||
| 35 | return render('folder/_browse.html') |