Skip to content

Commit

Permalink
fix: Add hack to remove duplicate sessions in user sessions API (#7152)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamareebjamal authored Jul 22, 2020
1 parent 23fe4a5 commit 90369d2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/api/sessions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask import request
from flask_jwt_extended import current_user
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from flask_rest_jsonapi.querystring import QueryStringManager as QSManager

from app.api.bootstrap import api
from app.api.events import Event
Expand Down Expand Up @@ -95,6 +97,28 @@ def after_create_object(self, session, data, view_kwargs):
}


def get_distinct_sort_fields(schema, model, sort=True):
"""Due to the poor code of flask-rest-jsonapi, distinct query needed
in sessions API to remove duplicate sessions can't be sorted on
returning subquery, thus we need to add all sort fields in distinct
group and repeat it in sort group as well"""
fields = []
qs = QSManager(request.args, schema)
for sort_opt in qs.sorting:
field = sort_opt['field']
if not hasattr(model, field):
continue
field = getattr(model, field)
if sort:
field = getattr(field, sort_opt['order'])()
fields.append(field)
field = Session.id
if sort:
field = field.desc()
fields.append(field)
return fields


class SessionList(ResourceList):
"""
List Sessions
Expand Down Expand Up @@ -131,6 +155,8 @@ def query(self, view_kwargs):
or Session.speakers.any(Speaker.user_id == user.id)
)
)
.distinct(*get_distinct_sort_fields(SessionSchema, Session, sort=False))
.order_by(*get_distinct_sort_fields(SessionSchema, Session))
)
query_ = event_query(query_, view_kwargs)
if view_kwargs.get('speaker_id'):
Expand Down

0 comments on commit 90369d2

Please sign in to comment.