def get(self, request, *args, **kwargs): date = datetime.now() account = Account.objects.get(id=kwargs['pk']) incomes = Income.objects.filter(user=request.user, account=account, created_date__year=date.year, created_date__month=date.month) spendings = Spending.objects.filter(user=request.user, account=account, created_date__year=date.year, created_date__month=date.month) currency = account.currency total_incomes, total_spendings = assembly(incomes), assembly(spendings) total_savings = total_incomes - total_spendings spendings_percent = percentages_of_incomes(total_incomes, total_spendings) savings_percent = percentages_of_incomes(total_incomes, total_savings) max_income, max_spending = max_amount(incomes), max_amount(spendings) context = { 'title': template_titles['dashboard_title'], 'account': account, 'incomes': incomes, 'spendings': spendings, 'currency': currency, 'total_incomes': total_incomes, 'total_spendings': total_spendings, 'total_savings': total_savings, 'spendings_percent': spendings_percent, 'savings_percent': savings_percent, 'max_income': max_income, 'max_spending': max_spending, } return render(request, self.template_name, context)
def post(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) self.account = Account.objects.get( id=request.POST.get('accountId') ) if request.user.pro_membership else Account.objects.filter( user=request.user).first() year = datetime.strptime(request.POST.get('year'), '%Y') month = datetime.strptime(request.POST.get('month'), '%m') self.incomes = Income.objects.filter(user=request.user, account=self.account, created_date__year=year.year, created_date__month=month.month) self.spendings = Spending.objects.filter( user=request.user, account=self.account, created_date__year=year.year, created_date__month=month.month) total_incomes, total_spendings = assembly(self.incomes), assembly( self.spendings) context['currency'] = self.account.currency context['incomes'] = self.incomes context['spendings'] = self.spendings context['total_incomes'] = total_incomes context['total_spendings'] = total_spendings context['total_savings'] = round(total_incomes - total_spendings, 2) context['year'] = year context['month'] = month return render(request, self.template_name, context)
def chart_pie(request, pk): account = Account.objects.get(id=pk) if pk else None object = Income if '/users/dashboard/incomes-chart-pie/' in request.get_full_path( ) else Spending objects = object.objects.filter(user=request.user, account=account, created_date__year=datetime.now().year, created_date__month=datetime.now().month) total_objects = assembly(objects) data = [] categories = [ 'Salary', 'Awards', 'Grants', 'Sale', 'Dividents', 'Rental', 'Refunds', 'Coupons', 'Lottery', 'Capital', 'Investments', 'Gift', 'Others' ] if object is Income else [ 'Utilities', 'Rent', 'Invoices', 'Shopping', 'Food', 'Education', 'Fun', 'Investment', 'Others' ] for category in categories: category_objects = object.objects.filter( user=request.user, account=account, created_date__year=datetime.now().year, created_date__month=datetime.now().month, category=category) total_category_objects = assembly(category_objects) if total_objects and total_category_objects: data.append({ category: round((total_category_objects / total_objects) * 100, 2) }) return JsonResponse(data, safe=False)
def get(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) total_incomes = total_spendings = None if request.user.pro_membership: total_incomes = total_currency_converter(request.user, Income, self.accounts, context['currency']) total_spendings = total_currency_converter(request.user, Spending, self.accounts, context['currency']) else: total_incomes, total_spendings = assembly(self.incomes), assembly( self.spendings) context['total_incomes'] = total_incomes context['total_spendings'] = total_spendings context['total_savings'] = round(total_incomes - total_spendings, 2) return render(request, self.template_name, context)
def chart_area(request, pk): data, date_distance, checks = [], -1, 12 account = Account.objects.get(id=pk) if pk else None object = Income if '/users/dashboard/incomes-chart-area/' in request.get_full_path( ) else Spending first_object = object.objects.filter( user=request.user, account=account).first() if account else object.objects.filter( user=request.user).first() last_object = object.objects.filter( user=request.user, account=account).last() if account else object.objects.filter( user=request.user).last() if first_object and last_object: date_distance = (last_object.created_date - first_object.created_date).days if 1 <= date_distance <= 365: year, month = first_object.created_date.year, first_object.created_date.month while checks: date = datetime(year, month, 1) if month == 12: objects = object.objects.filter(user=request.user, account=account, created_date__year=year, created_date__month=month) data.append({date.strftime('%b'): assembly(objects)}) month = 1 year += 1 else: objects = object.objects.filter(user=request.user, account=account, created_date__year=year, created_date__month=month) data.append({date.strftime('%b'): assembly(objects)}) month += 1 checks -= 1 elif date_distance > 0: year, month = last_object.created_date.year - 1, last_object.created_date.month + 1 while checks: date = datetime(year, month, 1) if month == 12: objects = object.objects.filter(user=request.user, account=account, created_date__year=year, created_date__month=month) data.append({date.strftime('%b'): assembly(objects)}) month = 1 year += 1 else: objects = object.objects.filter(user=request.user, account=account, created_date__year=year, created_date__month=month) data.append({date.strftime('%b'): assembly(objects)}) month += 1 checks -= 1 elif date_distance == 0: year, month = first_object.created_date.year, first_object.created_date.month while checks: date = datetime(year, month, 1) if month == 12: objects = object.objects.filter(user=request.user, account=account, created_date__year=year, created_date__month=month) data.append({date.strftime('%b'): assembly(objects)}) month = 1 year += 1 else: objects = object.objects.filter(user=request.user, account=account, created_date__year=year, created_date__month=month) data.append({date.strftime('%b'): assembly(objects)}) month += 1 checks -= 1 return JsonResponse(data, safe=False)