コード例 #1
0
ファイル: views.py プロジェクト: peterbe/django-spending
def categories(request):
    household = get_household(request.user)
    qs = Category.objects.filter(household=household)
    result = {
        'categories': [],
        'revision': get_categories_revision(household),
    }
    for each in qs.order_by('name'):
        result['categories'].append([each.name, each.pk])
    return result
コード例 #2
0
ファイル: views.py プロジェクト: peterbe/django-spending
def submit(request):
    household = get_household(request.user)
    form = forms.MobileExpenseForm(data=request.POST)
    if form.is_valid():
        data = form.cleaned_data
        category_value = data['category']
        if category_value == '_other':
            category_value = data['other_category']
        try:
            if category_value.isdigit():
                category = Category.objects.get(
                    pk=category_value,
                    household=household
                )
            else:
                category = Category.objects.get(
                    name__istartswith=category_value,
                    household=household
                )
        except Category.DoesNotExist:
            category = Category.objects.create(
                name=category_value,
                household=household
            )

        expense = None
        try:
            last_expense, = (
                Expense.objects
                .filter(household=household)
                .filter(user=request.user)
                .order_by('-added')[:1]
            )
            if (
                last_expense.amount == data['amount']
                and
                last_expense.category == category
                and
                last_expense.date == data['date']
                and
                last_expense.notes == data['notes'].strip()
            ):
                print "Not creating another identical one"
                expense = last_expense
        except IndexError:
            pass
        if expense is None:
            expense = Expense.objects.create(
                household=household,
                category=category,
                amount=data['amount'],
                user=request.user,
                date=data['date'],
                notes=data['notes'].strip()
            )
        #today = datetime.datetime.utcnow()
        data = {
            'success_message': '$%.2f for %s added' % (expense.amount, expense.category.name),
            #'todays_date': today.strftime('%Y-%m-%d'),
            'categories_revision': get_categories_revision(household),
        }
    else:
        data = {'errors': form.errors}
    return data