-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters.py
51 lines (40 loc) · 1.51 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from django import template
from datetime import datetime
from collections.abc import Iterable
from decimal import Decimal
register = template.Library()
@register.filter()
def format_date(date):
"""Function takes a datetime object and stringifies it down to MM/DD/YYYY format"""
try:
start_date = datetime.strftime(date, '%m/%d/%Y')
except (TypeError, ValueError) as e:
start_date = date
pass
return start_date
@register.filter(name='has_user')
def has_user(value, match):
"""function expects an array of dictionaries of Entry type, loops through and tries to match to provided User in match"""
if isinstance(value, Iterable):
for entry in value:
if entry.user and entry.user == match:
return entry
else:
return False
else:
return False
@register.filter(name="percent_rating")
def percent_rating(value):
"""take rating value and divide by 3 return decimal percentage value"""
value = Decimal(value)
value = round(value / 3, 2) * 100
return value
@register.filter(name="num")
def num(value):
"""take rating value and divide by 3 return decimal percentage value"""
value = Decimal(value)
return value
@register.inclusion_tag("challenges/video_player.html", takes_context=False)
def vid_player(video_url, id=None, width='100%', height='auto'):
""" take url value and does dynamic html for a video player"""
return {'video_url': video_url, 'width': width, 'height': height, 'id': id }