def insert_with_id(obj):
if '<STR_LIT>' in obj:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>obj['<STR_LIT>'] = datetime.datetime.utcnow()<EOL>obj['<STR_LIT>'] = obj['<STR_LIT>']<EOL>if obj['<STR_LIT>'] == '<STR_LIT>' or obj['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>collection = db.legislators<EOL>id_type = '<STR_LIT:L>'<EOL><DEDENT>elif obj['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>collection = db.committees<EOL>id_type = '<STR_LIT:C>'<EOL><DEDENT>elif obj['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>collection = db.bills<EOL>id_type = '<STR_LIT:B>'<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>abbr = obj[settings.LEVEL_FIELD].upper()<EOL>id_reg = re.compile('<STR_LIT>' % (abbr, id_type))<EOL>id_prefix = '<STR_LIT>' % (abbr, id_type)<EOL>cursor = collection.find({'<STR_LIT>': id_reg}).sort('<STR_LIT>', -<NUM_LIT:1>).limit(<NUM_LIT:1>)<EOL>try:<EOL><INDENT>new_id = int(next(cursor)['<STR_LIT>'][len(abbr) + <NUM_LIT:1>:]) + <NUM_LIT:1><EOL><DEDENT>except StopIteration:<EOL><INDENT>new_id = <NUM_LIT:1><EOL><DEDENT>while True:<EOL><INDENT>if obj['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>obj['<STR_LIT>'] = '<STR_LIT>' % (id_prefix, new_id)<EOL><DEDENT>else:<EOL><INDENT>obj['<STR_LIT>'] = '<STR_LIT>' % (id_prefix, new_id)<EOL><DEDENT>obj['<STR_LIT>'] = [obj['<STR_LIT>']]<EOL>if obj['<STR_LIT>'] in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>obj['<STR_LIT>'] = obj['<STR_LIT>']<EOL><DEDENT>try:<EOL><INDENT>return collection.insert(obj, safe=True)<EOL><DEDENT>except pymongo.errors.DuplicateKeyError:<EOL><INDENT>new_id += <NUM_LIT:1><EOL><DEDENT><DEDENT>
Generates a unique ID for the supplied legislator/committee/bill
and inserts it into the appropriate collection.