2017-08-13 15:59:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from math import floor
|
|
|
|
|
|
|
|
|
2017-08-16 23:08:52 +00:00
|
|
|
def duration_string(start, end, short=False):
|
2017-08-13 15:59:14 +00:00
|
|
|
diff = end - start
|
2017-08-13 19:27:32 +00:00
|
|
|
h = floor(diff.seconds / 3600)
|
|
|
|
m = floor((diff.seconds - h * 3600) / 60)
|
|
|
|
s = diff.seconds % 60
|
|
|
|
|
|
|
|
duration = ''
|
2017-08-16 23:08:52 +00:00
|
|
|
if short:
|
|
|
|
duration = '{}h {}m {}s'.format(h, m, s)
|
|
|
|
else:
|
|
|
|
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 '')
|
2017-08-13 19:27:32 +00:00
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
return duration
|
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
|