Ejemplo n.º 1
0
Archivo: views.py Proyecto: tmm/Wilbur
    def post(self, request, *args, **kwargs):
        user = get_user_in_session(request.session)
        categories = get_unused_categories_for_user(user)
        data = {'categories': categories}
        form = BudgetForm(request.POST, initial=data)
        if form.is_valid():
            category = form.cleaned_data['category']
            amount = form.cleaned_data['amount']
            description = form.cleaned_data['description']
            budget = Budget(user=user, category=category, amount=amount, description=description)
            budget.save()

            message_url = reverse(viewname='wilbur:budgets-edit', kwargs={'budget_id': budget.id})
            request.session['message_url'] = message_url
            messages.success(request, 'Budget added')
            return {
                'success': True,
            }
        form_html = render(request, 'budgets/add_form.html', {
            'form': form,
        })
        form_html = form_html.content.decode('utf-8')
        return {
            'success': False,
            'form_html': form_html,
        }
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        user = get_user_in_session(request.session)
        categories = get_unused_categories_for_user(user)
        data = {'categories': categories}
        form = BudgetForm(request.POST, initial=data)
        if form.is_valid():
            category = form.cleaned_data['category']
            amount = form.cleaned_data['amount']
            description = form.cleaned_data['description']
            budget = Budget(user=user,
                            category=category,
                            amount=amount,
                            description=description)
            budget.save()

            message_url = reverse(viewname='wilbur:budgets-edit',
                                  kwargs={'budget_id': budget.id})
            request.session['message_url'] = message_url
            messages.success(request, 'Budget added')
            return {
                'success': True,
            }
        form_html = render(request, 'budgets/add_form.html', {
            'form': form,
        })
        form_html = form_html.content.decode('utf-8')
        return {
            'success': False,
            'form_html': form_html,
        }
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        budget_id = kwargs['budget_id']
        budget = Budget.objects.get(pk=budget_id)
        user = get_user_in_session(request.session)
        categories = get_unused_categories_for_user(user, budget.category)
        data = {
            'category': budget.category.id,
            'amount': budget.amount,
            'description': budget.description,
            'categories': categories,
        }
        form = BudgetForm(request.POST, initial=data)
        if form.is_valid():
            if form.has_changed():
                for field in form.changed_data:
                    cleaned_data = form.cleaned_data[field]
                    if field == 'category':
                        budget.category = cleaned_data
                    elif field == 'amount':
                        budget.amount = cleaned_data
                    elif field == 'description':
                        budget.description = cleaned_data
                budget.save()

            message_url = reverse(viewname='wilbur:budgets-edit',
                                  kwargs={'budget_id': budget.id})
            request.session['message_url'] = message_url
            messages.success(request, 'Budget edited')
            return {'success': True}
        form_html = render(
            request, 'budgets/edit_form.html', {
                'form':
                form,
                'budget':
                budget,
                'dialog_title':
                'Delete Budget',
                'dialog_text':
                'Are you sure you want to delete {}s?'.format(
                    budget.category.name.title()),
                'dialog_action':
                reverse(viewname='wilbur:budgets-delete',
                        kwargs={'budget_id': budget.id}),
            })
        form_html = form_html.content.decode('utf-8')
        return {
            'success': False,
            'form_html': form_html,
        }
Ejemplo n.º 4
0
Archivo: views.py Proyecto: tmm/Wilbur
    def post(self, request, *args, **kwargs):
        budget_id = kwargs['budget_id']
        budget = Budget.objects.get(pk=budget_id)
        user = get_user_in_session(request.session)
        categories = get_unused_categories_for_user(user, budget.category)
        data = {
            'category': budget.category.id,
            'amount': budget.amount,
            'description': budget.description,
            'categories': categories,
        }
        form = BudgetForm(request.POST, initial=data)
        if form.is_valid():
            if form.has_changed():
                for field in form.changed_data:
                    cleaned_data = form.cleaned_data[field]
                    if field == 'category':
                        budget.category = cleaned_data
                    elif field == 'amount':
                        budget.amount = cleaned_data
                    elif field == 'description':
                        budget.description = cleaned_data
                budget.save()

            message_url = reverse(viewname='wilbur:budgets-edit', kwargs={'budget_id': budget.id})
            request.session['message_url'] = message_url
            messages.success(request, 'Budget edited')
            return {'success': True}
        form_html = render(request, 'budgets/edit_form.html', {
            'form': form,
            'budget': budget,
            'dialog_title': 'Delete Budget',
            'dialog_text': 'Are you sure you want to delete {}s?'.format(budget.category.name.title()),
            'dialog_action': reverse(viewname='wilbur:budgets-delete', kwargs={'budget_id': budget.id}),
        })
        form_html = form_html.content.decode('utf-8')
        return {
            'success': False,
            'form_html': form_html,
        }