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

Fix officer creation with notes and descriptions #1053

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 deletions OpenOversight/app/utils/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def add_officer_profile(form: AddOfficerForm, current_user: User) -> Officer:
birth_year=form.birth_year.data,
employment_date=form.employment_date.data,
department_id=form.department.data.id,
created_by=current_user.id,
last_updated_by=current_user.id,
)
db.session.add(officer)
db.session.commit()
Expand All @@ -79,6 +81,8 @@ def add_officer_profile(form: AddOfficerForm, current_user: User) -> Officer:
job_id=form.job_id.data,
unit=officer_unit,
start_date=form.employment_date.data,
created_by=current_user.id,
last_updated_by=current_user.id,
)
db.session.add(assignment)
if form.links.data:
Expand All @@ -91,19 +95,21 @@ def add_officer_profile(form: AddOfficerForm, current_user: User) -> Officer:
# don't try to create with a blank string
if note["text_contents"]:
new_note = Note(
note=note["text_contents"],
user_id=current_user.get_id(),
text_contents=note["text_contents"],
officer=officer,
created_by=current_user.id,
last_updated_by=current_user.id,
)
db.session.add(new_note)
if form.descriptions.data:
for description in form.data["descriptions"]:
# don't try to create with a blank string
if description["text_contents"]:
new_description = Description(
description=description["text_contents"],
user_id=current_user.get_id(),
text_contents=description["text_contents"],
officer=officer,
created_by=current_user.id,
last_updated_by=current_user.id,
)
db.session.add(new_description)
if form.salaries.data:
Expand All @@ -116,6 +122,8 @@ def add_officer_profile(form: AddOfficerForm, current_user: User) -> Officer:
overtime_pay=salary["overtime_pay"],
year=salary["year"],
is_fiscal_year=salary["is_fiscal_year"],
created_by=current_user.id,
last_updated_by=current_user.id,
)
db.session.add(new_salary)

Expand Down
29 changes: 28 additions & 1 deletion OpenOversight/tests/routes/test_officer_and_department.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import re
from datetime import date, datetime
from decimal import Decimal
from http import HTTPStatus
from io import BytesIO

Expand Down Expand Up @@ -1166,7 +1167,7 @@ def test_expected_dept_appears_in_submission_dept_selection(mockdata, client, se

def test_admin_can_add_new_officer(mockdata, client, session, department, faker):
with current_app.test_request_context():
login_admin(client)
_, admin = login_admin(client)

links = [
LinkForm(url=faker.url(), link_type="link").data,
Expand All @@ -1184,6 +1185,9 @@ def test_admin_can_add_new_officer(mockdata, client, session, department, faker)
department=department.id,
birth_year=1990,
links=links,
notes=[{"text_contents": "note"}],
descriptions=[{"text_contents": "description"}],
salaries=[{"salary": "123.45", "overtime_pay": "543.21"}],
)

data = process_form_data(form.data)
Expand All @@ -1197,6 +1201,29 @@ def test_admin_can_add_new_officer(mockdata, client, session, department, faker)
assert officer.first_name == "Test"
assert officer.race == "WHITE"
assert officer.gender == "M"
assert officer.created_by == admin.id
assert officer.last_updated_by == admin.id

assert len(officer.assignments) == 1
assert officer.assignments[0].star_no == "666"
assert officer.assignments[0].created_by == admin.id
assert officer.assignments[0].last_updated_by == admin.id

assert len(officer.notes) == 1
assert officer.notes[0].text_contents == "note"
assert officer.notes[0].created_by == admin.id
assert officer.notes[0].last_updated_by == admin.id

assert len(officer.descriptions) == 1
assert officer.descriptions[0].text_contents == "description"
assert officer.descriptions[0].created_by == admin.id
assert officer.descriptions[0].last_updated_by == admin.id

assert len(officer.salaries) == 1
assert officer.salaries[0].salary == Decimal("123.45")
assert officer.salaries[0].overtime_pay == Decimal("543.21")
assert officer.salaries[0].created_by == admin.id
assert officer.descriptions[0].last_updated_by == admin.id


def test_admin_can_add_new_officer_with_unit(
Expand Down