Experiments page.
[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=None):
31         contact_id = self.get_contact_id(email)
32         if contact_id is None:
33             contact_id = self.create_contact(email, key)
34         elif key:
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=None):
53         data = {
54             'values': {},
55             'chain': {
56                 'email': [
57                     'Email',
58                     'create',
59                     {
60                         'values': {
61                             'email': email,
62                             'contact_id': '$id'
63                         }
64                     }
65                 ]
66             }
67         }
68         if key:
69             data['values']['WL.TPWL_key'] =  key
70         result = self.request('Contact', 'create', data)
71         return result['values'][0]['id']
72     
73     def update_contact(self, contact_id, key):
74         return self.request(
75             'Contact',
76             'update',
77             {
78                 'values': {
79                     'WL.TPWL_key': key,
80                 },
81                 'where': [
82                     ['id', '=', contact_id]
83                 ]
84             }
85         )
86                 
87
88     def report_activity(self, email, tpwl_key, key, name, datetime, details):
89         if not self.enabled:
90             return
91
92         contact_id = self.create_or_update_contact(email, tpwl_key)
93
94         activity_id = self.get_activity_id(key)
95         if activity_id is None:
96             self.create_activity(
97                 contact_id,
98                 key,
99                 name,
100                 datetime,
101                 details
102             )
103         else:
104             self.update_activity(
105                 activity_id,
106                 contact_id,
107                 name,
108                 datetime,
109                 details
110             )
111
112     def get_activity_id(self, key):
113         result = self.request(
114             'Activity',
115             'get',
116             {
117                 'where': [
118                     ['subject', '=', key],
119                 ]
120             }
121         )['values']
122         if result:
123             return result[0]['id']
124     
125     def create_activity(self, contact_id, key, name, date_time, details):
126         detail_str = yaml.dump(details)
127         return self.request(
128             'Activity',
129             'create',
130             {
131                 'values': {
132                     'source_contact_id': contact_id,
133                     'activity_type_id:name': name,
134                     'status_id:name': 'Completed',
135                     'activity_date_time': date_time.isoformat() if date_time else '',
136                     'details' : detail_str,
137                     'subject': key,
138                 },
139                 'debug': True,
140             }
141         )
142
143     def update_activity(self, activity_id, contact_id, name, date_time, details):
144         detail_str = yaml.dump(details)
145
146         self.request(
147             'Activity',
148             'update',
149             {
150                 'values': {
151                     'source_contact_id': contact_id,
152                     'activity_type_id:name': name,
153                     'status_id:name': 'Completed',
154                     'activity_date_time': date_time.isoformat(),
155                     'details' : detail_str,
156                 },
157                 'where': [
158                     ['id', '=', activity_id]
159                 ]
160             }
161         )
162     
163     def add_email_to_group(self, email, group_id):
164         contact_id = self.create_or_update_contact(email)
165         self.add_contact_to_group(contact_id, group_id)
166
167     def add_contact_to_group(self, contact_id, group_id):
168         self.request(
169             'GroupContact',
170             'create',
171             {
172                 "values": {
173                     "group_id": group_id,
174                     "contact_id": contact_id,
175                 }
176             }
177         )
178
179
180 civicrm = CiviCRM(
181     settings.CIVICRM_BASE,
182     settings.CIVICRM_KEY,
183 )
184
185 @shared_task(ignore_result=True)
186 def report_activity(*args, **kwargs):
187     civicrm.report_activity(*args, **kwargs)
188
189