def setUp(self): """ Add some transactions to a VirtualAcct. """ user = User(username = '******', email = '*****@*****.**') user.save() profile = UserProfile(user = user) profile.save() budget = Budget(owner = profile, period_budget_amount = '100.00') year = Year() year.save() budget.period_length = year budget.save() category = Category(owner = profile, name = 'test', budget = budget) category.save() self.acct = RealAcct(owner = profile) self.acct.save() self.vacct = VirtualAcct(owner = profile, real_acct = self.acct, parent_budget = budget) self.vacct.save() self.txn_1 = RealTxn(value = '110.00', category = category, real_acct = self.acct) self.txn_1.save() self.vtxn_1 = VirtualTxn(value = '90.00', real_txn = self.txn_1, virtual_acct = self.vacct) self.vtxn_1.save() self.vtxn_2 = VirtualTxn(value = '20.00', real_txn = self.txn_1, virtual_acct = self.vacct) self.vtxn_2.save()
def test_periodlength_inheritance(self): """ See if having PeriodLength as not abstract will call PeriodLength methods or subclass methods """ user = User(username = '******', email = '*****@*****.**') user.save() profile = UserProfile(user = user) profile.save() budget = Budget(owner = profile, period_budget_amount = '100.00') year = Year() year.save() budget.period_length = year budget.save() length = budget.period_length assert(length.__unicode__() == 'Year PeriodLength')
def post(self, request): """ Create a new budget request, if there isn't user with that email, create a new user :param request: Data dictionary of the request with the information of a new budget request :return: Data dictionary with a message, status 200 and identification of the budget request created """ request_data = request.data parameters_required = ['description', 'email', 'phone', 'address'] self.check_parameters_required(parameters_required, request_data) user, data = self.check_user(request_data) data['uuid'] = Budget.get_new_uuid() data['user'] = user.pk data['title'] = request_data.get('title', '') data['description'] = request_data.get('description') if 'category' in data: category_name = data.get('category', '').lower() category = Categories.objects.filter(name=category_name).first() if not category: category = Categories.objects.create(name=category_name) data['category'] = category.pk serializer = BudgetSerializer(data=data) if serializer.is_valid(): serializer.save() context = { 'uuid': data['uuid'], 'message': _("The budget has been created") } return Response(context, status=status.HTTP_201_CREATED) else: return Response(serializer.error_messages, status.HTTP_400_BAD_REQUEST)
def post_budget(request): if Budget.objects.last(): budget_info = Budget.objects.last() else: budgetform = BudgetForm() if request.user.username: if request.method == "POST": author = Budget(author=request.user) budgetform = BudgetForm(request.POST, instance=author) if budgetform.is_valid(): author.save() return redirect("/profile/") else: budgetform = BudgetForm(instance=budget_info) return render(request, 'expenses.html', {'budgetform': budgetform})
def add_transaction_ajax(request): result = {} user = request.user trans = Transaction() budgets = Budget.objects.filter(Q(owner=user) | Q(coowners=user)) categories = Budget.objects.filter(Q(owner=user) | Q(coowners=user)) trans.budget_name = request.POST.get('budget', '') trans.budget_id = request.POST.get('budget_id', 0) trans.category_name = request.POST.get('category', '') trans.category_id = request.POST.get('category_id', 0) trans.money = request.POST.get('money', '') trans.owner = user.username if trans.budget_id and \ not budgets.filter(id=trans.budget_id).count(): trans.budget_id = 0 if trans.category_id and \ not categories.filter(id=trans.category_id).count(): trans.category_id = 0 if not trans.budget_id and trans.budget_name: trans.budget_id = get_similar(budgets, trans.budget_name) if not trans.budget_id: budget = Budget(owner=user, name=trans.budget_name) budget.save() trans.budget_id = budget.id if not trans.category_id and trans.category_name: trans.category_id = get_similar(categories, trans.category_name) if not trans.category_id: category = Category(owner=user, name=trans.category_name) category.save() trans.category_id = category.id trans.save_transaction() result['posted'] = True return HttpResponse(result, mimetype = 'application/javascript')
def addbudget(request): page_data = {"add_budget_form" : add_budget} if request.method == 'POST': add_budget_form = add_budget(request.POST) if(add_budget_form.is_valid()): desc = add_budget_form.cleaned_data["Description"] act = add_budget_form.cleaned_data["Actual"] pred = add_budget_form.cleaned_data["Projected"] chois = add_budget_form.cleaned_data["Category"] sel_category = BudgetCategory.objects.filter(id=chois).get() budget = Budget() budget.Username = request.user budget.Description = desc budget.Projected = pred budget.Actual = act budget.Category = sel_category budget.save() page_data = fillpagedata(request.user) return render(request, 'budget/budget.html', {"dat": page_data, "data":calculate(request.user)}) return render(request, 'budget/addbudget.html', context=page_data)
def test_unitary_create_budget(self): """ Correct created budget """ uuid = Budget.get_new_uuid() budget = Budget.objects.create( uuid=uuid, title="Contrución de casa", description= 'Necesito presupuesto para construir una casa de dos plantas en churriana de la vega (Granada). Tengo la parcela de 220 metros cuadrados en propiedad. Se trataría de una casa de 3 plantas con 5 o más habitaciones. Estoy pendiente de adquirir el proyecto.', category=Categories.objects.get(name="reformas baños"), status=PENDING, user=User.objects.get(email="*****@*****.**")) self.assertEqual(budget.status, PENDING) self.assertEqual(budget.title, "Contrución de casa") self.assertEqual(budget.uuid, uuid)
def test_unitary_create_serializer(self): """Correct create user by serializer""" data = { 'uuid': Budget.get_new_uuid(), 'user': User.objects.get(email="*****@*****.**").pk, 'title': "Nueva Mampara", 'category': Categories.objects.get(name="reformas baños").pk, 'description': "Una mampara de ducha de una puerta abatible de 70 no me importaria que fuera barata de expocicion." } serializer_test = BudgetSerializer(data=data) if serializer_test.is_valid(): serializer_test.save() self.assertEqual( Budget.objects.get(uuid=data['uuid']).title, data['title']) else: raise AttributeError(serializer_test.error_messages)
def upload_data_budget(request): template = dict() if request.method == 'POST': form = UploadDataForm(request.POST, request.FILES) if form.is_valid(): directory = MEDIA_ROOT + "/test_data/" + request.FILES["file"].name reader = csv.reader(open(directory)) for r in reader: budget = Budget() budget.position = Position.objects.get(name=r[0]) budget.term = r[1] budget.year = r[2] budget.stream = r[3] if r[4] == "TRUE": budget.approved = True budget.creator = request.user budget.start_date = start_date(int(budget.year), budget.term) budget.end_date = end_date(int(budget.year), budget.term) budget.save() return HttpResponseRedirect(reverse('budget_view_budgets')) else: form = UploadDataForm() template['form'] = form return render_to_response('budget/upload_budget.htm',template, context_instance=RequestContext(request))
class BudgetTest(TestCase): UUID = Budget.get_new_uuid() UUID2 = Budget.get_new_uuid() def setUp(self): user = User.objects.create(phone="+34666666666", address="España", email="*****@*****.**") user_1 = User.objects.create(phone="+34999999999", address="España, sevilla", email="*****@*****.**") category = Categories.objects.create(name="construcción casas") category_1 = Categories.objects.create(name="reformas baños") Budget.objects.create( uuid=self.UUID, title="Contrución de casa", description= 'Necesito presupuesto para construir una casa de dos plantas en churriana de la vega (Granada). Tengo la parcela de 220 metros cuadrados en propiedad. Se trataría de una casa de 3 plantas con 5 o más habitaciones. Estoy pendiente de adquirir el proyecto.', category=category, status=PENDING, user=user) Budget.objects.create( uuid=self.UUID2, title="Contrución de casa", description= 'Necesito presupuesto para construir una casa de dos plantas en churriana de la vega (Granada). Tengo la parcela de 220 metros cuadrados en propiedad. Se trataría de una casa de 3 plantas con 5 o más habitaciones. Estoy pendiente de adquirir el proyecto.', category=category_1, status=PENDING, user=user_1) def test_unitary_create_budget(self): """ Correct created budget """ uuid = Budget.get_new_uuid() budget = Budget.objects.create( uuid=uuid, title="Contrución de casa", description= 'Necesito presupuesto para construir una casa de dos plantas en churriana de la vega (Granada). Tengo la parcela de 220 metros cuadrados en propiedad. Se trataría de una casa de 3 plantas con 5 o más habitaciones. Estoy pendiente de adquirir el proyecto.', category=Categories.objects.get(name="reformas baños"), status=PENDING, user=User.objects.get(email="*****@*****.**")) self.assertEqual(budget.status, PENDING) self.assertEqual(budget.title, "Contrución de casa") self.assertEqual(budget.uuid, uuid) def test_unitary_serializer_instance_user(self): """Correct instance serializer by a User""" budget = Budget.objects.get(uuid=self.UUID) serializer_test = BudgetSerializer(budget) data_test = serializer_test.data self.assertEqual(data_test['title'], budget.title) def test_unitary_create_serializer(self): """Correct create user by serializer""" data = { 'uuid': Budget.get_new_uuid(), 'user': User.objects.get(email="*****@*****.**").pk, 'title': "Nueva Mampara", 'category': Categories.objects.get(name="reformas baños").pk, 'description': "Una mampara de ducha de una puerta abatible de 70 no me importaria que fuera barata de expocicion." } serializer_test = BudgetSerializer(data=data) if serializer_test.is_valid(): serializer_test.save() self.assertEqual( Budget.objects.get(uuid=data['uuid']).title, data['title']) else: raise AttributeError(serializer_test.error_messages) def test_unitary_update_serializer(self): """Correct update user by serializerr""" data = { 'title': "Construción de casa precio asequible", } budget = Budget.objects.get(uuid=self.UUID) serializer_test = BudgetSerializer(budget, data=data, partial=True) if serializer_test.is_valid(): serializer_test.save() self.assertEqual(serializer_test.data['title'], budget.title) else: raise AttributeError(serializer_test.error_messages) def test_unitary_api_get_budgets(self): """ Correct operation for the request of the entire budget """ response = self.client.get('/api/1.0/budgets/') self.assertEqual(response.status_code, status.HTTP_200_OK) def test_unitary_api_get_budgets_by_email(self): """ Correct operation for the request of all the budget according to the user's email """ response = self.client.get('/api/1.0/budgets/[email protected]/') self.assertEqual(response.status_code, status.HTTP_200_OK) def test_unitary_api_post_budgets_create(self): """ Correct operation of the api when creating a new budget, in addition the user will be updated or modified according to whether the email exists or not """ url = '/api/1.0/budgets/' data = { 'email': '*****@*****.**', 'phone': '+34666666666', 'address': 'España', 'title': "Nueva Mampara", 'category': "reformas baños", 'description': "Una mampara de ducha de una puerta " "abatible de 70 no me importaria que fuera barata de expocicion." } response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) user = User.objects.get(email='*****@*****.**') self.assertEqual(user.email, data['email']) budget = Budget.objects.get(uuid=response.data['uuid']) self.assertEqual(budget.title, data['title']) def test_unitary_api_post_budget(self): """ Correct operation of the api when we want to modify the title, the description or the category of a budget """ url = '/api/1.0/budget/' data = {'uuid': self.UUID2, 'title': 'Contrución de casa griega'} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) budget = Budget.objects.get(uuid=self.UUID2) self.assertEqual(budget.title, data['title']) def test_unitary_api_put_budget(self): """ Correct operation of the api when we want to publish a budget """ response = self.client.put('/api/1.0/budget/%s/' % self.UUID2) self.assertEqual(response.status_code, status.HTTP_200_OK) budget = Budget.objects.get(uuid=self.UUID2) self.assertEqual(budget.status, PUBLISHED) def test_unitary_api_delete_budget(self): """ Correct operation of the api when we want to discard a budget given an identifier """ url = '/api/1.0/budget/%s/' % self.UUID data = {} response = self.client.delete(url, data, format='json') budget = Budget.objects.get(uuid=self.UUID) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(budget.status, DISCARDED) def test_unitary_api_suggest_category_budget(self): """ Correct functioning of the api when we want to request a category suggestion for an identifier""" contentbase = ContentBased() category_suggest = contentbase.predict( "quiero quitar la bañera y poner una mampara con un plato de ducha" ) self.assertIn("reformas baños", category_suggest) response = self.client.get('/api/1.0/suggest_categories/%s/' % self.UUID2) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIn("construcción casas", response.data['categories_suggest'])