예제 #1
0
파일: views.py 프로젝트: tlam/spendalot
def details_json(request, slug):
    df = Expense.data_frame()
    categories = df.groupby('Category')
    category = Category.objects.get(slug=slug).name
    yearly = categories.get_group(category).resample('A').sum()
    yearly = yearly.fillna(0)
    monthly = categories.get_group(category).resample('M').sum()
    monthly = monthly.fillna(0)

    monthly_mean = monthly.mean()['Amount']
    yearly_mean = yearly.mean()['Amount']
    yearly.index = yearly.index.map(lambda t: t.strftime('%Y'))
    monthly.index = monthly.index.map(lambda t: t.strftime('%b %Y'))

    category_data = {
        'name': category,
        'monthly_mean': '{0:.2f}'.format(monthly_mean),
        'sum': '{0:.2f}'.format(categories.get_group(category).sum()['Amount']),
        'yearly_mean': '{0:.2f}'.format(yearly_mean),
    }

    json_data = {
        'category': category_data,
        'monthly': monthly.to_dict(into=collections.OrderedDict),
        'yearly': yearly.to_dict()
    }
    return JsonResponse(json_data)
예제 #2
0
파일: views.py 프로젝트: tlam/spendalot
def details_json(request, slug):
    df = Expense.data_frame()
    categories = df.groupby('Category')
    category = Category.objects.get(slug=slug).name
    yearly = categories.get_group(category).resample('A').sum()
    yearly = yearly.fillna(0)
    monthly = categories.get_group(category).resample('M').sum()
    monthly = monthly.fillna(0)

    monthly_mean = monthly.mean()['Amount']
    yearly_mean = yearly.mean()['Amount']
    yearly.index = yearly.index.map(lambda t: t.strftime('%Y'))
    monthly.index = monthly.index.map(lambda t: t.strftime('%b %Y'))

    category_data = {
        'name': category,
        'monthly_mean': '{0:.2f}'.format(monthly_mean),
        'sum':
        '{0:.2f}'.format(categories.get_group(category).sum()['Amount']),
        'yearly_mean': '{0:.2f}'.format(yearly_mean),
    }

    json_data = {
        'category': category_data,
        'monthly': monthly.to_dict(into=collections.OrderedDict),
        'yearly': yearly.to_dict()
    }
    return JsonResponse(json_data)
예제 #3
0
파일: views.py 프로젝트: tlam/spendalot
def cuisines(request):
    days = 30
    past = datetime.now() - timedelta(days=days)
    expenses = Expense.objects.filter(category__name='Restaurant',
                                      date__gte=past).order_by('-date')

    last_year = Expense.objects.filter(category__name='Restaurant',
                                       date__gte=past - timedelta(days=365),
                                       date__lte=datetime.now() -
                                       timedelta(days=365)).order_by('-date')

    df = Expense.data_frame()
    top_cuisines = df.groupby(['Cuisine'
                               ]).count().sort_values('Category',
                                                      ascending=False).head(40)
    context = {
        'days':
        days,
        'expenses':
        expenses,
        'last_year':
        last_year,
        'top_cuisines':
        top_cuisines['Category'].to_dict(into=collections.OrderedDict)
    }
    return render(request, 'expenses/cuisines.html', context)
예제 #4
0
파일: views.py 프로젝트: tlam/spendalot
def category_stats_json(request):
    df = Expense.data_frame()
    categories = df.groupby('Category')
    data = {}
    for category in Category.objects.all():
        if category.name in categories.groups:
            data[category.name] = '{:.2f}'.format(categories.get_group(category.name).sum()['Amount'])

    return JsonResponse(data)
예제 #5
0
파일: views.py 프로젝트: tlam/spendalot
def category_stats_json(request):
    df = Expense.data_frame()
    categories = df.groupby('Category')
    data = {}
    for category in Category.objects.all():
        if category.name in categories.groups:
            data[category.name] = '{:.2f}'.format(
                categories.get_group(category.name).sum()['Amount'])

    return JsonResponse(data)
예제 #6
0
파일: views.py 프로젝트: tlam/spendalot
def cuisines(request):
    days = 30
    past = datetime.now() - timedelta(days=days)
    expenses = Expense.objects.filter(
        category__name='Restaurant',
        date__gte=past).order_by('-date')

    last_year = Expense.objects.filter(
        category__name='Restaurant',
        date__gte=past - timedelta(days=365),
        date__lte=datetime.now() - timedelta(days=365)).order_by('-date')

    df = Expense.data_frame()
    top_cuisines = df.groupby(['Cuisine']).count().sort_values('Category', ascending=False).head(40)
    context = {
        'days': days,
        'expenses': expenses,
        'last_year': last_year,
        'top_cuisines': top_cuisines['Category'].to_dict(into=collections.OrderedDict)
    }
    return render(
        request,
        'expenses/cuisines.html',
        context)
예제 #7
0
파일: tests.py 프로젝트: tlam/spendalot
 def test_data_frame(self):
     df = Expense.data_frame()
     self.assertEqual(df.Description[0], 'Spotify')
예제 #8
0
파일: tests.py 프로젝트: tlam/spendalot
 def test_data_frame(self):
     df = Expense.data_frame()
     self.assertEqual(df.Description[0], 'Spotify')