Upload new NFIRS transaction file

In [2]:
import psycopg2
from nfirs_transaction_parser.settings import CONFIG

# Override DB_STRING to production
DB_STRING = 'postgresql://firecares:password@localhost/nfirs'

CONFIG['sqlalchemy.url'] = DB_STRING
conn = psycopg2.connect(DB_STRING)

from nfirs_transaction_parser.core.parser import IncidentFile
from nfirs_transaction_parser.core.loader import DBLoader

# List of files to be processed
# eg. ['newdep/data.inc.txt', 'newdep/data2.inc.txt',']
FILES = []
# FDID of the department to update
FDID = "66003"

# Load files to DB
print "Parsing files"
incidents = 0
for item in FILES:
    if item:
        inc = IncidentFile.load(open(item))
        incidents += len(inc.incidents)
print "Incidents loaded: {}".format(incidents)

if incidents:
    # Update materialized views inside department_nfirs schema
    print "Updating department_nfirs materialized views"
    with conn.cursor() as cursor:
        cursor.execute("REFRESH MATERIALIZED VIEW department_nfirs.buildingfires")

    # Update materialized views in the public schema
    print "Updating public materialized views"
    with conn.cursor() as cursor:
        cursor.execute("REFRESH MATERIALIZED VIEW joint_buildingfires")
        cursor.execute("REFRESH MATERIALIZED VIEW joint_incidentaddress")
        cursor.execute("REFRESH MATERIALIZED VIEW joint_fireincident")
        cursor.execute("REFRESH MATERIALIZED VIEW joint_ffcasualty")
        cursor.execute("REFRESH MATERIALIZED VIEW joint_civiliancasualty")

Parsing files
Incidents loaded: 0

In [3]:
import django.conf as conf
setattr(conf.settings, 'CELERY_ALWAYS_EAGER', True)
import django
from firecares.tasks import update
from firecares import celery
from firecares.firestation.models import FireDepartment
fd = FireDepartment.objects.get(fdid=FDID, state=STATE)

# Export buildingfires geom
print "Export buildingfires geom with the command:"
sql = """\COPY (select alarm, a.inc_type, alarms,ff_death, oth_death, ST_X(geom) as x, st_y(geom) as y, COALESCE(y.risk_category, 'Unknown') as risk_category from buildingfires a LEFT JOIN (SELECT state, fdid, inc_date, inc_no, exp_no, x.geom, x.parcel_id, x.risk_category FROM ( SELECT * FROM incidentaddress a LEFT JOIN parcel_risk_category_local using (parcel_id)) AS x) AS y USING (state, fdid, inc_date, inc_no, exp_no) WHERE a.state = '{state}' and a.fdid = '{fdid}') to PROGRAM 'aws s3 cp - s3://firecares-test/{id}-building-fires.csv --acl=\"public-read\"' DELIMITER ',' CSV HEADER;"""
print sql.format(fdid=FDID, state=STATE, id=fd.id)

ImportError                               Traceback (most recent call last)
<ipython-input-3-70d6e884506a> in <module>()
      1 import django.conf as conf
----> 2 setattr(conf.settings, 'CELERY_ALWAYS_EAGER', True)
      3 import django
      4 django.setup()
      5 from firecares.tasks import update

/webapps/firecares/local/lib/python2.7/site-packages/django/utils/functional.pyc in __setattr__(self, name, value)
    252         else:
    253             if self._wrapped is empty:
--> 254                 self._setup()
    255             setattr(self._wrapped, name, value)

/webapps/firecares/local/lib/python2.7/site-packages/django/conf/__init__.pyc in _setup(self, name)
     42                 % (desc, ENVIRONMENT_VARIABLE))
---> 44         self._wrapped = Settings(settings_module)
     46     def __getattr__(self, name):

/webapps/firecares/local/lib/python2.7/site-packages/django/conf/__init__.pyc in __init__(self, settings_module)
     90         self.SETTINGS_MODULE = settings_module
---> 92         mod = importlib.import_module(self.SETTINGS_MODULE)
     94         tuple_settings = (

/usr/lib/python2.7/importlib/__init__.pyc in import_module(name, package)
     35             level += 1
     36         name = _resolve_name(name[level:], package, level)
---> 37     __import__(name)
     38     return sys.modules[name]

ImportError: No module named firecares.settings.local