def go_over(tags, parent=None): for tag_name, tag in tags.items(): parent_id = parent['obj']['id'] if parent else None tag['obj'] = TagModel.create(tag_name, budget['id'], parent_id) flat_tags[tag_name] = tag if '_' in tag: go_over(tag['_'], parent)
def init_test_db(test_data): ''' - Fills the system by test data. ''' rebuild_db() # User creation. for name in test_data['users']: test_data['user_objects'][name] = UserModel.register('*****@*****.**' % name, name, name) # Budget creation. for budget_dict in test_data['budgets']: # Budget creation. owner_name = test_data['users'][0] user_id = test_data['user_objects'][owner_name].id budget_object = BudgetModel.create(budget_dict['title'], user_id) budget_dict['budget_object'] = budget_object # Users are attached to the budget. for name in test_data['users'][1:]: user_object = test_data['user_objects'][name] budget_object.attach_user(user_object.id) # Tag creation. for tag_name in budget_dict['tags']: tag = TagModel.create(tag_name, budget_object.id) budget_dict['tag_objects'][tag_name] = tag # Contribution creation. for contribution_dict in budget_dict['contributions']: user_object = test_data['user_objects'][contribution_dict['user']] contribution_object = budget_object.add_contribution(user_object.id, contribution_dict['amount']) contribution_dict['object'] = contribution_object # Expense creation. for expense_dict in budget_dict['expenses']: user_object = test_data['user_objects'][expense_dict['user']] tag_list = [ budget_dict['tag_objects'][tag_name] for tag_name in expense_dict['tags']] expense_dict['object'] = ExpenseModel.create(budget_object.id, user_object.id, expense_dict['amount'], tag_list, expense_dict['description'])