Skip to content

Commit 368c673

Browse files
authored
Add static Department functions (#1150)
## Description of Changes Moved `Department`-related methods to the `Department` object. ## Tests and Linting - [x] This branch is up-to-date with the `develop` branch. - [x] Ran `make create_db_diagram` command. - [x] `pytest` passes on my local development environment. - [x] `pre-commit` passes on my local development environment.
1 parent 80795a5 commit 368c673

File tree

4 files changed

+147
-86
lines changed

4 files changed

+147
-86
lines changed

OpenOversight/app/main/downloads.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Any, Callable, Dict, List, TypeVar
66

77
from flask import Response, abort
8-
from sqlalchemy.orm import Query
98

109
from OpenOversight.app.models.database import (
1110
Assignment,
@@ -40,7 +39,7 @@ def check_output(output_str):
4039

4140

4241
def make_downloadable_csv(
43-
query: Query,
42+
query: List,
4443
department_id: int,
4544
csv_suffix: str,
4645
field_names: List[str],

OpenOversight/app/main/views.py

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from flask_login import current_user, login_required, login_user
2222
from flask_wtf import FlaskForm
2323
from sqlalchemy.exc import IntegrityError
24-
from sqlalchemy.orm import contains_eager, joinedload, selectinload
24+
from sqlalchemy.orm import selectinload
2525
from sqlalchemy.orm.exc import NoResultFound
2626

2727
from OpenOversight.app.auth.forms import LoginForm
@@ -71,20 +71,14 @@
7171
User,
7272
db,
7373
)
74-
from OpenOversight.app.models.database_cache import (
75-
get_database_cache_entry,
76-
put_database_cache_entry,
77-
)
7874
from OpenOversight.app.utils.auth import ac_or_admin_required, admin_required
7975
from OpenOversight.app.utils.choices import AGE_CHOICES, GENDER_CHOICES, RACE_CHOICES
8076
from OpenOversight.app.utils.cloud import crop_image, save_image_to_s3_and_db
8177
from OpenOversight.app.utils.constants import (
8278
ENCODING_UTF_8,
8379
FLASH_MSG_PERMANENT_REDIRECT,
8480
KEY_DEPT_ALL_ASSIGNMENTS,
85-
KEY_DEPT_ALL_INCIDENTS,
8681
KEY_DEPT_ALL_LINKS,
87-
KEY_DEPT_ALL_NOTES,
8882
KEY_DEPT_ALL_OFFICERS,
8983
KEY_DEPT_ALL_SALARIES,
9084
KEY_DEPT_TOTAL_ASSIGNMENTS,
@@ -1572,18 +1566,7 @@ def redirect_download_dept_officers_csv(department_id: int):
15721566
)
15731567
@limiter.limit("5/minute")
15741568
def download_dept_officers_csv(department_id: int):
1575-
cache_params = (Department(id=department_id), KEY_DEPT_ALL_OFFICERS)
1576-
officers = get_database_cache_entry(*cache_params)
1577-
if officers is None:
1578-
officers = (
1579-
db.session.query(Officer)
1580-
.options(joinedload(Officer.assignments).joinedload(Assignment.job))
1581-
.options(joinedload(Officer.salaries))
1582-
.filter_by(department_id=department_id)
1583-
.all()
1584-
)
1585-
put_database_cache_entry(*cache_params, officers)
1586-
1569+
officers = Department.get_officers(department_id)
15871570
field_names = [
15881571
"id",
15891572
"unique identifier",
@@ -1599,6 +1582,7 @@ def download_dept_officers_csv(department_id: int):
15991582
"job title",
16001583
"most recent salary",
16011584
]
1585+
16021586
return make_downloadable_csv(
16031587
officers, department_id, "Officers", field_names, officer_record_maker
16041588
)
@@ -1620,20 +1604,7 @@ def redirect_download_dept_assignments_csv(department_id: int):
16201604
)
16211605
@limiter.limit("5/minute")
16221606
def download_dept_assignments_csv(department_id: int):
1623-
cache_params = Department(id=department_id), KEY_DEPT_ALL_ASSIGNMENTS
1624-
assignments = get_database_cache_entry(*cache_params)
1625-
if assignments is None:
1626-
assignments = (
1627-
db.session.query(Assignment)
1628-
.join(Assignment.base_officer)
1629-
.filter(Officer.department_id == department_id)
1630-
.options(contains_eager(Assignment.base_officer))
1631-
.options(joinedload(Assignment.unit))
1632-
.options(joinedload(Assignment.job))
1633-
.all()
1634-
)
1635-
put_database_cache_entry(*cache_params, assignments)
1636-
1607+
assignments = Department.get_assignments(department_id)
16371608
field_names = [
16381609
"id",
16391610
"officer id",
@@ -1645,6 +1616,7 @@ def download_dept_assignments_csv(department_id: int):
16451616
"unit id",
16461617
"unit description",
16471618
]
1619+
16481620
return make_downloadable_csv(
16491621
assignments,
16501622
department_id,
@@ -1670,12 +1642,7 @@ def redirect_download_incidents_csv(department_id: int):
16701642
)
16711643
@limiter.limit("5/minute")
16721644
def download_incidents_csv(department_id: int):
1673-
cache_params = (Department(id=department_id), KEY_DEPT_ALL_INCIDENTS)
1674-
incidents = get_database_cache_entry(*cache_params)
1675-
if incidents is None:
1676-
incidents = Incident.query.filter_by(department_id=department_id).all()
1677-
put_database_cache_entry(*cache_params, incidents)
1678-
1645+
incidents = Department.get_incidents(department_id)
16791646
field_names = [
16801647
"id",
16811648
"report_num",
@@ -1687,6 +1654,7 @@ def download_incidents_csv(department_id: int):
16871654
"links",
16881655
"officers",
16891656
]
1657+
16901658
return make_downloadable_csv(
16911659
incidents,
16921660
department_id,
@@ -1712,18 +1680,7 @@ def redirect_download_dept_salaries_csv(department_id: int):
17121680
)
17131681
@limiter.limit("5/minute")
17141682
def download_dept_salaries_csv(department_id: int):
1715-
cache_params = (Department(id=department_id), KEY_DEPT_ALL_SALARIES)
1716-
salaries = get_database_cache_entry(*cache_params)
1717-
if salaries is None:
1718-
salaries = (
1719-
db.session.query(Salary)
1720-
.join(Salary.officer)
1721-
.filter(Officer.department_id == department_id)
1722-
.options(contains_eager(Salary.officer))
1723-
.all()
1724-
)
1725-
put_database_cache_entry(*cache_params, salaries)
1726-
1683+
salaries = Department.get_salaries(department_id)
17271684
field_names = [
17281685
"id",
17291686
"officer id",
@@ -1734,6 +1691,7 @@ def download_dept_salaries_csv(department_id: int):
17341691
"year",
17351692
"is_fiscal_year",
17361693
]
1694+
17371695
return make_downloadable_csv(
17381696
salaries, department_id, "Salaries", field_names, salary_record_maker
17391697
)
@@ -1751,18 +1709,7 @@ def redirect_download_dept_links_csv(department_id: int):
17511709
@main.route("/download/departments/<int:department_id>/links", methods=[HTTPMethod.GET])
17521710
@limiter.limit("5/minute")
17531711
def download_dept_links_csv(department_id: int):
1754-
cache_params = (Department(id=department_id), KEY_DEPT_ALL_LINKS)
1755-
links = get_database_cache_entry(*cache_params)
1756-
if links is None:
1757-
links = (
1758-
db.session.query(Link)
1759-
.join(Link.officers)
1760-
.filter(Officer.department_id == department_id)
1761-
.options(contains_eager(Link.officers))
1762-
.all()
1763-
)
1764-
put_database_cache_entry(*cache_params, links)
1765-
1712+
links = Department.get_links(department_id)
17661713
field_names = [
17671714
"id",
17681715
"title",
@@ -1773,6 +1720,7 @@ def download_dept_links_csv(department_id: int):
17731720
"officers",
17741721
"incidents",
17751722
]
1723+
17761724
return make_downloadable_csv(
17771725
links, department_id, "Links", field_names, links_record_maker
17781726
)
@@ -1794,18 +1742,7 @@ def redirect_download_dept_descriptions_csv(department_id: int):
17941742
)
17951743
@limiter.limit("5/minute")
17961744
def download_dept_descriptions_csv(department_id: int):
1797-
cache_params = (Department(id=department_id), KEY_DEPT_ALL_NOTES)
1798-
notes = get_database_cache_entry(*cache_params)
1799-
if notes is None:
1800-
notes = (
1801-
db.session.query(Description)
1802-
.join(Description.officer)
1803-
.filter(Officer.department_id == department_id)
1804-
.options(contains_eager(Description.officer))
1805-
.all()
1806-
)
1807-
put_database_cache_entry(*cache_params, notes)
1808-
1745+
descriptions = Department.get_descriptions(department_id)
18091746
field_names = [
18101747
"id",
18111748
"text_contents",
@@ -1814,8 +1751,9 @@ def download_dept_descriptions_csv(department_id: int):
18141751
"created_at",
18151752
"last_updated_at",
18161753
]
1754+
18171755
return make_downloadable_csv(
1818-
notes, department_id, "Notes", field_names, descriptions_record_maker
1756+
descriptions, department_id, "Notes", field_names, descriptions_record_maker
18191757
)
18201758

18211759

0 commit comments

Comments
 (0)