Skip to content
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

browse department as directory #533

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

tomx4096
Copy link
Contributor

Status

Ready for review / in progress

Description of Changes

Related to #527 #474 #500 , provides a directory view of the department that users can quickly Ctrl-F through if they know name or star number.

Changes proposed in this pull request:

  • Add a departmental view button to browse.html
  • Leads to directory page that listing all officers in a department, with links to officer profiles

Notes for Deployment

  • Depends on maintaining ranks in RANK_CHOICES in high-to-low order, excluding Not Sure
  • Only Commander, Captain, PO, and Not Sure rank included in template right now

Screenshots (if appropriate)

screenshot from 2018-08-12 03-19-20
screenshot from 2018-08-12 03-19-09
screenshot from 2018-08-12 03-18-44
screenshot from 2018-08-12 03-18-57

Tests and linting

  • I have rebased my changes on current develop

  • pytests pass in the development environment on my local machine

  • flake8 checks pass

@dismantl
Copy link
Member

This is great, thanks! I was able to get it to work with the test data, but when I tried to use Baltimore data the ranks don't match up with what's in RANK_CHOICES and so it throws an error. Is there a way this could gracefully handle departments that have different sets of ranks?

@tomx4096
Copy link
Contributor Author

tomx4096 commented Aug 14, 2018

Thanks for the feedback. You're right, it could be hard to manually maintain proper rank names for many departments with slight variations. I pushed another approach that makes it independent of RANK_CHOICES. It makes up a list of ranks found in the data and uses those to demarcate sections in the directory view. The html was also generalized so all ranks found would be displayed, with Not Sure at the end, and a random (blue-ish) background color.

The disadvantages of this approach are
-they aren't guaranteed to be ordered from high to low rank in the directory view, and
-the ranks in the data must be consistent, no typos or alternate names. this type of validation/cleaning should happen when the data is imported imho.

Let me know if it fixes your error, I can also try your dataset if you like, just let me know where to find it.

rank_types = ["NOT SURE"]

for officer in officers:
rec = assign_dict[officer.id]
Copy link
Member

Choose a reason for hiding this comment

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

It's possible there exist officers that don't have assignments, so this should probably be wrapped in a try block:

try:
    rec = assign_dict[officer.id]
except KeyError:
    continue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, i hadn't considered this case. fixed in last commit

@dismantl
Copy link
Member

Cool, I like that approach and I was able to get it working for Baltimore data. We have like dozens of job titles, some with only 1 or 2 employees, so that made the list pretty long and unwieldy. But the solution to that is probably just to filter out what officers I import (e.g. not forensic scientists).

I wonder if the best way to allow department-specific ranks but not have the disadvantages you listed above, would be to have a per-department list of ranks provided when a department is created. Maybe by uploading an ordered CSV or otherwise storing it in the database? But that may also be a follow-on issue beyond the scope of this PR :)

if safe_rank not in officer_nodes.keys():
officer_nodes[safe_rank] = []
else:
officer_nodes[safe_rank].append({'id': officer.id,
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be in an else block or it won't add the first officer of a rank it finds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah ha! thanks for catching this, fixed in last commit

@tomx4096 tomx4096 mentioned this pull request Aug 16, 2018
3 tasks
Copy link
Member

@redshiftzero redshiftzero left a comment

Choose a reason for hiding this comment

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

This is super sweet! One speedup suggestion in the view function for this (we have a LOT of officers in some departments, e.g. NYPD has 40k+ cops and is on its way into the openoversight)

abort(404)

officers = Officer.query.filter(Officer.department_id == department_id).all()
# map each officer to their most recent assignment
Copy link
Member

Choose a reason for hiding this comment

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

a faster (i.e. to avoid the first for loop below) way to get the most recent assignment is to use the assignments relationship on the Officer object:

Officer.query.first().assignments[-1]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

at first i tried using this relationship, but I found that accessing assignments on each Officer was very slow (sometimes timing out the browser on the test data). i found it much faster to query all Assignments at once and then associate them to officers in the loop. additionally, i wasn't sure that the assignments would be guaranteed to be in chronological order in the list

@redshiftzero
Copy link
Member

I wonder if the best way to allow department-specific ranks but not have the disadvantages you listed above, would be to have a per-department list of ranks provided when a department is created. Maybe by uploading an ordered CSV or otherwise storing it in the database?

👍 to this, storing the ranks in the database is the right way to go (fine to do in a separate issue/PR imo, see #326)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants