Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sorting of entities by attribute of type reference #189

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions backend/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi_pagination.ext.sqlalchemy import paginate_query
from psycopg2.errors import ForeignKeyViolation
from sqlalchemy import func, distinct, column, asc, desc, or_, select, update
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, aliased
from sqlalchemy.exc import IntegrityError, NoResultFound
from sqlalchemy.sql.expression import delete, intersect
from sqlalchemy.sql.selectable import CompoundSelect
Expand Down Expand Up @@ -509,17 +509,24 @@ def get_entities(
except AttributeError:
pass
if order_by != 'name':
attrs = {i.attribute.name: i.attribute for i in schema.attr_defs if i.attribute.type.value.filters}
attrs = {i.attribute.name: i.attribute for i in schema.attr_defs}
attr = attrs[order_by]
value_model = attr.type.value.model
direction = asc if ascending else desc
q = q.order_by(
direction(
select(value_model.value)
.where(value_model.attribute_id == attr.id)
.where(value_model.entity_id == Entity.id).scalar_subquery()
), Entity.name.asc()
)
outer_query = subquery = (select(value_model.value)
.where(value_model.attribute_id == attr.id)
.where(value_model.entity_id == Entity.id)
.scalar_subquery()
.correlate(Entity))
if attr.type == AttrType.FK:
attr_defs = {i.attribute.name: i for i in schema.attr_defs}
attr_def = attr_defs[order_by]
e_alias = aliased(Entity)
outer_query = (select(e_alias.name)
.where(e_alias.schema_id == attr_def.bound_schema_id,
e_alias.id == subquery)
.scalar_subquery())
q = q.order_by(direction(outer_query), Entity.name.asc())
else:
direction = 'asc' if ascending else 'desc'
q = q.order_by(getattr(Entity.name, direction)())
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/EntityListTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default {
return s;
},
orderByField(field) {
if (this.listFields.includes(field) || field in this.fkFields) {
if (this.listFields.includes(field)) {
return;
}
if (this.orderBy === field) {
Expand Down
Loading