From 3fbfeed14f91e119399096080b5b698559c0bdbb Mon Sep 17 00:00:00 2001 From: Micah Martin Date: Wed, 15 Mar 2017 00:59:39 -0400 Subject: [PATCH] Add command for updating buildings with CBRA program --- .../management/commands/create_indexes.py | 12 +++++++ ebdjango/management/commands/update_cbra.py | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 ebdjango/management/commands/update_cbra.py diff --git a/ebdjango/management/commands/create_indexes.py b/ebdjango/management/commands/create_indexes.py index ee19570..b6b25c2 100644 --- a/ebdjango/management/commands/create_indexes.py +++ b/ebdjango/management/commands/create_indexes.py @@ -18,4 +18,16 @@ class Command(BaseCommand): self.stdout.write("Creating index nyc.address_text") db.nyc.create_index([("address", "text")], name="address_text") + self.stdout.write("Creating index nyc.bbl") + db.nyc.create_index([("bbl", pymongo.ASCENDING)], name="bbl_asc") + + self.stdout.write("Creating index roc.bbl") + db.roc.create_index([("bbl", pymongo.ASCENDING)], name="bbl_asc") + + self.stdout.write("Creating index nyc.properties.program") + db.nyc.create_index([("properties.program", pymongo.ASCENDING)], name="properties_program_asc") + + self.stdout.write("Creating index roc.properties.program") + db.roc.create_index([("properties.program", pymongo.ASCENDING)], name="properties_program_asc") + self.stdout.write(self.style.SUCCESS('Finished creating indexes.')) diff --git a/ebdjango/management/commands/update_cbra.py b/ebdjango/management/commands/update_cbra.py new file mode 100644 index 0000000..5985d11 --- /dev/null +++ b/ebdjango/management/commands/update_cbra.py @@ -0,0 +1,34 @@ +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings +import pymongo +from pymongo import MongoClient +from pymongo.errors import BulkWriteError +import pprint + +class Command(BaseCommand): + help = 'Update CBRA properties' + + def handle(self, *args, **options): + db = MongoClient(settings.MONGO_DB_URI)[settings.MONGO_CLIENT] + + for area in ("roc", "nyc"): + self.stdout.write("Cleaning up existing proprties in %s marked as CBRA." % area) + bulk_op = db[area].initialize_unordered_bulk_op() + bulk_op.find({"properties.program": "CBRA"}).update({"$set": {"properties.program": ""}}) + bulk_op.execute() + + i = 0 + bulk_op = db[area].initialize_unordered_bulk_op() + self.stdout.write("Updating %d records" % db.cbra.count()) + + for doc in db.cbra.find(): + i += 1 + self.stdout.write("Adding bbl: %d %d" % (i, doc['BBL'])) + bulk_op.find({"bbl": doc['BBL']}).update({"$set": {"properties.program": "CBRA"}}) + + self.stdout.write("Executing bulk update.") + try: + bulk_op.execute() + self.stdout.write(self.style.SUCCESS('Finished updating records.')) + except BulkWriteError as bwe: + pprint(bwe.details) -- GitLab