25 entity['upassword'] = u'motdepasse' |
25 entity['upassword'] = u'motdepasse' |
26 ctl.check('login', entity['login'], None) |
26 ctl.check('login', entity['login'], None) |
27 ctl.store.add('CWUser', entity) |
27 ctl.store.add('CWUser', entity) |
28 email = {'address': row['email']} |
28 email = {'address': row['email']} |
29 ctl.store.add('EmailAddress', email) |
29 ctl.store.add('EmailAddress', email) |
30 ctl.store.relate(entity['uid'], 'use_email', email['uid']) |
30 ctl.store.relate(entity['eid'], 'use_email', email['eid']) |
31 ctl.store.rql('SET U in_group G WHERE G name "users", U eid %(x)s', {'x':entity['uid']}) |
31 ctl.store.rql('SET U in_group G WHERE G name "users", U eid %(x)s', {'x':entity['eid']}) |
32 |
32 |
33 CHK = [('login', check_doubles, 'Utilisateurs Login', |
33 CHK = [('login', check_doubles, 'Utilisateurs Login', |
34 'Deux utilisateurs ne devraient pas avoir le même login.'), |
34 'Deux utilisateurs ne devraient pas avoir le même login.'), |
35 ] |
35 ] |
36 |
36 |
133 >>> store = ObjectStore() |
133 >>> store = ObjectStore() |
134 >>> user = {'login': 'johndoe'} |
134 >>> user = {'login': 'johndoe'} |
135 >>> store.add('CWUser', user) |
135 >>> store.add('CWUser', user) |
136 >>> group = {'name': 'unknown'} |
136 >>> group = {'name': 'unknown'} |
137 >>> store.add('CWUser', group) |
137 >>> store.add('CWUser', group) |
138 >>> store.relate(user['uid'], 'in_group', group['uid']) |
138 >>> store.relate(user['eid'], 'in_group', group['eid']) |
139 """ |
139 """ |
140 |
140 |
141 def __init__(self): |
141 def __init__(self): |
142 self.items = [] |
142 self.items = [] |
143 self.uids = {} |
143 self.eids = {} |
144 self.types = {} |
144 self.types = {} |
145 self.relations = set() |
145 self.relations = set() |
146 self.indexes = {} |
146 self.indexes = {} |
147 self._rql = None |
147 self._rql = None |
148 self._checkpoint = None |
148 self._checkpoint = None |
151 self.items.append(item) |
151 self.items.append(item) |
152 return len(self.items) - 1 |
152 return len(self.items) - 1 |
153 |
153 |
154 def add(self, type, item): |
154 def add(self, type, item): |
155 assert isinstance(item, dict), item |
155 assert isinstance(item, dict), item |
156 uid = item['uid'] = self._put(type, item) |
156 eid = item['eid'] = self._put(type, item) |
157 self.uids[uid] = item |
157 self.eids[eid] = item |
158 self.types.setdefault(type, []).append(uid) |
158 self.types.setdefault(type, []).append(eid) |
159 |
159 |
160 def relate(self, uid_from, rtype, uid_to): |
160 def relate(self, eid_from, rtype, eid_to): |
161 uids_valid = (uid_from < len(self.items) and uid_to <= len(self.items)) |
161 eids_valid = (eid_from < len(self.items) and eid_to <= len(self.items)) |
162 assert uids_valid, 'uid error %s %s' % (uid_from, uid_to) |
162 assert eids_valid, 'eid error %s %s' % (eid_from, eid_to) |
163 self.relations.add( (uid_from, rtype, uid_to) ) |
163 self.relations.add( (eid_from, rtype, eid_to) ) |
164 |
164 |
165 def build_index(self, name, type, func): |
165 def build_index(self, name, type, func): |
166 index = {} |
166 index = {} |
167 for uid in self.types[type]: |
167 for eid in self.types[type]: |
168 index.setdefault(func(self.uids[uid]), []).append(uid) |
168 index.setdefault(func(self.eids[eid]), []).append(eid) |
169 self.indexes[name] = index |
169 self.indexes[name] = index |
170 |
170 |
171 def get_many(self, name, key): |
171 def get_many(self, name, key): |
172 return self.indexes[name].get(key, []) |
172 return self.indexes[name].get(key, []) |
173 |
173 |
174 def get_one(self, name, key): |
174 def get_one(self, name, key): |
175 uids = self.indexes[name].get(key, []) |
175 eids = self.indexes[name].get(key, []) |
176 assert len(uids) == 1 |
176 assert len(eids) == 1 |
177 return uids[0] |
177 return eids[0] |
178 |
178 |
179 def find(self, type, key, value): |
179 def find(self, type, key, value): |
180 for idx in self.types[type]: |
180 for idx in self.types[type]: |
181 item = self.items[idx] |
181 item = self.items[idx] |
182 if item[key] == value: |
182 if item[key] == value: |
195 |
195 |
196 def _put(self, type, item): |
196 def _put(self, type, item): |
197 query = ('INSERT %s X: ' % type) + ', '.join(['X %s %%(%s)s' % (key,key) for key in item]) |
197 query = ('INSERT %s X: ' % type) + ', '.join(['X %s %%(%s)s' % (key,key) for key in item]) |
198 return self.rql(query, item)[0][0] |
198 return self.rql(query, item)[0][0] |
199 |
199 |
200 def relate(self, uid_from, rtype, uid_to): |
200 def relate(self, eid_from, rtype, eid_to): |
201 query = 'SET X %s Y WHERE X eid %%(from)s, Y eid %%(to)s' % rtype |
201 query = 'SET X %s Y WHERE X eid %%(from)s, Y eid %%(to)s' % rtype |
202 self.rql(query, {'from': int(uid_from), 'to': int(uid_to)}) |
202 self.rql(query, {'from': int(eid_from), 'to': int(eid_to)}) |
203 self.relations.add( (uid_from, rtype, uid_to) ) |
203 self.relations.add( (eid_from, rtype, eid_to) ) |
204 |
204 |
205 # import controller ##### |
205 # import controller ##### |
206 |
206 |
207 class CWImportController(object): |
207 class CWImportController(object): |
208 """Controller of the data import process. |
208 """Controller of the data import process. |
253 if err: |
253 if err: |
254 self.errors[title] = (help, err) |
254 self.errors[title] = (help, err) |
255 self.store.checkpoint() |
255 self.store.checkpoint() |
256 errors = sum(len(err[1]) for err in self.errors.values()) |
256 errors = sum(len(err[1]) for err in self.errors.values()) |
257 self.tell('Importation terminée. (%i objets, %i types, %i relations et %i erreurs).' |
257 self.tell('Importation terminée. (%i objets, %i types, %i relations et %i erreurs).' |
258 % (len(self.store.uids), len(self.store.types), |
258 % (len(self.store.eids), len(self.store.types), |
259 len(self.store.relations), errors)) |
259 len(self.store.relations), errors)) |
260 if self.errors and self.askerror and confirm('Afficher les erreurs ?'): |
260 if self.errors and self.askerror and confirm('Afficher les erreurs ?'): |
261 import pprint |
261 import pprint |
262 pprint.pprint(self.errors) |
262 pprint.pprint(self.errors) |
263 |
263 |