# # ex:ts=4:sw=4:sts=4:et # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- # # BitBake Toaster Implementation # # Copyright (C) 2013 Intel Corporation # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os from os.path import relpath import re import json as JsonLib from django import template from django.template.defaultfilters import filesizeformat from django.utils.safestring import mark_safe from django.contrib.auth.models import Group register = template.Library() @register.simple_tag def time_difference(start_time, end_time): return end_time - start_time @register.filter(name = 'sectohms') def sectohms(time): try: tdsec = int(time) except ValueError: tdsec = 0 hours = int(tdsec / 3600) return "%02d:%02d:%02d" % (hours, int((tdsec - (hours * 3600))/ 60), int(tdsec) % 60) @register.filter(name = 'get_tasks') def get_tasks(queryset): return list(target + ':' + task if task else target \ for target, task in queryset.values_list('target', 'task')) @register.filter(name = "json") def json(value, default = None): # JSON spec says that "\/" is functionally identical to "/" to allow for HTML-tag embedding in JSON strings # unfortunately, I can't find any option in the json module to turn on forward-slash escaping, so we do # it manually here return mark_safe(JsonLib.dumps(value, indent=2, default = default, ensure_ascii=False).replace('' r += '' return mark_safe(r) @register.filter def get_dict_value(dictionary, key): """ return the value of a dictionary key """ try: return dictionary[key] except (KeyError, IndexError): return '' @register.filter def is_shaid(text): """ return True if text length is 40 characters and all hex-digits """ try: int(text, 16) if len(text) == 40: return True return False except ValueError: return False @register.filter def cut_path_prefix(fullpath, prefixes): """Cut path prefix from fullpath.""" for prefix in prefixes: if fullpath.startswith(prefix): return relpath(fullpath, prefix) return fullpath @register.filter def for_target(package_dependencies, target): """ filter the dependencies to be displayed by the supplied target if no dependences are found for the target then return the predicted dependences""" return package_dependencies.for_target_or_none(target) @register.filter(name = 'recommend_display') def recommend_display(recommend): if recommend == 0: return '0' elif recommend == -3: return '<= -3' elif recommend == 3: return '>= 3' else: return '%s' % recommend @register.filter(name='basename') def basename(value): return os.path.basename(value) @register.filter(name='has_group') def has_group(user, group_name): if user.is_superuser: return True group = Group.objects.get(name=group_name) return group in user.groups.all()