import os, glob
import simplejson as json
import web
from settings import db
import cgitb
cgitb.enable(format='text')

DATA_DIR = 'load/manual'

def unidecode(d):
    newd = {}
    for k, v in d.iteritems():
        newd[k.encode('utf8')] = v
    return newd

def items(fname):
    print 'loading', fname
    return json.load(file(DATA_DIR + '/%s.json' % fname)).iteritems()

def load_all():
    for code, state in items('states'):
        if 'aka' in state: state.pop('aka')
        db.insert('state', seqname=False, code=code, **unidecode(state))

    for name, district in items('districts'):
        db.insert('district', seqname=False, name=name, **unidecode(district))

    for polid, pol in items('politicians'):
        db.insert('politician', seqname=False, id=polid, **unidecode(pol))

if __name__ == "__main__": load_all()

@@ -136,16 +136,38 @@
 /manual'
+%0Acurrent_session = 111
 %0A%0Adef un
@@ -687,24 +687,100 @@
 district))%0A%0A
+    db.update('congress', where=%22current_member='t'%22, current_member=False)%0A
     for poli
@@ -885,16 +885,206 @@
 de(pol))
+%0A        db.insert('congress', seqname=False, politician_id=polid, %0A                    congress_num=current_session, district_id=pol%5B'district_id'%5D,%0A                    current_member=True)
 %0A%0Aif __n

add pols from politician.json to congress as current members