39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import os
|
|
import django
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
|
django.setup()
|
|
|
|
from core.models import Voter
|
|
from django.db.models import Exists, OuterRef
|
|
|
|
def update_target_door_visit():
|
|
# We want to set target_door_visit = False for voters where:
|
|
# 1. door_visit == False
|
|
# 2. There is NOT at least 1 voter in the household where is_targeted == True
|
|
|
|
# Household is identified by (address_street, city, state, zip_code, tenant)
|
|
|
|
subquery = Voter.objects.filter(
|
|
address_street=OuterRef('address_street'),
|
|
city=OuterRef('city'),
|
|
state=OuterRef('state'),
|
|
zip_code=OuterRef('zip_code'),
|
|
tenant=OuterRef('tenant'),
|
|
is_targeted=True
|
|
)
|
|
|
|
voters_to_update = Voter.objects.filter(
|
|
door_visit=False,
|
|
target_door_visit=True
|
|
).annotate(
|
|
household_has_targeted=Exists(subquery)
|
|
).filter(
|
|
household_has_targeted=False
|
|
)
|
|
|
|
count = voters_to_update.update(target_door_visit=False)
|
|
print(f"Finished. Total updated: {count}")
|
|
|
|
if __name__ == "__main__":
|
|
update_target_door_visit() |