2017-08-13 15:59:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-08-18 05:53:48 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2017-08-13 20:26:52 +00:00
|
|
|
def filter_by_params(request, model, available_params):
|
|
|
|
queryset = model.objects.all()
|
|
|
|
|
|
|
|
for param in available_params:
|
|
|
|
value = request.query_params.get(param, None)
|
|
|
|
if value is not None:
|
|
|
|
queryset = queryset.filter(**{param: value})
|
|
|
|
|
|
|
|
return queryset
|
2017-08-18 05:53:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def timer_stop(timer_id, end=None):
|
2017-08-19 20:46:50 +00:00
|
|
|
"""Stop a timer instance by setting it's end field."""
|
2017-08-18 05:53:48 +00:00
|
|
|
if not end:
|
|
|
|
end = timezone.now()
|
|
|
|
from .models import Timer
|
|
|
|
timer_instance = Timer.objects.get(id=timer_id)
|
|
|
|
timer_instance.end = end
|
|
|
|
timer_instance.save()
|
2017-08-25 20:33:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def duration_string(duration):
|
|
|
|
"""Format hours, minutes and seconds in a human-friendly way (e.g. "2
|
|
|
|
hours, 25 minutes, 31 seconds")"""
|
|
|
|
h, m, s = duration_parts(duration)
|
|
|
|
|
|
|
|
duration = ''
|
|
|
|
if h > 0:
|
|
|
|
duration = '{} hour{}'.format(h, 's' if h > 1 else '')
|
|
|
|
if m > 0:
|
|
|
|
duration += '{}{} minute{}'.format(
|
|
|
|
'' if duration is '' else ', ', m, 's' if m > 1 else '')
|
|
|
|
if s > 0:
|
|
|
|
duration += '{}{} second{}'.format(
|
|
|
|
'' if duration is '' else ', ', s, 's' if s > 1 else '')
|
|
|
|
|
|
|
|
return duration
|
|
|
|
|
|
|
|
|
|
|
|
def duration_parts(duration):
|
|
|
|
"""Get hours, minutes and seconds from a timedelta."""
|
|
|
|
if not isinstance(duration, timezone.timedelta):
|
|
|
|
raise TypeError('Duration provided must be a timedetla')
|
|
|
|
h, remainder = divmod(duration.seconds, 3600)
|
|
|
|
h += duration.days * 24
|
|
|
|
m, s = divmod(remainder, 60)
|
|
|
|
return h, m, s
|