Housekeeping: reorganize format fields, simplify the building tasks.
[wolnelektury.git] / src / club / civicrm.py
1 from datetime import datetime
2 import json
3 from celery import shared_task
4 from django.conf import settings
5 import requests
6 import yaml
7
8
9 class CiviCRM:
10     def __init__(self, base, key):
11         self.base = base
12         self.key = key
13         self.api_base = (base or '') + 'civicrm/ajax/api4/'
14         self.enabled = bool(self.base and self.key)
15
16     def request(self, resource, method, params):
17         if not self.enabled:
18             return
19
20         response = requests.post(
21             self.api_base + f'{resource}/{method}',
22             params={
23                 'params': json.dumps(params),
24                 'api_key': self.key
25             },
26         )
27         d = response.json()
28         return d
29
30     def create_or_update_contact(self, email, key):
31         contact_id = self.get_contact_id(email)
32         if contact_id is None:
33             contact_id = self.create_contact(email, key)
34         else:
35             self.update_contact(contact_id, key)
36         return contact_id
37
38     def get_contact_id(self, email):
39         result = self.request(
40             'Contact',
41             'get',
42             {
43                 "join": [["Email AS email", "LEFT"]],
44                 "where":[["email.email", "=", email]],
45                 "limit":1,
46                 "debug":True
47             }
48         )['values']
49         if result:
50             return result[0]['id']
51
52     def create_contact(self, email, key):
53         result = self.request(
54             'Contact',
55             'create',
56             {
57                 'values': {
58                     'WL.TPWL_key': key,
59                 },
60                 'chain': {
61                     'email': [
62                         'Email',
63                         'create',
64                         {
65                             'values': {
66                                 'email': email,
67                                 'contact_id': '$id'
68                             }
69                         }
70                     ]
71                 }
72             }
73         )
74         return result[0]['id']
75     
76     def update_contact(self, contact_id, key):
77         return self.request(
78             'Contact',
79             'update',
80             {
81                 'values': {
82                     'WL.TPWL_key': key,
83                 },
84                 'where': [
85                     ['id', '=', contact_id]
86                 ]
87             }
88         )
89                 
90
91     def report_activity(self, email, tpwl_key, key, name, datetime, details):
92         if not self.enabled:
93             return
94
95         contact_id = self.create_or_update_contact(email, tpwl_key)
96
97         activity_id = self.get_activity_id(key)
98         if activity_id is None:
99             self.create_activity(
100                 contact_id,
101                 key,
102                 name,
103                 datetime,
104                 details
105             )
106         else:
107             self.update_activity(
108                 activity_id,
109                 contact_id,
110                 name,
111                 datetime,
112                 details
113             )
114
115     def get_activity_id(self, key):
116         result = self.request(
117             'Activity',
118             'get',
119             {
120                 'where': [
121                     ['subject', '=', key],
122                 ]
123             }
124         )['values']
125         if result:
126             return result[0]['id']
127     
128     def create_activity(self, contact_id, key, name, date_time, details):
129         detail_str = yaml.dump(details)
130         return self.request(
131             'Activity',
132             'create',
133             {
134                 'values': {
135                     'source_contact_id': contact_id,
136                     'activity_type_id:name': name,
137                     'status_id:name': 'Completed',
138                     'activity_date_time': date_time.isoformat() if date_time else '',
139                     'details' : detail_str,
140                     'subject': key,
141                 },
142                 'debug': True,
143             }
144         )
145
146     def update_activity(self, activity_id, contact_id, name, date_time, details):
147         detail_str = yaml.dump(details)
148
149         self.request(
150             'Activity',
151             'update',
152             {
153                 'values': {
154                     'source_contact_id': contact_id,
155                     'activity_type_id:name': name,
156                     'status_id:name': 'Completed',
157                     'activity_date_time': date_time.isoformat(),
158                     'details' : detail_str,
159                 },
160                 'where': [
161                     ['id', '=', activity_id]
162                 ]
163             }
164         )
165     
166     #do we create a civicontribution?
167
168
169 civicrm = CiviCRM(
170     settings.CIVICRM_BASE,
171     settings.CIVICRM_KEY,
172 )
173
174 @shared_task(ignore_result=True)
175 def report_activity(*args, **kwargs):
176     civicrm.report_activity(*args, **kwargs)
177
178