Skip to content

Commit

Permalink
Merge pull request #2650 from tkalir/2648-adding_endpoints_for_locati…
Browse files Browse the repository at this point in the history
…on_debugging

adding /api/news-flash/by-resolution endpoint
  • Loading branch information
tkalir authored May 12, 2024
2 parents e4beaa2 + 8bf386a commit 8a94090
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
44 changes: 41 additions & 3 deletions anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from flask_babel import Babel, gettext
from flask_compress import Compress
from flask_cors import CORS
from flask_restx import Resource, fields, reqparse
from flask_restx import Resource, fields, reqparse, Model
from sqlalchemy import and_, not_, or_
from sqlalchemy import func
from webassets import Environment as AssetsEnvironment, Bundle as AssetsBundle
Expand Down Expand Up @@ -57,7 +57,8 @@
City,
Streets,
Comment,
TelegramForwardedMessages
TelegramForwardedMessages,
NewsFlash
)
from anyway.request_params import get_request_params_from_request_values
from anyway.views.news_flash.api import (
Expand All @@ -70,6 +71,7 @@
DEFAULT_LIMIT_REQ_PARAMETER,
DEFAULT_OFFSET_REQ_PARAMETER,
DEFAULT_NUMBER_OF_YEARS_AGO,
search_newsflashes_by_resolution
)
from anyway.views.schools.api import (
schools_description_api,
Expand Down Expand Up @@ -1128,6 +1130,19 @@ def acc_in_area_query():
help="limit number of retrieved items to given limit",
)

def is_true(value):
return value.lower() == 'true'

newsflash_fields = [column.name for column in NewsFlash.__table__.columns]
nfbr_parser = reqparse.RequestParser()
nfbr_parser.add_argument("resolutions", type=str, action="append", required=True,
help="List of resolutions to filter by")
nfbr_parser.add_argument("include", type=is_true, required=True,
help="Flag to include or exclude the specified resolutions")
nfbr_parser.add_argument("limit", type=int, help="Maximum number of records to return")
nfbr_parser.add_argument("fields", type=str, choices=newsflash_fields, action="append",
help="List of fields to include in the response")


def datetime_to_str(val: datetime.datetime) -> str:
return val.strftime("%Y-%m-%d %H:%M:%S") if isinstance(val, datetime.datetime) else "None"
Expand Down Expand Up @@ -1184,7 +1199,30 @@ def patch(self, news_flash_id):
return update_news_flash_qualifying(news_flash_id)
def options(self, news_flash_id):
return single_news_flash(news_flash_id)



def filter_json_fields(json_data, fields):
return {field: json_data[field] for field in fields if field in json_data}


@api.route("/api/news-flash/by-resolution", methods=["GET"])
class RetrieveNewsFlashByResolution(Resource):
@api.doc("get news flash records by resolution")
@api.expect(nfbr_parser)
@api.response(404, "Parameter value not supported or missing")
@api.response(
200, "Retrieve news-flash items filtered by given parameters", news_flash_list_model
)
def get(self):
args = nfbr_parser.parse_args()
limit = args["limit"] if "limit" in args else None
query = search_newsflashes_by_resolution(db.session, args["resolutions"], args["include"], limit)
res = query.all()
news_flashes_jsons = [n.serialize() for n in res]
logging.debug(news_flashes_jsons)
filtered_jsons = [filter_json_fields(json_data, args["fields"]) for json_data in news_flashes_jsons]
return Response(json.dumps(filtered_jsons, default=str), mimetype="application/json")


@api.route("/api/news-flash-new", methods=["GET"])
class RetrieveNewsFlash(Resource):
Expand Down
16 changes: 16 additions & 0 deletions anyway/views/news_flash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,19 @@ def get_downloaded_data(format, years_ago):

headers = { 'Content-Disposition': f'attachment; filename=anyway_download_{datetime.datetime.now().strftime("%d_%m_%Y_%H_%M_%S")}.{file_type}' }
return Response(buffer.getvalue(), mimetype=mimetype, headers=headers)


def search_newsflashes_by_resolution(session, resolutions, include_resolutions, limit=None):
query = session.query(NewsFlash)
if include_resolutions:
query = query.filter(NewsFlash.resolution.in_(resolutions))
else:
query = query.filter(NewsFlash.resolution.notin_(resolutions))

query = query.filter(NewsFlash.accident == True) \
.order_by(NewsFlash.date.desc())

limit = DEFAULT_LIMIT_REQ_PARAMETER if not limit else limit
query = query.limit(limit)

return query

0 comments on commit 8a94090

Please sign in to comment.