def test_user_analytic_create(self):
        response = self.client.post(self.base_url)
        self.assertEqual(response.data['message'], 'Analytics created.')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        analytics = UserAnalytic.objects.filter(user=self.user)
        self.assertEqual(len(analytics), 5)
        for analytic in analytics:
            self.assertEqual(analytic.frequency, 0)

        date_param = '2020-10-09'
        response2 = self.client.post(f"{self.base_url}?date={date_param}")
        self.assertEqual(response2.data['message'], 'Analytics created.')
        self.assertEqual(response2.status_code, status.HTTP_201_CREATED)
        analytics = UserAnalytic.objects.filter(user=self.user,
                                                date=get_date(
                                                    {'date': date_param}))
        self.assertEqual(len(analytics), 5)
        for analytic in analytics:
            self.assertEqual(analytic.frequency, 0)

        label = 'Post Saver Nav'
        response3 = self.client.post(f"{self.base_url}", {'label': label})
        self.assertEqual(response3.data['message'], 'Analytics incremented.')
        self.assertEqual(response3.status_code, status.HTTP_200_OK)
        analytics2 = UserAnalytic.objects.filter(user=self.user, label=label)
        self.assertEqual(len(analytics2), 2)
        self.assertEqual(analytics2[0].frequency, 1)
        self.assertEqual(analytics2[1].frequency, 0)

        self.client.post(f"{self.base_url}?date=2020-10-10")
        self.client.post(f"{self.base_url}?date=2020-10-11")
        self.client.post(f"{self.base_url}?date=2020-10-12")
        self.client.post(f"{self.base_url}?date=2020-10-13")
        self.client.post(f"{self.base_url}?date=2020-10-14")
        self.client.post(f"{self.base_url}?date=2020-10-15")
        self.assertEqual(
            len(UserAnalyticThreshold.objects.filter(user=self.user)), 0)
        self.client.post(f"{self.base_url}?date=2020-10-16")
        self.assertEqual(
            len(UserAnalyticThreshold.objects.filter(user=self.user)), 5)
        self.client.post(f"{self.base_url}?date=2020-10-17")
        self.assertEqual(
            UserAnalytic.objects.filter(user=self.user,
                                        date=get_date({'date': '2020-10-15'
                                                       }))[0].threshold, None)
        self.assertEqual(
            UserAnalytic.objects.filter(user=self.user,
                                        date=get_date({'date': '2020-10-17'
                                                       }))[0].threshold,
            UserAnalyticThreshold.objects.filter(user=self.user)[0])
    def create(self, request, *args, **kwargs):
        obj_date = get_date(self.request.query_params)
        user = self.request.user
        create_unarchived_vice_analytics(user, obj_date)

        vice_analytics = ViceAnalytic.objects.filter(user=user, date=obj_date)
        serialized = ViceAnalyticSerializer(vice_analytics, many=True)
        return Response(serialized.data, status=status.HTTP_201_CREATED)
    def test_user_analytic_list(self):
        self.client.post(self.base_url)
        response = self.client.get(self.base_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 5)
        today = date.today()
        self.assertEqual(get_date({'date': response.data[0]['date']}), today)
        self.client.post(self.base_url)
        response2 = self.client.get(self.base_url)
        self.assertEqual(response2.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response2.data), 5)
        self.assertEqual(get_date({'date': response2.data[0]['date']}), today)
        self.assertEqual(response2.data[0]['frequency'], 0)

        self.client.post(f"{self.base_url}?date=2020-10-09")
        response3 = self.client.get(f"{self.base_url}?date=2020-10-09")
        self.assertEqual(response3.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response3.data), 5)
        self.assertEqual(response3.data[0]['date'], '2020-10-09')
def increment_user_analytic_frequency(request):
    obj_date = get_date(request.query_params)  # 2020-10-10
    label_request = request.data.get('label', None)
    if label_request:
        obj, created = UserAnalytic.objects.get_or_create(user=request.user,
                                                          label=label_request,
                                                          date=obj_date)
        if not created:
            obj.frequency += 1
            obj.save()
            return True
    return False
    def test_get_week_range(self):
        date1 = date_utils.get_date({'date': '2020-05-14'})
        date1 = date1.isocalendar()
        response = date_utils.get_week_range(date1)
        self.assertEqual(response[0], date(2020, 5, 10))
        self.assertEqual(response[1], date(2020, 5, 16))

        date2 = date_utils.get_date({'date': '2020-12-28'})
        date2 = date2.isocalendar()
        response = date_utils.get_week_range(date2)
        self.assertEqual(response[0], date(2020, 12, 27))
        self.assertEqual(response[1], date(2021, 1, 2))

        date3 = date_utils.get_date({'date': '2021-01-03'})
        date3 = date3.isocalendar()
        response = date_utils.get_week_range(date3)
        self.assertEqual(response[0], date(2021, 1, 3))
        self.assertEqual(response[1], date(2021, 1, 9))

        date4 = date_utils.get_date({'date': '2021-01-31'})
        date4 = date4.isocalendar()
        response = date_utils.get_week_range(date4)
        self.assertEqual(response[0], date(2021, 1, 31))
        self.assertEqual(response[1], date(2021, 2, 6))
 def create(self, request):
     obj_date = get_date(self.request.query_params)
     user = self.request.user
     for label in LABELS:
         try:
             ua_threshold = UserAnalyticThreshold.objects.get(
                 user=user, label=label)
         except UserAnalyticThreshold.DoesNotExist:
             try:
                 ua_threshold = create_user_analytic_threshold(
                     user=user, label=label)
             except Exception:
                 raise Http404('Something went wrong.') from None
         create_user_analytic(user=user, label=label,
                              obj_date=obj_date, threshold=ua_threshold)
     incremented = increment_user_analytic_frequency(self.request)
     if incremented:
         return Response({'message': 'Analytics incremented.'}, status=status.HTTP_200_OK)
     return Response({'message': 'Analytics created.'}, status=status.HTTP_201_CREATED)
    def test_vice_analytic_create(self):
        self.client.post(self.vice_url, data=self.sample_vice)

        response = self.client.post(self.base_url)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]["vice"]["name"],
                         self.sample_vice["name"])
        self.assertEqual(get_date({"date": response.data[0]["date"]}),
                         date.today())
        self.assertEqual(response.data[0]["frequency"], 0)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        response2 = self.client.post(self.base_url)
        self.assertEqual(len(response2.data), 1)

        new_vice = self.sample_vice.copy()
        new_vice["archived"] = True
        self.client.post(self.vice_url, data=new_vice)

        response3 = self.client.post(self.base_url)
        self.assertEqual(len(response3.data), 1)
Esempio n. 8
0
def get_timeframe_queryset(request):
    obj_date = get_date(request.query_params)
    timeframe = request.query_params.get('timeframe', None)
    user = request.user
    if timeframe == 'day':
        qs = Daily.objects.filter(user=user, date=obj_date)
    elif timeframe == 'week':
        isocalendar = obj_date.isocalendar()
        week_dates = get_week_range(isocalendar)
        qs = Daily.objects.filter(user=user,
                                  date__range=(week_dates[0], week_dates[1]))
    elif timeframe == 'month':
        month_dates = get_month_range(obj_date.year, obj_date.month)
        qs = Daily.objects.filter(user=user,
                                  date__range=(month_dates[0], month_dates[1]))
    elif timeframe == 'year':
        year_dates = get_year_range(obj_date.year)
        qs = Daily.objects.filter(user=user,
                                  date__range=(year_dates[0], year_dates[1]))
    else:
        qs = Daily.objects.filter(user=user, date=date.today())

    return qs
 def get_queryset(self):
     obj_date = get_date(self.request.query_params)
     isocalendar = obj_date.isocalendar()
     week_dates = get_week_range(isocalendar)
     return UserAnalytic.objects.filter(user=self.request.user, date__range=(
         week_dates[0], week_dates[1])).order_by('id')
 def get_queryset(self):
     obj_date = get_date(self.request.query_params)
     return ViceAnalytic.objects.filter(user=self.request.user, date=obj_date)
 def test_get_date(self):
     data = {}
     response = date_utils.get_date(data)
     self.assertEqual(response, date.today())
 def get_queryset(self):
     obj_date = get_date(self.request.query_params)
     return TrackTime.objects.filter(user=self.request.user, date=obj_date)
def create_sample_daily(habit, user, date):
    daily = Daily(habit=habit, user=user, date=get_date({"date": date}))
    daily.save()