Switch to unified view

a/scripts/teamforge-import.py b/scripts/teamforge-import.py
...
...
4
from pylons import c
4
from pylons import c
5
import re
5
import re
6
import os
6
import os
7
import os.path
7
import os.path
8
from time import mktime
8
from time import mktime
9
import time
9
import json
10
import json
10
from urlparse import urlparse
11
from urlparse import urlparse
11
from urllib import FancyURLopener
12
from urllib import FancyURLopener
12
from datetime import datetime
13
from datetime import datetime
13
from ConfigParser import ConfigParser
14
from ConfigParser import ConfigParser
15
import random
16
import string
14
17
18
import sqlalchemy
15
from suds.client import Client
19
from suds.client import Client
16
from suds import WebFault
20
from suds import WebFault
17
from ming.orm.ormsession import ThreadLocalORMSession
21
from ming.orm.ormsession import ThreadLocalORMSession
18
from ming.base import Object
22
from ming.base import Object
19
23
...
...
130
            try:
134
            try:
131
                project = create_project(pid, nbhd)
135
                project = create_project(pid, nbhd)
132
            except:
136
            except:
133
                log.exception('Error creating %s' % pid)
137
                log.exception('Error creating %s' % pid)
134
138
135
def save_user(usernames):
139
def load_users():
136
    if isinstance(usernames, basestring):
140
    ''' load the users data from file, if it hasn't been already '''
137
        usernames = [usernames]
138
139
    # load data from last runs
140
    global users
141
    global users
141
    user_filename = os.path.join(options.output_dir, 'users.json')
142
    user_filename = os.path.join(options.output_dir, 'users.json')
142
    if not users and os.path.exists(user_filename):
143
    if not users and os.path.exists(user_filename):
143
        with open(user_filename) as user_file:
144
        with open(user_filename) as user_file:
144
            users = json.load(user_file)
145
            users = json.load(user_file, object_hook=Object) # Object for attribute access
146
147
148
def save_user(usernames):
149
    if isinstance(usernames, basestring):
150
        usernames = [usernames]
151
152
    load_users()
145
153
146
    for username in usernames:
154
    for username in usernames:
147
        if username not in users:
155
        if username not in users:
148
            user_data = client.service.getUserData(s, username)
156
            user_data = client.service.getUserData(s, username)
149
            users[username] = Object(user_data)
157
            users[username] = Object(user_data)
...
...
203
        david_peyer = 'david-mmi',
211
        david_peyer = 'david-mmi',
204
        okruse = 'ottokruse',
212
        okruse = 'ottokruse',
205
        jvp = 'jvpmoto',
213
        jvp = 'jvpmoto',
206
        dmorelli = 'dmorelli',
214
        dmorelli = 'dmorelli',
207
    ).get(sf_username, sf_username + '-mmi')
215
    ).get(sf_username, sf_username + '-mmi')
216
    if len(sf_username) > 15:
217
        adjusted_username = sf_username[0:15-4] + '-mmi'
218
        log.error('invalid sf_username length: %s   Changing it to %s' % (sf_username, adjusted_username))
219
        sf_username = adjusted_username
208
220
209
    u = M.User.by_username(sf_username)
221
    u = M.User.by_username(sf_username)
210
222
211
    if not u:
223
    if not u:
212
        # FIXME: temporary:
224
        load_users()
213
        import random
214
        bogus = 'user%02d' % random.randrange(1,20)
215
        try:
216
            u = M.User.by_username(bogus)
217
        except:
218
            # try again
219
            return get_user(orig_username)
220
        """
221
        user = users[orig_username]
225
        user = users[orig_username]
222
        if user.status != 'Active':
226
        if user.status != 'Active':
223
            log.warn('Inactive user %s %s' % (orig_username, user.status))
227
            log.warn('Inactive user %s %s' % (orig_username, user.status))
228
229
        if not 3 <= len(user.fullName) <= 32:
230
            raise Exception('invalid fullName length: %s' % user.fullName)
231
        if '@' not in user.email:
232
            raise Exception('invalid email: %s' % user.email)
224
        # FIXME: hardcoded SFX integration
233
        # FIXME: hardcoded SFX integration
225
        from sfx.model import tables as T
234
        from sfx.model import tables as T
226
        nu = T.users.insert()
235
        nu = T.users.insert()
227
        nu.execute(user_name=sf_username,
236
        nu.execute(user_name=sf_username,
228
                   email=user.email,
237
                   email=user.email.lower(),
229
                   realname=user.fullName,
238
                   realname=user.fullName,
239
                   status='A' if user.status == 'Active' else 'D',
240
                   language=275, # english trove id
241
                   timezone=user.timeZone,
242
                   user_pw=''.join(random.sample(string.printable, 32)),
243
                   unix_pw=''.join(random.sample(string.printable, 32)),
244
                   user_pw_modtime=int(time.time()),
245
                   mail_siteupdates=0,
246
                   add_date=int(time.time()),
230
                   status='A')
247
                   )
248
        user_id = sqlalchemy.select([T.users.c.user_id], T.users.c.user_name==sf_username).execute().fetchone().user_id
249
        npref = T.user_preferences.insert()
250
        npref.execute(user_id=user_id, preference_name='country', preference_value='US')
251
        npref.execute(user_id=user_id, preference_name='opt_research', preference_value=0)
252
        npref.execute(user_id=user_id, preference_name='opt_thirdparty', preference_value=0)
253
254
        new_audit = T.audit_trail_user.insert()
255
        new_audit.execute(
256
            date=int(time.time()),
257
            username='nobody',
258
            ip_address='(imported)',
259
            operation_resource=user_id,
260
            operation='%s user account created by TeamForge import script' % user.status,
261
            operation_target='',
231
        """
262
        )
263
264
        u = M.User.by_username(sf_username)
232
    assert u
265
    assert u
233
    return u
266
    return u
234
267
235
def convert_project_shortname(teamforge_path):
268
def convert_project_shortname(teamforge_path):
236
    'convert from TeamForge to SF, and validate early'
269
    'convert from TeamForge to SF, and validate early'