-
Notifications
You must be signed in to change notification settings - Fork 79
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
Officer unit view #783
Officer unit view #783
Changes from 4 commits
41bfd77
5f2b15c
801ab74
cabec8b
419c456
dced1e3
7e0d1b5
943b62c
0e59d9b
cd137cd
95492a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ class FindOfficerForm(Form): | |
unique_internal_identifier = StringField('unique_internal_identifier', default='', validators=[Regexp(r'\w*'), Length(max=55)]) | ||
dept = QuerySelectField('dept', validators=[DataRequired()], | ||
query_factory=dept_choices, get_label='name') | ||
unit = SelectField('unit', default='Not Sure') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are using a |
||
rank = StringField('rank', default='Not Sure', validators=[Optional()]) # Gets rewritten by Javascript | ||
race = SelectField('race', default='Not Sure', choices=RACE_CHOICES, | ||
validators=[AnyOf(allowed_values(RACE_CHOICES))]) | ||
|
@@ -384,6 +385,7 @@ class IncidentForm(DateFieldForm): | |
class BrowseForm(Form): | ||
rank = QuerySelectField('rank', validators=[Optional()], get_label='job_title', | ||
get_pk=lambda job: job.job_title) # query set in view function | ||
unit = SelectField('unit', default='Not Sure') | ||
name = StringField('Last name') | ||
badge = StringField('Badge number') | ||
unique_internal_identifier = StringField('Unique ID') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import datetime | ||
from typing import List | ||
|
||
from flask import current_app | ||
from io import BytesIO | ||
import pytest | ||
|
@@ -16,7 +18,7 @@ | |
|
||
from OpenOversight.app import create_app, models | ||
from OpenOversight.app.utils import merge_dicts | ||
from OpenOversight.app.models import db as _db | ||
from OpenOversight.app.models import db as _db, Unit, Job, Officer | ||
|
||
factory = Faker() | ||
|
||
|
@@ -39,6 +41,22 @@ def pick_birth_date(): | |
return random.randint(1950, 2000) | ||
|
||
|
||
def pick_date(seed: bytes = None, start_year=2000, end_year=2020): | ||
# source: https://stackoverflow.com/questions/40351791/how-to-hash-strings-into-a-float-in-01 | ||
# Wanted to deterministically create a date from a seed string (e.g. the hash or uuid on an officer object) | ||
from struct import unpack | ||
from hashlib import sha256 | ||
|
||
def bytes_to_float(b): | ||
return float(unpack('L', sha256(b).digest()[:8])[0]) / 2 ** 64 | ||
|
||
if seed is None: | ||
seed = str(uuid.uuid4()).encode('utf-8') | ||
|
||
return datetime.datetime(start_year, 1, 1, 00, 00, 00) \ | ||
+ datetime.timedelta(days=365 * (end_year - start_year) * bytes_to_float(seed)) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
|
||
def pick_race(): | ||
return random.choice(['WHITE', 'BLACK', 'HISPANIC', 'ASIAN', | ||
'PACIFIC ISLANDER', 'Not Sure']) | ||
|
@@ -95,9 +113,11 @@ def generate_officer(): | |
) | ||
|
||
|
||
def build_assignment(officer, unit, jobs): | ||
def build_assignment(officer: Officer, units: List[Unit], jobs: Job): | ||
return models.Assignment(star_no=pick_star(), job_id=random.choice(jobs).id, | ||
officer=officer) | ||
officer=officer, unit_id=random.choice(units).id, | ||
star_date=pick_date(officer.full_name().encode('utf-8')), | ||
resign_date=pick_date(officer.full_name().encode('utf-8'))) | ||
|
||
|
||
def build_note(officer, user): | ||
|
@@ -131,10 +151,11 @@ def build_salary(officer): | |
|
||
def assign_faces(officer, images): | ||
if random.uniform(0, 1) >= 0.5: | ||
for num in range(1, len(images)): | ||
return models.Face(officer_id=officer.id, | ||
img_id=num, | ||
original_image_id=random.choice(images).id) | ||
# for num in range(1, len(images)): # <-- bug here, we always return on the first one, the for-loop is unneeded | ||
img_id = random.choice(images).id | ||
return models.Face(officer_id=officer.id, | ||
img_id=img_id, | ||
original_image_id=img_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixing a bug - patch-pulled this in from my |
||
else: | ||
return False | ||
|
||
|
@@ -249,7 +270,15 @@ def add_mockdata(session): | |
SEED = current_app.config['SEED'] | ||
random.seed(SEED) | ||
|
||
unit1 = models.Unit(descrip="test") | ||
test_units = [ | ||
models.Unit(descrip="test", department_id=1), | ||
models.Unit(descrip='District 13', department_id=1), | ||
models.Unit(descrip='Donut Devourers', department_id=1), | ||
models.Unit(descrip='Bureau of Organized Crime', department_id=2), | ||
models.Unit(descrip='Porky\'s BBQ: Rub Division', department_id=2) | ||
] | ||
session.add_all(test_units) | ||
session.commit() | ||
|
||
test_images = [models.Image(filepath='/static/images/test_cop{}.png'.format(x + 1), department_id=1) for x in range(5)] + \ | ||
[models.Image(filepath='/static/images/test_cop{}.png'.format(x + 1), department_id=2) for x in range(5)] | ||
|
@@ -270,15 +299,14 @@ def add_mockdata(session): | |
|
||
jobs_dept1 = models.Job.query.filter_by(department_id=1).all() | ||
jobs_dept2 = models.Job.query.filter_by(department_id=2).all() | ||
assignments_dept1 = [build_assignment(officer, unit1, jobs_dept1) for officer in officers_dept1] | ||
assignments_dept2 = [build_assignment(officer, unit1, jobs_dept2) for officer in officers_dept2] | ||
assignments_dept1 = [build_assignment(officer, test_units, jobs_dept1) for officer in officers_dept1] | ||
assignments_dept2 = [build_assignment(officer, test_units, jobs_dept2) for officer in officers_dept2] | ||
|
||
salaries = [build_salary(officer) for officer in all_officers] | ||
faces_dept1 = [assign_faces(officer, assigned_images_dept1) for officer in officers_dept1] | ||
faces_dept2 = [assign_faces(officer, assigned_images_dept2) for officer in officers_dept2] | ||
faces1 = [f for f in faces_dept1 if f] | ||
faces2 = [f for f in faces_dept2 if f] | ||
session.add(unit1) | ||
session.commit() | ||
session.add_all(assignments_dept1) | ||
session.add_all(assignments_dept2) | ||
|
@@ -313,12 +341,6 @@ def add_mockdata(session): | |
session.add(test_unconfirmed_user) | ||
session.commit() | ||
|
||
test_units = [models.Unit(descrip='District 13', department_id=1), | ||
models.Unit(descrip='Bureau of Organized Crime', | ||
department_id=1)] | ||
session.add_all(test_units) | ||
session.commit() | ||
|
||
test_addresses = [ | ||
models.Location( | ||
street_name='Test St', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added - there was a weird issue of failing unit tests on the first PR. I updated the test data to have datetime's for start_date + resign_date on assignments. Without this equality check, we would be == comparing datetime(2020, 01, 01) to '2020-01-01', which would fail and we would trigger spurious row updates and fail the tests.