Esempio n. 1
0
    def test_week(self):

        with self.settings(LANGUAGE_CODE='en-us'):
            s, e = get_date_ranges(
                datetime.date(2015, 4, 29), GRANULARITY_WEEK
            )
            self.assertEqual(s, datetime.date(2015, 4, 26))
            self.assertEqual(e, datetime.date(2015, 5, 2))

        with self.settings(LANGUAGE_CODE='fr-fr'):
            s, e = get_date_ranges(
                datetime.date(2015, 4, 29), GRANULARITY_WEEK
            )
            self.assertEqual(s, datetime.date(2015, 4, 27))
            self.assertEqual(e, datetime.date(2015, 5, 3))

        with self.settings(LANGUAGE_CODE='fr-fr'):
            s, e = get_date_ranges(
                datetime.date(2015, 7, 13), GRANULARITY_WEEK
            )
            self.assertEqual(s, datetime.date(2015, 7, 13))
            self.assertEqual(e, datetime.date(2015, 7, 19))

        with self.settings(LANGUAGE_CODE='fa'):
            s, e = get_date_ranges(
                datetime.date(2015, 4, 29), GRANULARITY_WEEK
            )
            self.assertEqual(s, datetime.date(2015, 4, 25))
            self.assertEqual(e, datetime.date(2015, 5, 1))
Esempio n. 2
0
    def test_month(self):

        s, e = get_date_ranges(
            datetime.date(2015, 2, 15), GRANULARITY_MONTH
        )
        self.assertEqual(s, datetime.date(2015, 2, 1))
        self.assertEqual(e, datetime.date(2015, 2, 28))

        s, e = get_date_ranges(
            datetime.date(2016, 2, 15), GRANULARITY_MONTH
        )
        self.assertEqual(s, datetime.date(2016, 2, 1))
        self.assertEqual(e, datetime.date(2016, 2, 29))
Esempio n. 3
0
 def get_total_unscheduled_period(self, bankaccount,
                                  granularity=GRANULARITY_MONTH):
     """
     Returns the total sum for the current period of bank transactions not
     scheduled.
     """
     return (
         self
         .filter(
             bankaccount=bankaccount,
             date__range=get_date_ranges(timezone.now(), granularity),
             scheduled=False,
         )
         .exclude(status=BankTransaction.STATUS_INACTIVE)
         .aggregate(total=models.Sum('amount'))
     )['total']
Esempio n. 4
0
    def get_context_data(self, **kwargs):
        context = super(TrendTimeView, self).get_context_data(**kwargs)
        context['bankaccount'] = self.bankaccount

        filters = self.session_data.get('filters', {})
        context['has_filters'] = bool(filters)

        if filters:

            # Get first and last bank transaction to prevent infinite pager. If
            # one is missing, it just mean that there is no data at all.
            first, last = self.get_queryset_dates_delimiters()
            if first and last:
                base_date = datetime.date(**filters['date_kwargs'])
                granularity = filters['granularity']

                if self.request.GET.get('date'):
                    try:
                        base_date = datetime.datetime.strptime(
                            self.request.GET.get('date'),
                            '%Y-%m-%d',
                        ).date()

                    except ValueError:
                        pass

                # Requested date is not out of range?
                first_range = get_date_ranges(first, granularity)[0]
                last_range = get_date_ranges(last, granularity)[1]
                if first_range <= base_date <= last_range:

                    date_start, date_end = get_date_ranges(
                        base_date,
                        granularity,
                    )

                    balance = self.get_queryset_balance(date_start)['sum'] or 0
                    balance += self.bankaccount.balance_initial
                    context['balance_initial'] = balance

                    items_qs = self.get_queryset_items(date_start, date_end)
                    items = {item['date']: item for item in items_qs}

                    # Start and end iterator at first/last bank transaction,
                    # not the range calculated.
                    iterator = DateIterator(
                        first if first > date_start else date_start,
                        last if last < date_end else date_end,
                    )
                    rows = []
                    for date_step in iterator:
                        delta = percentage = count = 0

                        # If no new bank transaction, same as previous.
                        if date_step in items:
                            delta = items[date_step]['sum']
                            percentage = (delta * 100 /
                                          balance) if balance else 0
                            balance += items[date_step]['sum']
                            count = items[date_step]['count']

                        rows.append({
                            'date': date_step,
                            'count': count,
                            'balance': balance,
                            'delta': delta,
                            'percentage': round(percentage, 2),
                        })

                    context['rows'] = rows
                    context['chart_data'] = json.dumps({
                        'data': {
                            'labels': [
                                formats.date_format(
                                    row['date'],
                                    'SHORT_DATE_FORMAT',
                                ) for row in rows
                            ],
                            'datasets': [{
                                'fillColor':
                                "rgba(66, 139, 202, 0.5)",
                                'strokeColor':
                                "rgba(66, 139, 202, 0.8)",
                                'pointColor':
                                "rgba(66, 139, 202, 0.75)",
                                'pointHighlightFill':
                                "#fff",
                                'pointHighlightStroke':
                                "rgba(66, 139, 202, 1)",
                                'data':
                                [float(row['balance']) for row in rows],
                            }],
                        },
                        'type':
                        filters['chart'],
                    })
                    paginator = DatePaginator(first_range, last_range,
                                              granularity)
                    context['page_obj'] = paginator.page(base_date)

        return context
Esempio n. 5
0
    def list(self, request, *args, **kwargs):
        serializer = TrendTimeInputSerializer(data=request.query_params)
        serializer.is_valid(raise_exception=True)
        self.filters = serializer.validated_data

        results, links = [], {}

        # Get first and last bank transaction to prevent infinite pager. If
        # one is missing, it just mean that there is no data at all.
        first, last = self.get_queryset_dates_delimiters()
        if first and last:
            base_date = self.filters['date']
            granularity = self.filters['granularity']

            # Requested date is out of range?
            first_range = get_date_ranges(first, granularity)[0]
            last_range = get_date_ranges(last, granularity)[1]
            if first_range <= base_date <= last_range:

                date_start, date_end = get_date_ranges(
                    base_date,
                    granularity,
                )

                balance = self.get_queryset_balance(date_start)['sum'] or 0
                balance += self.bankaccount.balance_initial

                items_qs = self.get_queryset_items(date_start, date_end)
                items = {item['date']: item for item in items_qs}

                # Start and end iterator at first/last bank transaction,
                # not the range calculated.
                iterator = DateIterator(
                    first if first > date_start else date_start,
                    last if last < date_end else date_end,
                )
                instances = []
                for date_step in iterator:
                    delta = percentage = count = 0

                    # If no new bank transaction, same as previous.
                    if date_step in items:
                        delta = items[date_step]['sum']
                        percentage = (delta * 100 / balance) if balance else 0
                        balance += items[date_step]['sum']
                        count = items[date_step]['count']

                    instances.append({
                        'date': date_step,
                        'count': count,
                        'delta': delta,
                        'balance': balance,
                        'percentage': percentage,
                    })

                results = TrendTimeOuputSerializer(instances, many=True).data
                paginator = DatePaginator(first_range, last_range, granularity)
                links = paginator.get_links(base_date, request)

        return Response({
            'results': results,
            'previous': links.get('previous'),
            'next': links.get('next'),
        })
Esempio n. 6
0
    def get_context_data(self, **kwargs):
        context = super(TrendTimeView, self).get_context_data(**kwargs)
        context['bankaccount'] = self.bankaccount

        filters = self.session_data.get('filters', {})
        context['has_filters'] = bool(filters)

        if filters:

            # Get first and last bank transaction to prevent infinite pager. If
            # one is missing, it just mean that there is no data at all.
            first, last = self.get_queryset_dates_delimiters()
            if first and last:
                base_date = datetime.date(**filters['date_kwargs'])
                granularity = filters['granularity']

                if self.request.GET.get('date'):
                    try:
                        base_date = datetime.datetime.strptime(
                            self.request.GET.get('date'),
                            '%Y-%m-%d',
                        ).date()

                    except ValueError:
                        pass

                # Requested date is not out of range?
                first_range = get_date_ranges(first, granularity)[0]
                last_range = get_date_ranges(last, granularity)[1]
                if first_range <= base_date <= last_range:

                    date_start, date_end = get_date_ranges(
                        base_date,
                        granularity,
                    )

                    balance = self.get_queryset_balance(date_start)['sum'] or 0
                    balance += self.bankaccount.balance_initial
                    context['balance_initial'] = balance

                    items_qs = self.get_queryset_items(date_start, date_end)
                    items = {item['date']: item for item in items_qs}

                    # Start and end iterator at first/last bank transaction,
                    # not the range calculated.
                    iterator = DateIterator(
                        first if first > date_start else date_start,
                        last if last < date_end else date_end,
                    )
                    rows = []
                    for date_step in iterator:
                        delta = percentage = count = 0

                        # If no new bank transaction, same as previous.
                        if date_step in items:
                            delta = items[date_step]['sum']
                            percentage = (delta * 100 / balance) if balance else 0
                            balance += items[date_step]['sum']
                            count = items[date_step]['count']

                        rows.append({
                            'date': date_step,
                            'count': count,
                            'balance': balance,
                            'delta': delta,
                            'percentage': round(percentage, 2),
                        })

                    context['rows'] = rows
                    context['chart_data'] = json.dumps({
                        'data': {
                            'labels': [
                                formats.date_format(
                                    row['date'], 'SHORT_DATE_FORMAT',
                                )
                                for row in rows
                            ],
                            'datasets': [
                                {
                                    'fillColor': "rgba(66, 139, 202, 0.5)",
                                    'strokeColor': "rgba(66, 139, 202, 0.8)",
                                    'pointColor': "rgba(66, 139, 202, 0.75)",
                                    'pointHighlightFill': "#fff",
                                    'pointHighlightStroke': "rgba(66, 139, 202, 1)",
                                    'data': [
                                        float(row['balance']) for row in rows
                                    ],
                                }
                            ],
                        },
                        'type': filters['chart'],
                    })
                    paginator = DatePaginator(
                        first_range, last_range, granularity
                    )
                    context['page_obj'] = paginator.page(base_date)

        return context