def budget_exists(muni, year): munis = get_munis() entry = munis.find_one({'name': muni}) if not 'roots' in entry.keys(): return False else: return (str(year) in entry['roots'].keys())
def get_budget(muni=None, year=None, layer=4): if not (0 <= layer <= 4): layer = 4 munis = get_munis() if muni is None: munis = list(munis.find({})) else: munis = list(munis.find({'name':muni})) quries = [] if year is None: for muni in munis: quries.extend([(muni['name'], year) for year in muni['years']]) else: years = [year] quries.extend([(muni['name'], year) for muni in munis if int(year) in muni['years']]) budgets = [] for query in quries: budgets.extend(_get_layer(get_root(*query,layer=layer), layer)) budgets = [budget.to_dict(0) for budget in budgets] return budgets
def get_root_tree(muni, year, layer=1000, expense=None): munis = get_munis() flatten = get_flatten() entry = munis.find_one({'name':muni}) root_id = entry['roots'][str(year)] root_tree = get_subtree(root_id, layer, expense=expense) munis.close() flatten.close() return root_tree
def remove_muni_year_tree(muni, year): print 'Cleaning budget tree of %s of year %s' % (muni, year) flatten = get_flatten() results = flatten.delete_many({'muni': muni, 'year': year}) flatten.close() munis = get_munis() entry = munis.find_one({'name': muni}) entry['roots'].pop(str(year)) munis.save(entry) munis.close()
def add_muni(self,muni_object, years=None): db = get_munis() muni_entry = db.dataset.find_one({'name':muni_object.MUNI}) if years is None: years = [] if muni_entry is None: db.dataset.insert({'name':muni_object.MUNI,'info':muni_object.info,'years':years,'roots':{}}) else: # import pdb; pdb.set_trace() muni_entry['years'] = list(set(muni_entry['years']+years)) db.dataset.save(muni_entry)
def update_root(muni, year, root): munis = get_munis() entry = munis.find_one({'name': muni}) entry['roots'][str(year)] = root munis.save(entry) munis.close()