Skip to content

Commit

Permalink
Fix officer creation with notes and descriptions (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-kelp authored Sep 5, 2023
1 parent 65a02f6 commit 334cf40
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
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

0 comments on commit 334cf40

Please sign in to comment.