-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcapsule_crm.py
110 lines (87 loc) · 3.38 KB
/
capsule_crm.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
import os
import sys
import django
sys.path.append("/dspexplorer/")
os.environ['DJANGO_SETTINGS_MODULE'] = 'dspexplorer.settings'
django.setup()
from dashboard.models import User, Profile
import json
from crmconnector.models import Party
from utils.Colorizer import Colorizer
from crmconnector.capsule import CRMValidationException
def update_crm(user=None):
errored = []
partially_updated = []
party = None
users = user or User.objects.all()
for user in users:
try:
print ('--------------------')
print ('UPDATING USER : %s' % user)
print ('--------------------')
print (' ')
party = Party(user)
party.create_or_update()
print Colorizer.Green('UPDATED %s' % user)
print (' ')
except Profile.DoesNotExist as e:
print Colorizer.custom('[ERROR USER MALFORMED] : %s ' % e, 'white', 'purple')
print (' ')
except CRMValidationException as e:
try:
print Colorizer.Red('Try to exclude incompatible custom fields for user: %s' % user)
party.safe_create_or_update()
partially_updated.append(user.email)
print Colorizer.Yellow('UPDATED partially: %s' % user)
print (' ')
except Exception as safe_exc:
print Colorizer.Red('[ ERROR IN SAFE UPDATE ] : %s' % safe_exc)
print json.dumps(party.as_dict(), indent=1)
print (' ')
print Colorizer.Red('ERROR UPDATING USER : %s' % user)
print ('ERROR: %s' % e)
print (' ')
errored.append(user.email)
except Exception as e:
print Colorizer.Red('ERROR UPDATING USER : %s' % user)
print ('ERROR: %s' % e)
print (' ')
return errored, partially_updated
def print_results(errored=None, partially_updated=None):
# PRINT RESULTS
print '-------------'
print 'TOTAL RESULTS'
print '-------------'
if len(errored):
print Colorizer.Red('%s errored users' % len(errored))
# logger.error('ERROR updating users : %s' % errored)
print errored
if len(partially_updated):
print Colorizer.Purple('%s partially updated users : ' % len(partially_updated))
print(partially_updated)
elif not len(errored) and not len(partially_updated):
print Colorizer.Green('No errored users')
print '-------------'
def get_test_user(user_email):
user = User.objects.filter(email=user_email)
return user if len(user) > 0 else User.objects.filter(email='massimo.santoli@top-ix.org')
def pair_crm_ids():
users = User.objects.all()
for u in users:
try:
party = Party(u)
party_crm_id = party.get()['id']
profile = Profile.get_by_email(u)
profile.set_crm_id(party_crm_id)
except Exception as e:
print 'PAIR CRM IDs %s' % u
print 'PAIR CRM IDs %s' % e
sys.exit("Paring finished")
if __name__ == "__main__":
pair_crm_ids() if len(sys.argv) > 1 and sys.argv[1] == 'pair_crm_ids' else None
user = get_test_user(sys.argv[1]) if len(sys.argv) > 1 else None
if len(user) > 0:
errored, partially_updated = update_crm(user)
print_results(errored, partially_updated)
else:
print Colorizer.Red('No user found')