Skip to content

Commit

Permalink
drop sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
fgregg committed Apr 22, 2024
1 parent 039d64f commit 30cf919
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
43 changes: 12 additions & 31 deletions camp_fin/management/commands/aggregate_data.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import sqlalchemy as sa
from django.conf import settings
from django.core.management.base import BaseCommand

DB_CONN = "postgresql://{USER}:{PASSWORD}@{HOST}:{PORT}/{NAME}"

engine = sa.create_engine(
DB_CONN.format(**settings.DATABASES["default"]),
convert_unicode=True,
server_side_cursors=True,
)
from django.db import connections, transaction
from django.db.utils import ProgrammingError


class Command(BaseCommand):
help = "Import New Mexico Campaign Finance data"

def handle(self, *args, **options):
self.connection = engine.connect()

self.makeLoanBalanceView()
self.makeTransactionAggregates()
Expand All @@ -31,7 +22,7 @@ def makeTransactionAggregates(self):
interval
)
)
except sa.exc.ProgrammingError:
except ProgrammingError:
view = """
CREATE MATERIALIZED VIEW contributions_by_{0} AS (
SELECT
Expand Down Expand Up @@ -78,7 +69,7 @@ def makeTransactionAggregates(self):
interval
)
)
except sa.exc.ProgrammingError:
except ProgrammingError:
view = """
CREATE MATERIALIZED VIEW expenditures_by_{0} AS (
SELECT
Expand Down Expand Up @@ -135,7 +126,7 @@ def makeLoanBalanceView(self):
""",
raise_exc=True,
)
except sa.exc.ProgrammingError:
except ProgrammingError:
self.executeTransaction(
"""
CREATE MATERIALIZED VIEW current_loan_status AS (
Expand All @@ -154,20 +145,10 @@ def makeLoanBalanceView(self):
)

def executeTransaction(self, query, *args, **kwargs):
trans = self.connection.begin()

raise_exc = kwargs.get("raise_exc", True)

try:
self.connection.execute("SET local timezone to 'America/Denver'")
if kwargs:
self.connection.execute(query, **kwargs)
else:
self.connection.execute(query, *args)
trans.commit()
except sa.exc.ProgrammingError as e:
# TODO: Make some kind of logger
# logger.error(e, exc_info=True)
trans.rollback()
if raise_exc:
raise e
with connections["default"].cursor() as cursor:
with transaction.atomic():
cursor.execute("SET local timezone to 'America/Denver'")
if kwargs:
cursor.execute(query, kwargs)
else:
cursor.execute(query, args)
18 changes: 10 additions & 8 deletions camp_fin/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,9 @@ def test_race_change_list_loads(self):
colname = 'class="column-{field}"'.format(field=field)
self.assertIn(colname, html)

table_list = html.split("<tr")

# We created one race. With the header row, that should make for a list
# length of 3
self.assertEqual(len(table_list), 3)
self.assertIn("1 race", html)

def test_race_edit_loads(self):
url = reverse("admin:camp_fin_race_change", args=(self.race.id,))
Expand All @@ -202,24 +200,28 @@ def test_race_winner_restricted_to_foreign_keys(self):
self.assertNotIn(str(self.non_race_campaign), html)

def test_admin_search(self):

base_url = reverse("admin:camp_fin_race_changelist")

# We can search offices
query = "?q=office"
base_url = reverse("admin:camp_fin_race_changelist")
url = base_url + query
changelist = self.client.get(url, follow=True)
html = changelist.content.decode("utf-8")

table_list = html.split("<tr")
self.assertEqual(len(table_list), 3)
self.assertIn("1 race", html)

def test_admin_search_fail(self):

base_url = reverse("admin:camp_fin_race_changelist")

# We can't search candidates (change this if we decide we can!)
bad_query = "?q=candidate"
bad_url = base_url + bad_query
empty_changes = self.client.get(bad_url, follow=True)
empty_html = empty_changes.content.decode("utf-8")

empty_table = empty_html.split("<tr")
self.assertEqual(len(empty_table), 1)
self.assertIn("0 results", empty_html)


class TestUtils(TestCase):
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Django==3.2.25
psycopg2-binary==2.9.9
SQLAlchemy==1.3.0
django-bootstrap-pagination==1.7.1
djangorestframework==3.15.1
django-pdb==0.6.2
Expand Down

0 comments on commit 30cf919

Please sign in to comment.