Skip to content

Added Unit and Ethnicity Filters #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def register_routes(app: Flask):
# app.register_blueprint(incidents_bp)
app.register_blueprint(auth_bp)
app.register_blueprint(healthcheck_bp)
app.register_blueprint(officers_bp)
app.register_blueprint(officers_bp, url_prefix="/api/v1/officers")
app.register_blueprint(agencies_bp)

@app.route("/")
Expand Down
2 changes: 1 addition & 1 deletion backend/database/models/officer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ class Officer(StructuredNode, JsonSerializable):
'backend.database.models.source.Source', "UPDATED_BY", model=Citation)

def __repr__(self):
return f"<Officer {self.id}>"
return f"<Officer {self.element_id}>"
46 changes: 39 additions & 7 deletions backend/routes/officers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from flask_jwt_extended import get_jwt
from flask_jwt_extended.view_decorators import jwt_required
from pydantic import BaseModel

from neomodel import db

bp = Blueprint("officer_routes", __name__, url_prefix="/api/v1/officers")

Expand Down Expand Up @@ -160,12 +160,11 @@ def create_officer():
def get_officer(officer_uid: int):
"""Get an officer profile.
"""
o = Officer.nodes.get_or_none(uid=officer_uid)
o = Officer.nodes.get_or_none(element_id=officer_uid)
if o is None:
abort(404, description="Officer not found")
return o.to_json()


# Get all officers
@bp.route("/", methods=["GET"])
@jwt_required()
Expand All @@ -175,14 +174,47 @@ def get_all_officers():
Accepts Query Parameters for pagination:
per_page: number of results per page
page: page number
unit_name: filter by unit name
ethnicity: filter by officer ethnicity
"""
args = request.args
q_page = args.get("page", 1, type=int)
q_per_page = args.get("per_page", 20, type=int)

all_officers = Officer.nodes.all()
results = paginate_results(all_officers, q_page, q_per_page)

unit_name = args.get("unit_name", None)
ethnicity = args.get("ethnicity", None) # New parameter
# Base query
query = "MATCH (o:Officer)"
conditions = []

# Add unit filter if provided
if unit_name:
query += " MATCH (o)-[]-(u:Unit)"
conditions.append("u.name CONTAINS $unit_name")

# Add ethnicity filter if provided
if ethnicity:
conditions.append("o.ethnicity = $ethnicity")
# Combine conditions
if conditions:
query += " WHERE " + " AND ".join(conditions)
# Add pagination
query += " RETURN o SKIP $skip LIMIT $limit"
# Calculate pagination values
skip = (q_page - 1) * q_per_page
limit = q_per_page
# Execute query
all_officers, _ = db.cypher_query(
query,
{
"unit_name": unit_name,
"ethnicity": ethnicity, # Include new param
"skip": skip,
"limit": limit
}
)
# Process results
officers = [Officer.inflate(record[0]) for record in all_officers]
results = paginate_results(officers, q_page, q_per_page)
return ordered_jsonify(results), 200


Expand Down
9 changes: 9 additions & 0 deletions backend/run_dev.sh
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file should be removed.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

export FLASK_ENV=${FLASK_ENV:-development}
export PYTHONPATH=.

#flask psql create
#flask psql init
flask db seed
flask run --host=0.0.0.0 --port=${PORT:-5000}
Loading
Loading