root / snf-astakos-app / astakos / im / templatetags / filters.py @ 9efd0075
History | View | Annotate | Download (6.6 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import calendar |
35 |
import datetime |
36 |
import math |
37 |
|
38 |
from collections import defaultdict |
39 |
|
40 |
from django import template |
41 |
from django.core.paginator import Paginator, EmptyPage |
42 |
from django.db.models.query import QuerySet |
43 |
from django.utils.safestring import mark_safe |
44 |
|
45 |
from synnefo.lib.ordereddict import OrderedDict |
46 |
|
47 |
from astakos.im import settings |
48 |
from astakos.im.models import ProjectResourceGrant |
49 |
from astakos.im.views import util as views_util |
50 |
from astakos.im import util |
51 |
|
52 |
register = template.Library() |
53 |
|
54 |
DELIM = ','
|
55 |
|
56 |
|
57 |
@register.filter
|
58 |
def monthssince(joined_date): |
59 |
now = datetime.datetime.now() |
60 |
date = datetime.datetime( |
61 |
year=joined_date.year, month=joined_date.month, day=1)
|
62 |
months = [] |
63 |
|
64 |
month = date.month |
65 |
year = date.year |
66 |
timestamp = calendar.timegm(date.utctimetuple()) |
67 |
|
68 |
while date < now:
|
69 |
months.append((year, month, timestamp)) |
70 |
|
71 |
if date.month < 12: |
72 |
month = date.month + 1
|
73 |
year = date.year |
74 |
else:
|
75 |
month = 1
|
76 |
year = date.year + 1
|
77 |
|
78 |
date = datetime.datetime(year=year, month=month, day=1)
|
79 |
timestamp = calendar.timegm(date.utctimetuple()) |
80 |
|
81 |
return months
|
82 |
|
83 |
|
84 |
@register.filter
|
85 |
def to_unicode(s): |
86 |
return unicode(s) |
87 |
|
88 |
|
89 |
@register.filter
|
90 |
def to_string(s): |
91 |
return str(s) |
92 |
|
93 |
|
94 |
@register.filter
|
95 |
def lookup(d, key): |
96 |
try:
|
97 |
return d.get(key)
|
98 |
except:
|
99 |
return
|
100 |
|
101 |
|
102 |
@register.filter
|
103 |
def lookup_uni(d, key): |
104 |
return d.get(unicode(key)) |
105 |
|
106 |
|
107 |
@register.filter
|
108 |
def dkeys(d): |
109 |
return d.keys()
|
110 |
|
111 |
|
112 |
@register.filter
|
113 |
def month_name(month_number): |
114 |
return calendar.month_name[month_number]
|
115 |
|
116 |
|
117 |
@register.filter
|
118 |
def todate(value, arg=''): |
119 |
secs = int(value) / 1000 |
120 |
return datetime.datetime.fromtimestamp(secs)
|
121 |
|
122 |
|
123 |
# @register.filter
|
124 |
# def rcut(value, chars='/'):
|
125 |
# return value.rstrip(chars)
|
126 |
|
127 |
|
128 |
@register.filter
|
129 |
def paginate(l, args): |
130 |
l = l or []
|
131 |
page, delim, sorting = args.partition(DELIM) |
132 |
if sorting:
|
133 |
if isinstance(l, QuerySet): |
134 |
l = l.order_by(sorting) |
135 |
elif isinstance(l, list): |
136 |
default = ''
|
137 |
if sorting.endswith('_date'): |
138 |
default = datetime.datetime.utcfromtimestamp(0)
|
139 |
l.sort(key=lambda i: getattr(i, sorting) |
140 |
if getattr(i, sorting) else default) |
141 |
paginator = Paginator(l, settings.PAGINATE_BY) |
142 |
try:
|
143 |
paginator.len |
144 |
except AttributeError: |
145 |
paginator._count = len(list(l)) |
146 |
|
147 |
try:
|
148 |
page_number = int(page)
|
149 |
except ValueError: |
150 |
if page == 'last': |
151 |
page_number = paginator.num_pages |
152 |
else:
|
153 |
page_number = 1
|
154 |
try:
|
155 |
page = paginator.page(page_number) |
156 |
except EmptyPage:
|
157 |
page = paginator.page(1)
|
158 |
return page
|
159 |
|
160 |
|
161 |
@register.filter
|
162 |
def concat(str1, str2): |
163 |
if not str2: |
164 |
return str(str1) |
165 |
return '%s%s%s' % (str1, DELIM, str2) |
166 |
|
167 |
|
168 |
@register.filter
|
169 |
def items(d): |
170 |
if isinstance(d, defaultdict): |
171 |
return d.iteritems()
|
172 |
return d
|
173 |
|
174 |
|
175 |
@register.filter
|
176 |
def get_value_after_dot(value): |
177 |
return value.split(".")[1] |
178 |
|
179 |
# @register.filter
|
180 |
# def strip_http(value):
|
181 |
# return value.replace('http://','')[:-1]
|
182 |
|
183 |
|
184 |
@register.filter
|
185 |
def truncatename(v, max=18, append="..."): |
186 |
util.truncatename(v, max, append)
|
187 |
|
188 |
|
189 |
@register.filter
|
190 |
def selected_resource_groups(project_or_app): |
191 |
if not project_or_app: |
192 |
return []
|
193 |
|
194 |
grants = project_or_app.resource_set |
195 |
resources = grants.values_list('resource__name', flat=True) |
196 |
return map(lambda r: r.split(".")[0], resources) |
197 |
|
198 |
|
199 |
@register.filter
|
200 |
def resource_grants(project_or_app): |
201 |
try:
|
202 |
grants = project_or_app.resource_set |
203 |
grants = grants.values_list( |
204 |
'resource__name', 'member_capacity', 'project_capacity') |
205 |
return dict((e[0], {'member':e[1], 'project':e[2]}) for e in grants) |
206 |
except:
|
207 |
return {}
|
208 |
|
209 |
|
210 |
def get_resource_grant(project_or_app, rname, capacity_for): |
211 |
if project_or_app is None: |
212 |
return None |
213 |
|
214 |
resource_set = project_or_app.resource_set |
215 |
if not resource_set.filter(resource__name=rname).count(): |
216 |
return None |
217 |
|
218 |
resource = resource_set.get(resource__name=rname) |
219 |
return getattr(resource, '%s_capacity' % capacity_for) |
220 |
|
221 |
|
222 |
@register.filter
|
223 |
def get_member_resource_grant_value(project_or_app, rname): |
224 |
return get_resource_grant(project_or_app, rname, "member") |
225 |
|
226 |
|
227 |
@register.filter
|
228 |
def get_project_resource_grant_value(project_or_app, rname): |
229 |
return get_resource_grant(project_or_app, rname, "project") |
230 |
|
231 |
|
232 |
@register.filter
|
233 |
def resource_diff(r, member_or_project): |
234 |
if not hasattr(r, 'display_project_diff'): |
235 |
return '' |
236 |
|
237 |
project, member = r.display_project_diff() |
238 |
diff = dict(zip(['project', 'member'], |
239 |
r.display_project_diff())).get(member_or_project) |
240 |
tpl = '<span class="policy-diff %s">(%s)</span>'
|
241 |
cls = 'red' if diff.startswith("-") else 'green' |
242 |
return mark_safe(tpl % (cls, diff))
|
243 |
|
244 |
|
245 |
@register.filter
|
246 |
def sorted_resources(resources_set): |
247 |
return views_util.sorted_resources(resources_set)
|
248 |
|
249 |
|
250 |
@register.filter
|
251 |
def is_pending_app(app): |
252 |
if not app: |
253 |
return False |
254 |
return app.state in [app.PENDING] |
255 |
|
256 |
|
257 |
@register.filter
|
258 |
def is_denied_app(app): |
259 |
if not app: |
260 |
return False |
261 |
return app.state in [app.DENIED] |