-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregistered_services.py
More file actions
99 lines (83 loc) · 3.36 KB
/
Copy pathregistered_services.py
File metadata and controls
99 lines (83 loc) · 3.36 KB
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
#
# registered_services.py
#
# script to be invoked via
#
# cd <your-buildout-root-dir>
# bin/instance run src/pcp.cregsync/src/pcp/cregsync/registered_services.py
#
# run with --help to see available options
import logging
from Products.PlonePAS.utils import cleanId
from pcp.cregsync import utils
from pcp.cregsync import config
def prepareid(id):
return cleanId(id)
def preparedata(values, site, additional_org, email2puid):
logger = logging.getLogger('cregsync.regservcedata')
fields = {}
for k,v in values.items():
key = k.lower()
fields[key] = v
title = fields['name'].encode('utf8').replace('_',' ')
additional = []
additional.append({'key': 'creg_id',
'value': str(fields['id']),
},
)
additional.append({'key': 'email',
'value': str(fields['email']),
},
)
del fields['id']
fields['title'] = title
fields['additional'] = utils.extend(additional_org, additional)
email = config.creg2dp_email.get(fields['email'].lower(), fields['email'])
contact_uid = email2puid.get(email.lower(), None)
# if contact_uid is None:
# contact_uid = utils.fixContact(site, fields, contact_type='support')
if contact_uid is None:
logger.warning("No contact with email address '%s' found." % fields['email'].lower())
else:
fields['contact'] = contact_uid
return fields.copy()
def main(app):
argparser = utils.getArgParser()
logger = utils.getLogger('var/log/cregsync_registered_services.log')
args = argparser.parse_args()
logger.info("'registered_services.py' called with '%s'" % args)
site = utils.getSite(app, args.site_id, args.admin_id)
logger.info("Got site '%s' as '%s'" % (args.site_id, args.admin_id))
targetfolder = site.operations
creg_services = utils.getData(args.path, args.filename) # returns a csv.DictReader instance
email2puid = utils.email2puid(site)
logger.info("Iterating over the registered services data")
for entry in creg_services:
id = prepareid(entry['NAME'])
if id is None:
logger.warning("Couldn't generate id for ", values)
continue
if id not in targetfolder.objectIds():
targetfolder.invokeFactory('RegisteredService', id)
logger.info("Added %s to the operations folder" % id)
# retrieve data to extended rather than overwritten
additional = targetfolder[id].getAdditional()
data = preparedata(entry, site, additional, email2puid)
logger.debug(data)
targetfolder[id].edit(**data)
targetfolder[id].reindexObject()
site.portal_repository.save(obj=targetfolder[id],
comment="Synchronization from Central Registry")
logger.info("Updated %s in the operations folder" % id)
if not args.dry:
logger.info("Committing changes to database")
import transaction
transaction.commit()
else:
logger.info("dry run; not committing anything")
logger.info("Done")
# As this script lives in your source tree, we need to use this trick so that
# five.grok, which scans all modules, does not try to execute the script while
# modules are being loaded on start-up
if "app" in locals():
main(app)