Skip to content

Commit c179d2d

Browse files
committed
psycopg2 cleanup
1 parent 31d08fd commit c179d2d

File tree

6 files changed

+77
-78
lines changed

6 files changed

+77
-78
lines changed

config/settings/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
SEED Platform (TM), Copyright (c) Alliance for Sustainable Energy, LLC, and other contributors.
33
See also https://github.com/SEED-platform/seed/blob/main/LICENSE.md
44
"""
5-
from __future__ import annotations
65

76
import os
7+
from typing import Union
88

99
from django.utils.translation import gettext_lazy as _
1010
from kombu.serialization import register
@@ -307,7 +307,7 @@
307307
}
308308

309309

310-
def yn(s: bool | str) -> bool:
310+
def yn(s: Union[bool, str]) -> bool:
311311
if isinstance(s, bool):
312312
return s
313313
if isinstance(s, str):

config/settings/test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@
8484
SF_SECURITY_TOKEN = os.environ.get("SF_SECURITY_TOKEN", "")
8585

8686
# load small EEEJ dataset for testing
87-
EEEJ_LOAD_SMALL_TEST_DATASET = yn(os.environ.get("EEEJ_LOAD_SMALL_TEST_DATASET", "True"))
87+
EEEJ_LOAD_SMALL_TEST_DATASET = yn(os.environ.get("EEEJ_LOAD_SMALL_TEST_DATASET", "True")) # noqa: F405

requirements/base.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
django==4.2.18
33

44
# Persistence stores
5-
psycopg[binary]==3.2.4
5+
psycopg2-binary==2.9.10
66

77
# Background process management
88
celery==5.4.0

seed/migrations/0242_rename_indices.py

+55-56
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,99 @@
44

55

66
class Migration(migrations.Migration):
7-
87
dependencies = [
9-
('seed', '0241_alter_meter_type'),
8+
("seed", "0241_alter_meter_type"),
109
]
1110

1211
operations = [
1312
migrations.RenameIndex(
14-
model_name='note',
15-
new_name='seed_note_organiz_909e1a_idx',
16-
old_fields=('organization', 'note_type'),
13+
model_name="note",
14+
new_name="seed_note_organiz_909e1a_idx",
15+
old_fields=("organization", "note_type"),
1716
),
1817
migrations.RenameIndex(
19-
model_name='propertyauditlog',
20-
new_name='seed_proper_parent__8a8a20_idx',
21-
old_fields=('parent_state1', 'parent_state2'),
18+
model_name="propertyauditlog",
19+
new_name="seed_proper_parent__8a8a20_idx",
20+
old_fields=("parent_state1", "parent_state2"),
2221
),
2322
migrations.RenameIndex(
24-
model_name='propertyauditlog',
25-
new_name='seed_proper_state_i_9c4415_idx',
26-
old_fields=('state', 'name'),
23+
model_name="propertyauditlog",
24+
new_name="seed_proper_state_i_9c4415_idx",
25+
old_fields=("state", "name"),
2726
),
2827
migrations.RenameIndex(
29-
model_name='propertymeasure',
30-
new_name='seed_proper_propert_3e8b15_idx',
31-
old_fields=('property_measure_name', 'property_state'),
28+
model_name="propertymeasure",
29+
new_name="seed_proper_propert_3e8b15_idx",
30+
old_fields=("property_measure_name", "property_state"),
3231
),
3332
migrations.RenameIndex(
34-
model_name='propertystate',
35-
new_name='seed_proper_import__302589_idx',
36-
old_fields=('import_file', 'data_state'),
33+
model_name="propertystate",
34+
new_name="seed_proper_import__302589_idx",
35+
old_fields=("import_file", "data_state"),
3736
),
3837
migrations.RenameIndex(
39-
model_name='propertystate',
40-
new_name='seed_proper_import__a13e5f_idx',
41-
old_fields=('import_file', 'data_state', 'merge_state'),
38+
model_name="propertystate",
39+
new_name="seed_proper_import__a13e5f_idx",
40+
old_fields=("import_file", "data_state", "merge_state"),
4241
),
4342
migrations.RenameIndex(
44-
model_name='propertystate',
45-
new_name='seed_proper_import__ddad37_idx',
46-
old_fields=('import_file', 'data_state', 'source_type'),
43+
model_name="propertystate",
44+
new_name="seed_proper_import__ddad37_idx",
45+
old_fields=("import_file", "data_state", "source_type"),
4746
),
4847
migrations.RenameIndex(
49-
model_name='propertystate',
50-
new_name='seed_proper_hash_ob_6497e4_idx',
51-
old_fields=('hash_object',),
48+
model_name="propertystate",
49+
new_name="seed_proper_hash_ob_6497e4_idx",
50+
old_fields=("hash_object",),
5251
),
5352
migrations.RenameIndex(
54-
model_name='propertyview',
55-
new_name='seed_proper_state_i_dae98d_idx',
56-
old_fields=('state', 'cycle'),
53+
model_name="propertyview",
54+
new_name="seed_proper_state_i_dae98d_idx",
55+
old_fields=("state", "cycle"),
5756
),
5857
migrations.RenameIndex(
59-
model_name='taxlotauditlog',
60-
new_name='seed_taxlot_parent__a3e767_idx',
61-
old_fields=('parent_state1', 'parent_state2'),
58+
model_name="taxlotauditlog",
59+
new_name="seed_taxlot_parent__a3e767_idx",
60+
old_fields=("parent_state1", "parent_state2"),
6261
),
6362
migrations.RenameIndex(
64-
model_name='taxlotauditlog',
65-
new_name='seed_taxlot_state_i_093659_idx',
66-
old_fields=('state', 'name'),
63+
model_name="taxlotauditlog",
64+
new_name="seed_taxlot_state_i_093659_idx",
65+
old_fields=("state", "name"),
6766
),
6867
migrations.RenameIndex(
69-
model_name='taxlotproperty',
70-
new_name='seed_taxlot_propert_be033b_idx',
71-
old_fields=('property_view', 'taxlot_view'),
68+
model_name="taxlotproperty",
69+
new_name="seed_taxlot_propert_be033b_idx",
70+
old_fields=("property_view", "taxlot_view"),
7271
),
7372
migrations.RenameIndex(
74-
model_name='taxlotproperty',
75-
new_name='seed_taxlot_cycle_i_b3c514_idx',
76-
old_fields=('cycle', 'taxlot_view'),
73+
model_name="taxlotproperty",
74+
new_name="seed_taxlot_cycle_i_b3c514_idx",
75+
old_fields=("cycle", "taxlot_view"),
7776
),
7877
migrations.RenameIndex(
79-
model_name='taxlotproperty',
80-
new_name='seed_taxlot_cycle_i_577466_idx',
81-
old_fields=('cycle', 'property_view'),
78+
model_name="taxlotproperty",
79+
new_name="seed_taxlot_cycle_i_577466_idx",
80+
old_fields=("cycle", "property_view"),
8281
),
8382
migrations.RenameIndex(
84-
model_name='taxlotstate',
85-
new_name='seed_taxlot_import__8f600b_idx',
86-
old_fields=('import_file', 'data_state'),
83+
model_name="taxlotstate",
84+
new_name="seed_taxlot_import__8f600b_idx",
85+
old_fields=("import_file", "data_state"),
8786
),
8887
migrations.RenameIndex(
89-
model_name='taxlotstate',
90-
new_name='seed_taxlot_hash_ob_6c9805_idx',
91-
old_fields=('hash_object',),
88+
model_name="taxlotstate",
89+
new_name="seed_taxlot_hash_ob_6c9805_idx",
90+
old_fields=("hash_object",),
9291
),
9392
migrations.RenameIndex(
94-
model_name='taxlotstate',
95-
new_name='seed_taxlot_import__bc9aae_idx',
96-
old_fields=('import_file', 'data_state', 'merge_state'),
93+
model_name="taxlotstate",
94+
new_name="seed_taxlot_import__bc9aae_idx",
95+
old_fields=("import_file", "data_state", "merge_state"),
9796
),
9897
migrations.RenameIndex(
99-
model_name='taxlotview',
100-
new_name='seed_taxlot_state_i_aaa042_idx',
101-
old_fields=('state', 'cycle'),
98+
model_name="taxlotview",
99+
new_name="seed_taxlot_state_i_aaa042_idx",
100+
old_fields=("state", "cycle"),
102101
),
103102
]

seed/models/meters.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django.db import IntegrityError, connection, models
77
from django.db.models.signals import pre_save
88
from django.dispatch import receiver
9-
from psycopg2.extras import execute_values
109

1110
from seed.models import Property, Scenario
1211
from seed.models.inventory_groups import InventoryGroupMapping
@@ -177,19 +176,26 @@ def copy_readings(self, source_meter, overlaps_possible=True):
177176
if overlaps_possible:
178177
sql = (
179178
"INSERT INTO seed_meterreading(meter_id, start_time, end_time, reading, source_unit, conversion_factor) "
180-
"VALUES %s "
179+
"VALUES (%s, %s, %s, %s, %s, %s) "
181180
"ON CONFLICT (meter_id, start_time, end_time) "
182-
"DO UPDATE SET reading=excluded.reading, source_unit=excluded.source_unit, conversion_factor=excluded.conversion_factor "
181+
"DO UPDATE SET reading=excluded.reading, source_unit = excluded.source_unit, conversion_factor = excluded.conversion_factor "
183182
"RETURNING reading"
184183
)
185184

186-
with connection.cursor() as cursor:
187-
execute_values(
188-
cursor,
189-
sql,
190-
source_meter.meter_readings.values(),
191-
template=f"({self.id}, %(start_time)s, %(end_time)s, %(reading)s, %(source_unit)s, %(conversion_factor)s)",
185+
data = [
186+
(
187+
self.id,
188+
reading["start_time"],
189+
reading["end_time"],
190+
reading["reading"],
191+
reading["source_unit"],
192+
reading["conversion_factor"],
192193
)
194+
for reading in source_meter.meter_readings.values()
195+
]
196+
197+
with connection.cursor() as cursor:
198+
cursor.executemany(sql, data)
193199
else:
194200
readings = {
195201
MeterReading(

seed/serializers/meter_readings.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from django.core.exceptions import ValidationError
1010
from django.db import connection
1111
from django.utils.timezone import make_aware
12-
from psycopg2.extras import execute_values
1312
from pytz import timezone
1413
from rest_framework import serializers
1514

@@ -32,20 +31,15 @@ def to_internal_value(self, data):
3231
def create(self, validated_data) -> list[MeterReading]:
3332
upsert_sql = (
3433
f"INSERT INTO seed_meterreading({', '.join(meter_fields)}) " # noqa: S608
35-
"VALUES %s "
34+
"VALUES (%(meter_id)s, %(start_time)s, %(end_time)s, %(reading)s, %(source_unit)s, %(conversion_factor)s) "
3635
"ON CONFLICT (meter_id, start_time, end_time) "
3736
"DO UPDATE SET reading=excluded.reading, source_unit=excluded.source_unit, conversion_factor=excluded.conversion_factor "
3837
f"RETURNING {', '.join(meter_fields)}"
3938
)
4039

4140
with connection.cursor() as cursor:
42-
results: list[tuple] = execute_values(
43-
cursor,
44-
upsert_sql,
45-
validated_data,
46-
template="(%(meter_id)s, %(start_time)s, %(end_time)s, %(reading)s, %(source_unit)s, %(conversion_factor)s)",
47-
fetch=True,
48-
)
41+
cursor.executemany(upsert_sql, validated_data)
42+
results = cursor.fetchall()
4943

5044
# Convert list of tuples to list of MeterReadings for response
5145
updated_readings = [MeterReading(**{field: result[i] for i, field in enumerate(meter_fields)}) for result in results]

0 commit comments

Comments
 (0)