mirror of https://github.com/snachodog/mybuddy.git
Add core templatetags tests and update some docstrings.
This commit is contained in:
parent
fe3e753d65
commit
df5c3e98b9
|
@ -7,7 +7,6 @@ from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.template.defaultfilters import slugify
|
from django.template.defaultfilters import slugify
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.timesince import timesince
|
|
||||||
|
|
||||||
|
|
||||||
def validate_duration(model, max_duration=timedelta(hours=24)):
|
def validate_duration(model, max_duration=timedelta(hours=24)):
|
||||||
|
|
|
@ -9,6 +9,11 @@ register = template.Library()
|
||||||
|
|
||||||
@register.filter()
|
@register.filter()
|
||||||
def bool_icon(value):
|
def bool_icon(value):
|
||||||
|
"""
|
||||||
|
Create a safe HTML version of True/False using Bootstrap styles.
|
||||||
|
:param value: a boolean.
|
||||||
|
:returns: a string of html for an icon representing the boolean.
|
||||||
|
"""
|
||||||
if value:
|
if value:
|
||||||
classes = 'icon-true text-success'
|
classes = 'icon-true text-success'
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -11,7 +11,13 @@ register = template.Library()
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def duration_string(duration, precision='s'):
|
def duration_string(duration, precision='s'):
|
||||||
"""Format a duration (e.g. "2 hours, 3 minutes, 35 seconds")."""
|
"""
|
||||||
|
Format a duration (e.g. "2 hours, 3 minutes, 35 seconds").
|
||||||
|
:param duration: a timedetla instance.
|
||||||
|
:param precision: the level of precision to return (h for hours, m for
|
||||||
|
minutes, s for seconds)
|
||||||
|
:returns: a string representation of the duration.
|
||||||
|
"""
|
||||||
if not duration:
|
if not duration:
|
||||||
return ''
|
return ''
|
||||||
try:
|
try:
|
||||||
|
@ -22,7 +28,11 @@ def duration_string(duration, precision='s'):
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def hours(duration):
|
def hours(duration):
|
||||||
"""Return "hours" portion of a duration."""
|
"""
|
||||||
|
Return the "hours" portion of a duration.
|
||||||
|
:param duration: a timedetla instance.
|
||||||
|
:returns: an integer representing the number of hours in duration.
|
||||||
|
"""
|
||||||
if not duration:
|
if not duration:
|
||||||
return 0
|
return 0
|
||||||
try:
|
try:
|
||||||
|
@ -34,7 +44,11 @@ def hours(duration):
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def minutes(duration):
|
def minutes(duration):
|
||||||
"""Return "minutes" portion of a duration."""
|
"""
|
||||||
|
Return the "minutes" portion of a duration.
|
||||||
|
:param duration: a timedetla instance.
|
||||||
|
:returns: an integer representing the number of minutes in duration.
|
||||||
|
"""
|
||||||
if not duration:
|
if not duration:
|
||||||
return 0
|
return 0
|
||||||
try:
|
try:
|
||||||
|
@ -46,7 +60,11 @@ def minutes(duration):
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def seconds(duration):
|
def seconds(duration):
|
||||||
"""Return "seconds" portion of a duration."""
|
"""
|
||||||
|
Return the "seconds" portion of a duration.
|
||||||
|
:param duration: a timedetla instance.
|
||||||
|
:returns: an integer representing the number of seconds in duration.
|
||||||
|
"""
|
||||||
if not duration:
|
if not duration:
|
||||||
return 0
|
return 0
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -11,6 +11,12 @@ register = template.Library()
|
||||||
|
|
||||||
@register.inclusion_tag('core/timer_nav.html', takes_context=True)
|
@register.inclusion_tag('core/timer_nav.html', takes_context=True)
|
||||||
def timer_nav(context, active=True):
|
def timer_nav(context, active=True):
|
||||||
|
"""
|
||||||
|
Get a list of active Timer instances to include in the nav menu.
|
||||||
|
:param context: Django's context data.
|
||||||
|
:param active: the state of Timers to filter.
|
||||||
|
:returns: a dictionary with timers data.
|
||||||
|
"""
|
||||||
request = context['request'] or None
|
request = context['request'] or None
|
||||||
timers = Timer.objects.filter(active=active)
|
timers = Timer.objects.filter(active=active)
|
||||||
perms = context['perms'] or None
|
perms = context['perms'] or None
|
||||||
|
|
|
@ -196,4 +196,3 @@ class FormsTestCase(TestCase):
|
||||||
self.assertEqual(page.status_code, 200)
|
self.assertEqual(page.status_code, 200)
|
||||||
self.assertFormError(page, 'form', 'end',
|
self.assertFormError(page, 'form', 'end',
|
||||||
'Date/time can not be in the future.')
|
'Date/time can not be in the future.')
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from core.templatetags import bootstrap, duration
|
||||||
|
|
||||||
|
|
||||||
|
class TemplateTagsTestCase(TestCase):
|
||||||
|
def test_bootstrap_bool_icon(self):
|
||||||
|
self.assertEqual(
|
||||||
|
bootstrap.bool_icon(True),
|
||||||
|
'<i class="icon icon-true text-success" aria-hidden="true"></i>')
|
||||||
|
self.assertEqual(
|
||||||
|
bootstrap.bool_icon(False),
|
||||||
|
'<i class="icon icon-false text-danger" aria-hidden="true"></i>')
|
||||||
|
|
||||||
|
def test_duration_duration_string(self):
|
||||||
|
delta = timezone.timedelta(hours=1, minutes=30, seconds=15)
|
||||||
|
self.assertEqual(
|
||||||
|
duration.duration_string(delta),
|
||||||
|
'1 hour, 30 minutes, 15 seconds')
|
||||||
|
self.assertEqual(
|
||||||
|
duration.duration_string(delta, 'm'),
|
||||||
|
'1 hour, 30 minutes')
|
||||||
|
self.assertEqual(duration.duration_string(delta, 'h'), '1 hour')
|
||||||
|
|
||||||
|
self.assertEqual(duration.duration_string(''), '')
|
||||||
|
self.assertRaises(TypeError, duration.duration_string('not a delta'))
|
||||||
|
|
||||||
|
def test_duration_hours(self):
|
||||||
|
delta = timezone.timedelta(hours=1)
|
||||||
|
self.assertEqual(duration.hours(delta), 1)
|
||||||
|
self.assertEqual(duration.hours(''), 0)
|
||||||
|
self.assertRaises(TypeError, duration.hours('not a delta'))
|
||||||
|
|
||||||
|
def test_duration_minutes(self):
|
||||||
|
delta = timezone.timedelta(minutes=45)
|
||||||
|
self.assertEqual(duration.minutes(delta), 45)
|
||||||
|
self.assertEqual(duration.minutes(''), 0)
|
||||||
|
self.assertRaises(TypeError, duration.minutes('not a delta'))
|
||||||
|
|
||||||
|
def test_duration_seconds(self):
|
||||||
|
delta = timezone.timedelta(seconds=20)
|
||||||
|
self.assertEqual(duration.seconds(delta), 20)
|
||||||
|
self.assertEqual(duration.seconds(''), 0)
|
||||||
|
self.assertRaises(TypeError, duration.seconds('not a delta'))
|
Loading…
Reference in New Issue