def handle(self, *args, **options): print "bla for the win" flatten_dataset = get_flatten() for (muni, info) in muni_iter(**options): if options['year'] is not None: years = [options['year']] else: years = get_muni_years(muni) for year in years: print 'Converting to tree the budget %s of year %s' % (muni, year) if budget_exists(muni, year): if options['clean']: remove_muni_year_tree(muni, year) else: print "Budget %s, of year %s is already uploaded. Use clean to overwrite." % ( muni, year) continue tree = create_tree(muni, year) tree.name = "%s (%s)" % (info['heb_name'], year) root = tree.to_db(flatten_dataset) update_root(muni, year, root) flatten_dataset.close()
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 search_code(muni,year,code): # TODO : rewrite this code dataset = get_flatten() results = [] code_rex = re.compile("^%s*" %(code,)) for item in dataset.find({'muni':muni,'year':year,'code': code_rex}): results.append({key: value for key, value in item.items() if (key != "_id") and (key != 'children')}) for result in results: result['amount'] = int(result['amount']) dataset.close() return results
def get_subtree(_id, layer, expense=None): if not isinstance(_id, ObjectId): _id = ObjectId(_id) flatten = get_flatten() root = flatten.find_one(_id) if expense is None: root_tree = Tree.from_db(flatten, root, layer, expense=expense) else: second_level_roots = [flatten.find_one(_id) for _id in root['children']] if second_level_roots[0]['expense'] is True: expenditure_root = second_level_roots[0] revenue_root = second_level_roots[1] else: expenditure_root = second_level_roots[1] revenue_root = second_level_roots[0] if expense: root_tree = Tree.from_db(flatten, expenditure_root, layer, expense=expense) else: root_tree = Tree.from_db(flatten, revenue_root, layer, expense=expense) flatten.close() return root_tree
def handle(self, *args, **options): print "bla for the win" with open(join(STATICFILES_DIRS[0], 'obudget'), 'w') as f: for item in get_flatten().find(): f.write(dumps(item) + '\n')