21
21
from flask_login import current_user , login_required , login_user
22
22
from flask_wtf import FlaskForm
23
23
from sqlalchemy .exc import IntegrityError
24
- from sqlalchemy .orm import contains_eager , joinedload , selectinload
24
+ from sqlalchemy .orm import selectinload
25
25
from sqlalchemy .orm .exc import NoResultFound
26
26
27
27
from OpenOversight .app .auth .forms import LoginForm
71
71
User ,
72
72
db ,
73
73
)
74
- from OpenOversight .app .models .database_cache import (
75
- get_database_cache_entry ,
76
- put_database_cache_entry ,
77
- )
78
74
from OpenOversight .app .utils .auth import ac_or_admin_required , admin_required
79
75
from OpenOversight .app .utils .choices import AGE_CHOICES , GENDER_CHOICES , RACE_CHOICES
80
76
from OpenOversight .app .utils .cloud import crop_image , save_image_to_s3_and_db
81
77
from OpenOversight .app .utils .constants import (
82
78
ENCODING_UTF_8 ,
83
79
FLASH_MSG_PERMANENT_REDIRECT ,
84
80
KEY_DEPT_ALL_ASSIGNMENTS ,
85
- KEY_DEPT_ALL_INCIDENTS ,
86
81
KEY_DEPT_ALL_LINKS ,
87
- KEY_DEPT_ALL_NOTES ,
88
82
KEY_DEPT_ALL_OFFICERS ,
89
83
KEY_DEPT_ALL_SALARIES ,
90
84
KEY_DEPT_TOTAL_ASSIGNMENTS ,
@@ -1572,18 +1566,7 @@ def redirect_download_dept_officers_csv(department_id: int):
1572
1566
)
1573
1567
@limiter .limit ("5/minute" )
1574
1568
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 )
1587
1570
field_names = [
1588
1571
"id" ,
1589
1572
"unique identifier" ,
@@ -1599,6 +1582,7 @@ def download_dept_officers_csv(department_id: int):
1599
1582
"job title" ,
1600
1583
"most recent salary" ,
1601
1584
]
1585
+
1602
1586
return make_downloadable_csv (
1603
1587
officers , department_id , "Officers" , field_names , officer_record_maker
1604
1588
)
@@ -1620,20 +1604,7 @@ def redirect_download_dept_assignments_csv(department_id: int):
1620
1604
)
1621
1605
@limiter .limit ("5/minute" )
1622
1606
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 )
1637
1608
field_names = [
1638
1609
"id" ,
1639
1610
"officer id" ,
@@ -1645,6 +1616,7 @@ def download_dept_assignments_csv(department_id: int):
1645
1616
"unit id" ,
1646
1617
"unit description" ,
1647
1618
]
1619
+
1648
1620
return make_downloadable_csv (
1649
1621
assignments ,
1650
1622
department_id ,
@@ -1670,12 +1642,7 @@ def redirect_download_incidents_csv(department_id: int):
1670
1642
)
1671
1643
@limiter .limit ("5/minute" )
1672
1644
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 )
1679
1646
field_names = [
1680
1647
"id" ,
1681
1648
"report_num" ,
@@ -1687,6 +1654,7 @@ def download_incidents_csv(department_id: int):
1687
1654
"links" ,
1688
1655
"officers" ,
1689
1656
]
1657
+
1690
1658
return make_downloadable_csv (
1691
1659
incidents ,
1692
1660
department_id ,
@@ -1712,18 +1680,7 @@ def redirect_download_dept_salaries_csv(department_id: int):
1712
1680
)
1713
1681
@limiter .limit ("5/minute" )
1714
1682
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 )
1727
1684
field_names = [
1728
1685
"id" ,
1729
1686
"officer id" ,
@@ -1734,6 +1691,7 @@ def download_dept_salaries_csv(department_id: int):
1734
1691
"year" ,
1735
1692
"is_fiscal_year" ,
1736
1693
]
1694
+
1737
1695
return make_downloadable_csv (
1738
1696
salaries , department_id , "Salaries" , field_names , salary_record_maker
1739
1697
)
@@ -1751,18 +1709,7 @@ def redirect_download_dept_links_csv(department_id: int):
1751
1709
@main .route ("/download/departments/<int:department_id>/links" , methods = [HTTPMethod .GET ])
1752
1710
@limiter .limit ("5/minute" )
1753
1711
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 )
1766
1713
field_names = [
1767
1714
"id" ,
1768
1715
"title" ,
@@ -1773,6 +1720,7 @@ def download_dept_links_csv(department_id: int):
1773
1720
"officers" ,
1774
1721
"incidents" ,
1775
1722
]
1723
+
1776
1724
return make_downloadable_csv (
1777
1725
links , department_id , "Links" , field_names , links_record_maker
1778
1726
)
@@ -1794,18 +1742,7 @@ def redirect_download_dept_descriptions_csv(department_id: int):
1794
1742
)
1795
1743
@limiter .limit ("5/minute" )
1796
1744
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 )
1809
1746
field_names = [
1810
1747
"id" ,
1811
1748
"text_contents" ,
@@ -1814,8 +1751,9 @@ def download_dept_descriptions_csv(department_id: int):
1814
1751
"created_at" ,
1815
1752
"last_updated_at" ,
1816
1753
]
1754
+
1817
1755
return make_downloadable_csv (
1818
- notes , department_id , "Notes" , field_names , descriptions_record_maker
1756
+ descriptions , department_id , "Notes" , field_names , descriptions_record_maker
1819
1757
)
1820
1758
1821
1759
0 commit comments