Skip to content
Merged
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
30 changes: 29 additions & 1 deletion app/api/event_locations.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
from flask_rest_jsonapi import ResourceList
from flask_rest_jsonapi.querystring import QueryStringManager as QSManager
from flask_rest_jsonapi.pagination import add_pagination_links

from flask import request, url_for
from app.api.bootstrap import api
from app.api.schema.event_locations import EventLocationSchema
from app.models import db
from app.models.event import Event
from app.models.event_location import EventLocation
from sqlalchemy import func, desc


class EventLocationList(ResourceList):

"""
List event locations
"""
def get(self, *args, **kwargs):
qs = QSManager(request.args, self.schema)
popular_locations = db.session.query(Event.searchable_location_name, func.count(Event.id).label('counts')) \
.group_by(Event.searchable_location_name) \
.order_by(desc('counts')) \
.limit(6)
locations = []
for location, _ in popular_locations:
if location is not None:
new_location = EventLocation(location)
new_location.id = len(locations)
locations.append(new_location)
schema = EventLocationSchema()
result = schema.dump(locations, many=True).data
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal Had to make these changes to keep it JSON-API compliant. Please review.

view_kwargs = request.view_args if getattr(self, 'view_kwargs', None) is True else dict()
add_pagination_links(result,
len(locations),
qs,
url_for(self.view, **view_kwargs))
result.update({'meta': {'count': len(locations)}})
return result

decorators = (api.has_permission('is_admin', methods="POST"),)
schema = EventLocationSchema
data_layer = {'session': db.session,
'model': EventLocation}
'model': EventLocation
}