-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilters.py
89 lines (67 loc) · 2.28 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import datetime as dt
import sys
from functools import wraps
from docxtpl import RichText
from models import Service, ServiceItem
def nullsafe(f):
@wraps(f)
def ns(x):
return f(x) if x is not None else ''
return ns
def as_richtext(item: ServiceItem) -> RichText:
return item.as_richtext()
def service_supertitle(service: Service) -> str:
s = english_date(service.date)
if service.time:
fmt = "%I:%M%p"
s += ", " + service.time.strftime(fmt).lower()
return s
@nullsafe
def english_date(date: dt.date) -> str:
# https://stackoverflow.com/a/74227668
def format_date_with_ordinal(d, format_string):
if d.day not in (11, 12, 13):
ordinal = {'1': 'st', '2': 'nd', '3': 'rd'}.get(
str(d.day)[-1:],
'th'
)
else:
ordinal = 'th'
return d.strftime(format_string).replace('{th}', ordinal)
# try:
if sys.platform.startswith('win'):
# https://stackoverflow.com/questions/904928/python-strftime-date-without-leading-0
return format_date_with_ordinal(date, '%A %#d{th} %B %Y')
else:
return format_date_with_ordinal(date, '%A %-d{th} %B %Y')
def service_summary(service: Service) -> str:
return service.date.strftime('%Y-%m-%d') + ' ' + service_subtitle(service)
def service_header(service: Service) -> str:
return service.service_type
def service_subtitle(service: Service) -> str:
# Feastday (Secondary), Fr XX YY (Preacher: AN Other)
if service.secondary_feasts:
parens = ", ".join(sf.name for sf in service.secondary_feasts)
s = service.primary_feast.name + ' (' + parens + '),'
else:
s = service.primary_feast.name + ','
c, p = service.celebrant, service.preacher
if c:
if p and p != c:
s += f' {c} (Preacher: {p})'
else:
s += f' {c}'
else:
if p:
s += f' Preacher: {p}'
return s
# These get registered by the Flask app, and also need to be passed into
# docxtpl.
filters_context = {
'as_richtext': as_richtext,
'english_date': english_date,
'service_header': service_header,
'service_subtitle': service_subtitle,
'service_supertitle': service_supertitle,
'service_summary': service_summary,
}