Ejemplo n.º 1
0
 def test_clean_no_previous(self):
     now = timezone.now()
     form = SubmitCaffeineForm(self.user,
                               DRINK_TYPES.coffee,
                               data={
                                   'date': now.date(),
                                   'time': now.time()
                               })
     self.assertTrue(form.is_valid(), str(form.errors))
Ejemplo n.º 2
0
 def test_clean_old_previous(self):
     Caffeine.objects.create(
         user=self.user, ctype=DRINK_TYPES.coffee,
         date=timezone.now() - timedelta(minutes=10))
     now = timezone.now()
     form = SubmitCaffeineForm(
         self.user, DRINK_TYPES.coffee,
         data={'date': now.date(), 'time': now.time()})
     self.assertTrue(form.is_valid(), str(form.errors))
Ejemplo n.º 3
0
 def test_submit_older(self):
     Caffeine.objects.create(
         user=self.user, ctype=DRINK_TYPES.coffee,
         date=timezone.now())
     before = timezone.now() - timedelta(
         minutes=settings.MINIMUM_DRINK_DISTANCE + 1)
     form = SubmitCaffeineForm(
         self.user, DRINK_TYPES.coffee,
         data={'date': before.date(), 'time': before.time()})
     self.assertTrue(form.is_valid(), str(form.errors))
Ejemplo n.º 4
0
 def test_clean_old_previous(self):
     Caffeine.objects.create(user=self.user,
                             ctype=DRINK_TYPES.coffee,
                             date=timezone.now() - timedelta(minutes=10))
     now = timezone.now()
     form = SubmitCaffeineForm(self.user,
                               DRINK_TYPES.coffee,
                               data={
                                   'date': now.date(),
                                   'time': now.time()
                               })
     self.assertTrue(form.is_valid(), str(form.errors))
Ejemplo n.º 5
0
 def test_submit_older(self):
     Caffeine.objects.create(user=self.user,
                             ctype=DRINK_TYPES.coffee,
                             date=timezone.now())
     before = timezone.now() - timedelta(
         minutes=settings.MINIMUM_DRINK_DISTANCE + 1)
     form = SubmitCaffeineForm(self.user,
                               DRINK_TYPES.coffee,
                               data={
                                   'date': before.date(),
                                   'time': before.time()
                               })
     self.assertTrue(form.is_valid(), str(form.errors))
Ejemplo n.º 6
0
 def test_submit_close_older_fails(self):
     Caffeine.objects.create(
         user=self.user, ctype=DRINK_TYPES.coffee,
         date=timezone.now())
     close_before = timezone.now() - timedelta(
         minutes=settings.MINIMUM_DRINK_DISTANCE - 1)
     form = SubmitCaffeineForm(
         self.user, DRINK_TYPES.coffee,
         data={'date': close_before.date(), 'time': close_before.time()})
     self.assertFalse(form.is_valid())
     form_errors = form.non_field_errors()
     self.assertEqual(len(form_errors), 1)
     self.assertRegexpMatches(
         form_errors[0],
         r'^Your last %(drink)s was less than %(minutes)d minutes ago' % {
             'drink': DRINK_TYPES[DRINK_TYPES.coffee],
             'minutes': settings.MINIMUM_DRINK_DISTANCE
         })
Ejemplo n.º 7
0
def add_drink(request, userinfo, messages, *args, **kwargs):
    """
    Submit a caffeinated beverage.

    :param HttpRequest request: POST request
    :param User userinfo: :py:class:`caffeine.models.User`
    :param dict messages: message dictionary
    :return: messages array or :py:class:`django.http.HttpResponseBadRequest`

    """

    ctype = request.POST.get('beverage')
    drinktime = request.POST.get('time')
    time = None
    if not ctype:
        messages.setdefault('error', []).append(
            API_ERROR_MISSING_PARAM_BEVERAGE)
    elif not hasattr(DRINK_TYPES, ctype):
        messages.setdefault('error', []).append(
            API_ERROR_INVALID_BEVERAGE)
    if not drinktime:
        messages.setdefault('error', []).append(
            API_ERROR_MISSING_PARAM_TIME)
    else:
        time = _parse_drinktime(drinktime, messages)
        if time is not None and time > datetime.now() + timedelta(minutes=1):
            messages.setdefault('error', []).append(API_ERROR_FUTURE_DATETIME)
    if 'error' in messages:
        return HttpResponseBadRequest(json.dumps(messages), 'text/json')
    data = {'date': time.date(), 'time': time.time()}
    form = SubmitCaffeineForm(userinfo, getattr(DRINK_TYPES, ctype), data)
    form.date = time.date()
    form.time = time.time()
    if not form.is_valid():
        for key in form.errors:
            messages.setdefault('error', []).extend(form.errors[key])
        return HttpResponseBadRequest(json.dumps(messages), 'text/json')
    drink = form.save()
    messages['success'] = _('Your %(drink)s has been registered!') % {
        'drink': drink
    }
    return messages
Ejemplo n.º 8
0
 def test_submit_close_older_fails(self):
     Caffeine.objects.create(user=self.user,
                             ctype=DRINK_TYPES.coffee,
                             date=timezone.now())
     close_before = timezone.now() - timedelta(
         minutes=settings.MINIMUM_DRINK_DISTANCE - 1)
     form = SubmitCaffeineForm(self.user,
                               DRINK_TYPES.coffee,
                               data={
                                   'date': close_before.date(),
                                   'time': close_before.time()
                               })
     self.assertFalse(form.is_valid())
     form_errors = form.non_field_errors()
     self.assertEqual(len(form_errors), 1)
     self.assertRegexpMatches(
         form_errors[0],
         r'^Your last %(drink)s was less than %(minutes)d minutes ago' % {
             'drink': DRINK_TYPES[DRINK_TYPES.coffee],
             'minutes': settings.MINIMUM_DRINK_DISTANCE
         })
Ejemplo n.º 9
0
 def test_clean_no_previous(self):
     now = timezone.now()
     form = SubmitCaffeineForm(
         self.user, DRINK_TYPES.coffee,
         data={'date': now.date(), 'time': now.time()})
     self.assertTrue(form.is_valid(), str(form.errors))