-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchurchtoolsapi.py
241 lines (199 loc) · 9.15 KB
/
churchtoolsapi.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
# Copyright (c) 2022 Samuel Mehrbrodt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import datetime
import io
import pickle
import os
import requests
from dotenv import load_dotenv
from os.path import exists
from PIL import Image, ImageDraw, ImageFilter
from pyactiveresource.activeresource import ActiveResource
# DEBUG MODE (cache REST API result)
DEBUG = False
CACHED_FILENAME = "persons_{group_id}.dump"
# Random limits from Churchtools API
MAX_PERSONS_LIMIT = 500
MAX_GROUP_MEMBERS_LIMIT = 100
load_dotenv()
# REST API definitions
class ApiBase(ActiveResource):
_site = 'https://' + os.getenv('CHURCHTOOLS_DOMAIN') + '/api/'
_headers = { 'Authorization': 'Login ' + os.getenv('CHURCHTOOLS_LOGIN_TOKEN') }
class Group(ApiBase):
pass
class Person(ApiBase):
pass
class Child:
def __lt__(self, other):
return self.birthdate > other.birthdate
def __str__(self):
return self.name + self.age
def str_to_date(birthdate_str):
if not birthdate_str:
return datetime.date(1900, 1, 1)
return datetime.datetime.strptime(birthdate_str, "%Y-%m-%d").date()
def __age(birthdate_str):
if not birthdate_str:
return ""
birthdate = str_to_date(birthdate_str)
today = datetime.date.today()
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
return age
def format_date(birthdate_str):
if not birthdate_str:
return ""
birthdate = str_to_date(birthdate_str)
return birthdate.strftime("%d.%m.%Y")
# From https://note.nkmk.me/en/python-pillow-square-circle-thumbnail/
def __mask_circle_transparent(pil_img, blur_radius, offset=0):
offset = blur_radius * 2 + offset
mask = Image.new("L", pil_img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((offset, offset, pil_img.size[0] - offset, pil_img.size[1] - offset), fill=255)
mask = mask.filter(ImageFilter.GaussianBlur(blur_radius))
result = pil_img.copy()
result.putalpha(mask)
return result
def __make_img_round(img_bytes):
im = Image.open(io.BytesIO(img_bytes))
im_round = __mask_circle_transparent(im, 0, 2)
img_byte_arr = io.BytesIO()
im_round.save(img_byte_arr, format='PNG')
return img_byte_arr.getvalue()
def get_persons(filter_group_id=None, filter_role_id=None, include_images=False):
filter_group_id = int(filter_group_id) if filter_group_id else None
filter_role_id = int(filter_role_id) if filter_role_id else None
filename = CACHED_FILENAME.format(group_id=filter_group_id)
if DEBUG and exists(filename):
with open(filename, 'rb') as f:
return pickle.load(f)
# Get all persons
persons_result = Person.find(from_=ApiBase._site + 'persons', limit=MAX_PERSONS_LIMIT)
persons = persons_result[0]['data']
# Filter only those in current group
if filter_group_id:
persons_filtered = []
group_url = ApiBase._site + 'groups/{id}/members'.format(id=filter_group_id)
persons_in_group_result = Group.find(from_=group_url, limit=MAX_GROUP_MEMBERS_LIMIT)
persons_in_group = persons_in_group_result[0]['data']
# Remove persons not in this group from persons
for person in persons:
found = False
for group_person in persons_in_group:
if group_person['personId'] == person['id']:
found = True
if filter_role_id:
found = found and group_person['groupTypeRoleId'] == filter_role_id
if found:
break
if found:
persons_filtered.append(person)
persons = persons_filtered
# Postprocessing
for person in persons:
# Profile pic
if include_images:
if person['imageUrl']:
person['image'] = requests.get(person['imageUrl']).content
else:
default_img_path = os.path.realpath(os.path.dirname(__file__)) + '/images/placeholder.png'
img = open(default_img_path,'rb')
person['image'] = bytes(img.read())
# Make image round
person['image'] = __make_img_round(person['image'])
# Format birthdate
if person['birthday']:
person['birthday_date'] = str_to_date(person['birthday'])
person['birthday'] = format_date(person['birthday'])
else:
person['birthday_date'] = None
# Relationships (Spouse, children)
relationships_url = ApiBase._site + 'persons/{id}/relationships'.format(id=person['id'])
relationships_result = Person.find(from_=relationships_url, limit=MAX_PERSONS_LIMIT)
relationships = relationships_result[0]['data']
person['children'] = []
person['family_id'] = "{}-{}".format(person['lastName'], person['firstName'])
person['familyEnd'] = False
personHasSpouse = False
if not relationships:
person['familyEnd'] = True
for relationship in relationships:
if relationship['relationshipTypeId'] == 1 and relationship['degreeOfRelationship'] == 'relationship.part.child': # Kind
child = Child()
child.name = relationship['relative']['domainAttributes']['firstName']
child_result = Person.find(from_=relationship['relative']['apiUrl'], limit=MAX_PERSONS_LIMIT)
if len(child_result) > 0:
child.birthdate = str_to_date(child_result[0]['birthday'])
child.age = ' (' + str(__age(child_result[0]['birthday'])) + ')'
person['children'].append(child)
elif relationship['relationshipTypeId'] == 2: # Ehepartner
personHasSpouse = True
# Create family_id for sorting (last name, ID of husband & wife)
if person['sexId'] == 1: # Male
person['family_id'] = '{lastname}-{husband_name}-{wife_name}'.format(
lastname=person['lastName'],
husband_name=person['firstName'],
wife_name=str(relationship['relative']['domainAttributes']['firstName']))
else: # Female
person['family_id'] = '{lastname}-{husband_name}-{wife_name}'.format(
lastname=person['lastName'],
husband_name=str(relationship['relative']['domainAttributes']['firstName']),
wife_name=person['firstName'])
person['familyEnd'] = True
if not personHasSpouse:
person['familyEnd'] = True
# Sort children by age
person['children'].sort(reverse=True)
# All children in one line
person['allChildren'] = ', '.join(str(child) for child in person['children'])
# Sort persons by their family
persons_sorted = sorted(persons, key = lambda p: (p['family_id'], p['sexId']))
# Cache result if in debug mode
if DEBUG:
with open(filename, 'wb') as f:
pickle.dump(persons_sorted, f)
return persons_sorted
class Member:
personId = None
firstName = ''
lastName = ''
present = False # Whether the person was present in the meeting
def __hash__(self):
return hash(self.personId)
def __eq__(self, other):
return self.personId == other.personId
def __lt__(self, other):
return self.lastName + self.firstName < other.lastName + other.firstName
def __str__(self):
return "{lastName} {firstName}".format(firstName = self.firstName, lastName = self.lastName)
def get_group_meeting(group_id, meeting_date):
start_date_str = meeting_date.strftime("%Y-%m-%d")
# End date must be one day more than start date
end_date = meeting_date + datetime.timedelta(days=1)
end_date_str = end_date.strftime("%Y-%m-%d")
group_url = ApiBase._site + 'groups/{id}/meetings'.format(id=group_id)
meetings_in_group_result = Group.find(from_=group_url,
limit=1,
start_date=start_date_str, end_date=end_date_str)
meetings_in_group = meetings_in_group_result[0]['data']
return meetings_in_group[0] if meetings_in_group else None
def get_meeting_members(group_id, meeting_id, filter_role_id=None):
url = ApiBase._site + 'groups/{groupId}/meetings/{meetingId}/members'.format(groupId=group_id, meetingId=meeting_id)
members_result = Group.find(from_=url)
members = members_result[0]['data']
new_members = []
for member in members:
if filter_role_id and int(member['member']['groupTypeRoleId']) != int(filter_role_id):
continue
new_member = Member()
new_member.personId = member['member']['personId']
new_member.firstName = member['member']['person']['domainAttributes']['firstName']
new_member.lastName = member['member']['person']['domainAttributes']['lastName']
new_member.present = member['status'] == 'present'
new_members.append(new_member)
return new_members