-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* officer end point * officer end point * updated * update * clean update
- Loading branch information
1 parent
bc62e1d
commit ce7f97f
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import logging | ||
from operator import or_ | ||
from typing import Optional | ||
from venv import logger | ||
|
||
from backend.auth.jwt import min_role_required | ||
from backend.database.models.agency import JURISDICTION | ||
from backend.mixpanel.mix import track_to_mp | ||
from mixpanel import MixpanelException | ||
from backend.database.models.user import UserRole | ||
from flask import Blueprint, abort, jsonify, request | ||
from flask_jwt_extended.view_decorators import jwt_required | ||
from pydantic import BaseModel | ||
|
||
from ..database import Officer, db, agency_officer, Agency | ||
from ..schemas import ( | ||
Officer_orm_to_json, | ||
validate, | ||
) | ||
|
||
|
||
bp = Blueprint("officer_routes", __name__, url_prefix="/api/v1/officers") | ||
|
||
class SearchOfficerSchema(BaseModel): | ||
officerName: Optional[str] = None | ||
location:Optional[str]=None | ||
badgeNumber:Optional[str]=None | ||
page: Optional[int] = 1 | ||
perPage: Optional[int] = 20 | ||
|
||
class Config: | ||
extra = "forbid" | ||
schema_extra = { | ||
"example": { | ||
"officerName": "John Doe", | ||
"location":"New York", | ||
"badgeNumber":1234, | ||
"page": 1, | ||
"perPage": 20, | ||
} | ||
} | ||
|
||
@bp.route("/search/officer",methods=["POST"]) | ||
@jwt_required() | ||
@min_role_required(UserRole.PUBLIC) | ||
@validate(json=SearchOfficerSchema) | ||
def search_officer(): | ||
"""Search Officers""" | ||
body:SearchOfficerSchema=request.context.json | ||
|
||
query = db.session.query('Officer') | ||
try: | ||
|
||
if body.officerName: | ||
names = body.officerName.split() | ||
first_name = names[0] if len(names) > 0 else '' | ||
last_name = names[1] if len(names) > 1 else '' | ||
query = Officer.query.filter(or_( | ||
Officer.first_name.ilike(f"%{first_name}%"), | ||
Officer.last_name.ilike(f"%{last_name}%") | ||
)) | ||
|
||
if body.badgeNumber: | ||
officer_ids = [result.officer_id for result in db.session.query(agency_officer).filter_by(badge_number=body.badgeNumber).all()] | ||
query = Officer.query.filter(Officer.id.in_(officer_ids)).all() | ||
|
||
except Exception as e: | ||
abort(422,description=str(e)) | ||
|
||
results = query.paginate( | ||
page=body.page, per_page=body.perPage, max_per_page=100 | ||
) | ||
|
||
try: | ||
track_to_mp(request, "search_officer", { | ||
"officername": body.officerName, | ||
"badgeNumber": body.badgeNumber | ||
}) | ||
except MixpanelException as e: | ||
logger.error(e) | ||
try: | ||
return{ | ||
"results":[ | ||
|
||
Officer_orm_to_json(result) for result in results.items | ||
], | ||
"page": results.page, | ||
"totalPages": results.pages, | ||
"totalResults": results.total, | ||
} | ||
except Exception as e: | ||
abort(500, description=str(e)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters