mirror of
https://github.com/lucaspalomodevelop/eventcally.git
synced 2026-03-13 00:07:22 +00:00
Add sort parameter #191
This commit is contained in:
parent
984d15e1c7
commit
cce4dd21c0
@ -1,8 +1,7 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from flask import Flask, jsonify, redirect, request, url_for
|
||||
from flask import Flask
|
||||
from flask_babelex import Babel
|
||||
from flask_cors import CORS
|
||||
from flask_gzip import Gzip
|
||||
@ -11,7 +10,6 @@ from flask_migrate import Migrate
|
||||
from flask_qrcode import QRcode
|
||||
from flask_security import Security, SQLAlchemySessionUserDatastore
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from webargs import flaskparser
|
||||
|
||||
from project.custom_session_interface import CustomSessionInterface
|
||||
|
||||
|
||||
@ -228,6 +228,9 @@ class EventSearchRequestSchema(PaginationRequestSchema):
|
||||
"description": "Looks for events at this weekdays (0=Sunday, 1=Monday, ..)."
|
||||
},
|
||||
)
|
||||
sort = fields.Str(
|
||||
metadata={"description": "Sort result items."},
|
||||
)
|
||||
|
||||
|
||||
class EventSearchResponseSchema(PaginationResponseSchema):
|
||||
|
||||
@ -4,8 +4,8 @@ import icalendar
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from flask import url_for
|
||||
from flask_babelex import format_date, format_time
|
||||
from sqlalchemy import and_, func, or_
|
||||
from sqlalchemy.orm import contains_eager, defaultload, joinedload, lazyload
|
||||
from sqlalchemy import and_, case, func, or_
|
||||
from sqlalchemy.orm import aliased, contains_eager, defaultload, joinedload, lazyload
|
||||
from sqlalchemy.sql import extract
|
||||
|
||||
from project import db
|
||||
@ -85,14 +85,18 @@ def get_event_dates_query(params):
|
||||
|
||||
event_filter = fill_event_filter(event_filter, params)
|
||||
|
||||
admin_unit_reference = None
|
||||
if params.admin_unit_id:
|
||||
admin_unit_refs_subquery = EventReference.query.filter(
|
||||
EventReference.admin_unit_id == params.admin_unit_id
|
||||
).subquery()
|
||||
admin_unit_reference = aliased(EventReference, admin_unit_refs_subquery)
|
||||
|
||||
event_filter = and_(
|
||||
event_filter,
|
||||
or_(
|
||||
Event.admin_unit_id == params.admin_unit_id,
|
||||
Event.references.any(
|
||||
EventReference.admin_unit_id == params.admin_unit_id
|
||||
),
|
||||
admin_unit_reference.id.isnot(None),
|
||||
),
|
||||
)
|
||||
|
||||
@ -107,11 +111,21 @@ def get_event_dates_query(params):
|
||||
weekdays = params.weekday
|
||||
date_filter = and_(date_filter, extract("dow", EventDate.start).in_(weekdays))
|
||||
|
||||
return (
|
||||
result = (
|
||||
EventDate.query.join(EventDate.event)
|
||||
.join(Event.event_place, isouter=True)
|
||||
.join(EventPlace.location, isouter=True)
|
||||
.options(
|
||||
)
|
||||
|
||||
if admin_unit_reference:
|
||||
result = result.join(
|
||||
admin_unit_reference,
|
||||
Event.id == admin_unit_reference.event_id,
|
||||
isouter=True,
|
||||
)
|
||||
|
||||
result = (
|
||||
result.options(
|
||||
contains_eager(EventDate.event)
|
||||
.contains_eager(Event.event_place)
|
||||
.contains_eager(EventPlace.location),
|
||||
@ -128,9 +142,27 @@ def get_event_dates_query(params):
|
||||
)
|
||||
.filter(date_filter)
|
||||
.filter(event_filter)
|
||||
.order_by(EventDate.start)
|
||||
)
|
||||
|
||||
if params.sort == "-rating":
|
||||
if admin_unit_reference:
|
||||
result = result.order_by(
|
||||
case(
|
||||
[
|
||||
(
|
||||
admin_unit_reference.rating.isnot(None),
|
||||
admin_unit_reference.rating,
|
||||
),
|
||||
],
|
||||
else_=Event.rating,
|
||||
).desc()
|
||||
)
|
||||
else:
|
||||
result = result.order_by(Event.rating.desc())
|
||||
|
||||
result = result.order_by(EventDate.start)
|
||||
return result
|
||||
|
||||
|
||||
def get_event_date_with_details_or_404(event_id):
|
||||
return (
|
||||
|
||||
@ -24,6 +24,7 @@ class EventSearchParams(object):
|
||||
self.category_id = None
|
||||
self.organizer_id = None
|
||||
self.weekday = None
|
||||
self.sort = None
|
||||
|
||||
@property
|
||||
def date_from(self):
|
||||
@ -112,3 +113,6 @@ class EventSearchParams(object):
|
||||
|
||||
if "organizer_id" in request.args:
|
||||
self.organizer_id = request.args["organizer_id"]
|
||||
|
||||
if "sort" in request.args:
|
||||
self.sort = request.args["sort"]
|
||||
|
||||
2
project/static/angular-elements.js
vendored
2
project/static/angular-elements.js
vendored
File diff suppressed because one or more lines are too long
@ -645,7 +645,7 @@
|
||||
<div class="mr-4 float-sm-left">{{ render_logo(event.admin_unit.logo) }}</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="font-weight-bold"><a href="{{ url_for('organization', id=event.admin_unit.id) }}">{{ event.admin_unit.name }}</a></div>
|
||||
<div class="font-weight-bold"><a href="{{ url_for('organization_by_name', au_short_name=event.admin_unit.short_name) }}">{{ event.admin_unit.name }}</a></div>
|
||||
|
||||
{{ render_link_prop(event.admin_unit.url) }}
|
||||
{{ render_email_prop(event.admin_unit.email) }}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
{% block content_container_attribs %}{% endblock %}
|
||||
{% block content %}
|
||||
|
||||
<organization-landing-page basepath="{{ url_for('home', _external=True) }}" organizationId="{{ organization.id }}"></organization-landing-page>
|
||||
<organization-landing-page basepath="{{ url_for('home', _external=True) }}" organizationId="{{ organization.id }}" {% if "r" in request.args %}datefilterpreset="{{ request.args.get("r") }}{% endif %}"></organization-landing-page>
|
||||
<script type="text/javascript" src="{{ url_for('static', filename='angular-elements.js')}}"></script>
|
||||
|
||||
{% endblock %}
|
||||
@ -9,3 +9,12 @@ def organization(id):
|
||||
organization = AdminUnit.query.get_or_404(id)
|
||||
|
||||
return render_template("organization/read.html", organization=organization)
|
||||
|
||||
|
||||
@app.route("/org/<string:au_short_name>")
|
||||
def organization_by_name(au_short_name):
|
||||
organization = AdminUnit.query.filter(
|
||||
AdminUnit.short_name == au_short_name
|
||||
).first_or_404()
|
||||
|
||||
return render_template("organization/read.html", organization=organization)
|
||||
|
||||
@ -18,5 +18,5 @@ def test_search(client, seeder, utils):
|
||||
user_id, admin_unit_id = seeder.setup_base()
|
||||
seeder.create_event(admin_unit_id)
|
||||
|
||||
url = utils.get_url("api_v1_event_date_search")
|
||||
url = utils.get_url("api_v1_event_date_search", sort="-rating")
|
||||
utils.get_ok(url)
|
||||
|
||||
@ -16,7 +16,9 @@ def test_event_date_search(client, seeder, utils):
|
||||
user_id, admin_unit_id = seeder.setup_base()
|
||||
seeder.create_event(admin_unit_id)
|
||||
|
||||
url = utils.get_url("api_v1_organization_event_date_search", id=admin_unit_id)
|
||||
url = utils.get_url(
|
||||
"api_v1_organization_event_date_search", id=admin_unit_id, sort="-rating"
|
||||
)
|
||||
utils.get_ok(url)
|
||||
|
||||
|
||||
|
||||
@ -4,3 +4,11 @@ def test_read(client, seeder, utils):
|
||||
|
||||
url = utils.get_url("organization", id=1)
|
||||
utils.get_ok(url)
|
||||
|
||||
|
||||
def test_read_by_name(client, seeder, utils):
|
||||
user_id, admin_unit_id = seeder.setup_base()
|
||||
seeder.create_event(admin_unit_id)
|
||||
|
||||
url = utils.get_url("organization_by_name", au_short_name="meinecrew")
|
||||
utils.get_ok(url)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user