Skip to content

Commit 90369d2

Browse files
fix: Add hack to remove duplicate sessions in user sessions API (#7152)
1 parent 23fe4a5 commit 90369d2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

app/api/sessions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from flask import request
12
from flask_jwt_extended import current_user
23
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
4+
from flask_rest_jsonapi.querystring import QueryStringManager as QSManager
35

46
from app.api.bootstrap import api
57
from app.api.events import Event
@@ -95,6 +97,28 @@ def after_create_object(self, session, data, view_kwargs):
9597
}
9698

9799

100+
def get_distinct_sort_fields(schema, model, sort=True):
101+
"""Due to the poor code of flask-rest-jsonapi, distinct query needed
102+
in sessions API to remove duplicate sessions can't be sorted on
103+
returning subquery, thus we need to add all sort fields in distinct
104+
group and repeat it in sort group as well"""
105+
fields = []
106+
qs = QSManager(request.args, schema)
107+
for sort_opt in qs.sorting:
108+
field = sort_opt['field']
109+
if not hasattr(model, field):
110+
continue
111+
field = getattr(model, field)
112+
if sort:
113+
field = getattr(field, sort_opt['order'])()
114+
fields.append(field)
115+
field = Session.id
116+
if sort:
117+
field = field.desc()
118+
fields.append(field)
119+
return fields
120+
121+
98122
class SessionList(ResourceList):
99123
"""
100124
List Sessions
@@ -131,6 +155,8 @@ def query(self, view_kwargs):
131155
or Session.speakers.any(Speaker.user_id == user.id)
132156
)
133157
)
158+
.distinct(*get_distinct_sort_fields(SessionSchema, Session, sort=False))
159+
.order_by(*get_distinct_sort_fields(SessionSchema, Session))
134160
)
135161
query_ = event_query(query_, view_kwargs)
136162
if view_kwargs.get('speaker_id'):

0 commit comments

Comments
 (0)