Ejemplo n.º 1
0
    def test_get_all_activity(self):
        # First add some transactions, balances and eventlogs, and make sure the ActivityLogs are set
        Fixture1.settings_event1()
        Fixture1.transaction_event1()
        Fixture1.populate_balance1()  # 2 Activity lines
        ActivityLogEvent.get(Event.APPROVE_SELECTED_SETTINGS)
        ActivityLogEvent.get(Event.GOAL_BALANCE_CALCULATED)
        ActivityLogEvent.get(Event.GOAL_DEPOSIT_EXECUTED)

        url = '/api/v1/clients/{}/activity'.format(
            Fixture1.personal_account1().primary_owner.id)
        self.client.force_authenticate(
            user=Fixture1.personal_account1().primary_owner.user)
        response = self.client.get(url)
        self.assertEqual(len(response.data), 4)
        self.assertEqual(
            response.data[3], {
                'goal':
                1,
                'account':
                1,
                'time':
                946684800,
                'type':
                ActivityLogEvent.get(
                    Event.APPROVE_SELECTED_SETTINGS).activity_log.id
            })  # Setting change
        self.assertEqual(
            response.data[2], {
                'balance':
                0.0,
                'time':
                978220800,
                'type':
                ActivityLogEvent.get(
                    Event.GOAL_BALANCE_CALCULATED).activity_log.id
            })  # Balance
        self.assertEqual(
            response.data[1], {
                'balance':
                3000.0,
                'time':
                978307200,
                'type':
                ActivityLogEvent.get(
                    Event.GOAL_BALANCE_CALCULATED).activity_log.id
            })  # Balance
        self.assertEqual(
            response.data[0], {
                'data': [3000.0],
                'goal':
                1,
                'account':
                1,
                'time':
                978307200,
                'type':
                ActivityLogEvent.get(
                    Event.GOAL_DEPOSIT_EXECUTED).activity_log.id
            })  # Deposit
Ejemplo n.º 2
0
    def test_get_all_activity(self):
        # First add some transactions, balances and eventlogs, and make sure the ActivityLogs are set.
        Fixture1.settings_event1()
        Fixture1.transaction_event1()
        Fixture1.populate_balance1()  # 2 Activity lines
        # We also need to activate the activity logging for the desired event types.
        ActivityLogEvent.get(Event.APPROVE_SELECTED_SETTINGS)
        ActivityLogEvent.get(Event.GOAL_BALANCE_CALCULATED)
        ActivityLogEvent.get(Event.GOAL_DEPOSIT_EXECUTED)

        url = '/api/v1/goals/{}/activity'.format(Fixture1.goal1().id)
        self.client.force_authenticate(user=Fixture1.client1().user)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 4)
        # Note the Goal not included in response as it is in request.
        self.assertEqual(response.data[0], {'time': 946684800,
                                            'type': ActivityLogEvent.get(Event.APPROVE_SELECTED_SETTINGS).activity_log.id})  # Setting change approval
        self.assertEqual(response.data[1], {'balance': 0.0,
                                            'time': 978220800,
                                            'type': ActivityLogEvent.get(Event.GOAL_BALANCE_CALCULATED).activity_log.id})  # Balance
        # Deposit. Note inclusion of amount, as we're looking at it from the goal perspective.
        self.assertEqual(response.data[2], {'amount': 3000.0,
                                            'data': [3000.0],
                                            'time': 978307200,
                                            'type': ActivityLogEvent.get(Event.GOAL_DEPOSIT_EXECUTED).activity_log.id})
        self.assertEqual(response.data[3], {'balance': 3000.0,
                                            'time': 978307200,
                                            'type': ActivityLogEvent.get(Event.GOAL_BALANCE_CALCULATED).activity_log.id})  # Balance
Ejemplo n.º 3
0
    def test_get_activity_types(self):
        # Populate some activity type items
        ActivityLog.objects.all().delete()
        ActivityLogEvent.objects.all().delete()
        ActivityLogEvent.get(Event.APPROVE_SELECTED_SETTINGS)
        ActivityLogEvent.get(Event.GOAL_WITHDRAWAL_EXECUTED)

        url = '/api/v1/settings/activity-types'
        self.client.force_authenticate(user=Fixture1.client1().user)
        response = self.client.get(url)
        self.assertEqual(len(response.data), 2)
Ejemplo n.º 4
0
    def test_event_memo(self):
        '''
        Tests event memos and assigning multiple events to one activity log item.
        :return:
        '''
        # Add a public settings event with a memo
        se = Fixture1.settings_event1()
        EventMemo.objects.create(event=se,
                                 comment='A memo for e1',
                                 staff=False)
        # Add a staff settings event with a memo
        se2 = Fixture1.settings_event2()
        EventMemo.objects.create(event=se2,
                                 comment='A memo for e2',
                                 staff=True)
        # Add a transaction event without a memo
        Fixture1.transaction_event1()
        # We also need to activate the activity logging for the desired event types.
        # We add selected and update to the same une to test that too
        al = ActivityLog.objects.create(name="Settings Funk",
                                        format_str='Settings messed with')
        ActivityLogEvent.objects.create(
            id=Event.APPROVE_SELECTED_SETTINGS.value, activity_log=al)
        ActivityLogEvent.objects.create(
            id=Event.UPDATE_SELECTED_SETTINGS.value, activity_log=al)
        ActivityLogEvent.get(Event.GOAL_DEPOSIT_EXECUTED)

        url = '/api/v1/goals/{}/activity'.format(Fixture1.goal1().id)
        # Log in as a client and make sure I see the public settings event, and the transaction, not the staff entry.
        self.client.force_authenticate(user=Fixture1.client1().user)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 3)
        self.assertEqual(response.data[2]['memos'], ['A memo for e1'])
        self.assertFalse('memos' in response.data[0])
        self.assertFalse('memos' in response.data[1])

        # Log in as the advisor and make sure I see all three events.
        self.client.force_authenticate(user=Fixture1.advisor1().user)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 3)
        self.assertEqual(response.data[2]['memos'], ['A memo for e1'])
        self.assertEqual(response.data[1]['memos'], ['A memo for e2'])
        self.assertFalse('memos' in response.data[0])
Ejemplo n.º 5
0
def get(request, obj):
    if isinstance(obj, Goal):
        goal = obj
        goal_ids = [goal.id]
        gct = ContentType.objects.get_for_model(Goal)
        el_filter = (Q(content_type=gct, object_id__in=goal_ids))
    elif isinstance(obj, ClientAccount):
        goal = None
        goal_ids = obj.goals.values_list('id', flat=True)
        # Filter for only the events where the object is account or goal
        ctm = ContentType.objects.get_for_models(ClientAccount, Goal)
        el_filter = (Q(content_type=ctm[ClientAccount], object_id=obj.id)
                     | Q(content_type=ctm[Goal], object_id__in=goal_ids))
    else:
        raise Exception("object type: {} not supported.".format(type(obj)))

    query_params = ActivityQueryParamSerializer.parse(request.query_params)
    sd = query_params.get('sd', None)
    ed = query_params.get('ed', None)

    # Get the eventlog activity items
    date_constraint = Q()
    if sd is not None:
        date_constraint &= Q(date__gte=sd)
    if ed is not None:
        date_constraint &= Q(date__lte=ed)
    events = Log.objects.filter(el_filter
                                & date_constraint).prefetch_related('memos')

    # Get all the transactions for the period
    transactions = get_transactions(sd, ed, goal_ids)
    items = parse_event_logs(request, events, transactions, goal)

    # Get the balances
    tp = ActivityLogEvent.get(Event.GOAL_BALANCE_CALCULATED).activity_log.id
    qs = HistoricalBalance.objects.filter(
        date_constraint,
        goal__in=goal_ids).values('date').annotate(sum=Sum('balance'))

    # Join the two lists to one sorted on date
    items.extend([{
        'type':
        tp,
        'time':
        int((datetime.combine(bal['date'], time()) -
             EPOCH_TM).total_seconds()),
        'balance':
        Decimal.from_float(bal['sum']).quantize(DEC_2PL)
    } for bal in qs])
    return Response(sorted(items, key=operator.itemgetter("time")))