def user_import(row, current_user, is_merge, users_map): existent = is_merge and verify_existence(row) or None roles = row.get('roles').get_listc('role') valid_email = row.get('email').get_attr('validated').as_bool() badges = row.get('badges') if existent: user = existent user.reputation += row.get('reputation').as_int() user.gold += badges.get_attr('gold').as_int() user.silver += badges.get_attr('gold').as_int() user.bronze += badges.get_attr('gold').as_int() else: username = row.getc('username') if is_merge: username_count = 0 while orm.User.objects.filter(username=username).count(): username_count += 1 username = "******" % (row.getc('username'), username_count) user = orm.User( id=(not is_merge) and row.getc('id') or None, username=username, password=row.getc('password'), email=row.getc('email'), email_isvalid=valid_email, is_superuser=(not is_merge) and 'superuser' in roles, is_staff=('moderator' in roles) or (is_merge and 'superuser' in roles), is_active=row.get('active').as_bool(), date_joined=row.get('joindate').as_datetime(), about=row.getc('bio'), date_of_birth=row.get('birthdate').as_date(None), website=row.getc('website'), reputation=row.get('reputation').as_int(), gold=badges.get_attr('gold').as_int(), silver=badges.get_attr('silver').as_int(), bronze=badges.get_attr('bronze').as_int(), real_name=row.getc('realname'), location=row.getc('location'), ) user.save() users_map[row.get('id').as_int()] = user.id authKeys = row.get('authKeys') for key in authKeys.get_list('key'): if (not is_merge) or orm.AuthKeyUserAssociation.objects.filter( key=key.getc('key')).count() == 0: orm.AuthKeyUserAssociation(user=user, key=key.getc('key'), provider=key.getc('provider')).save() if not existent: notifications = row.get('notifications') attributes = dict([ (str(k), v.as_bool() and 'i' or 'n') for k, v in notifications.get('notify').attrs.items() ]) attributes.update( dict([(str(k), v.as_bool()) for k, v in notifications.get('autoSubscribe').attrs.items() ])) attributes.update( dict([(str("notify_%s" % k), v.as_bool()) for k, v in notifications.get('notifyOnSubscribed').attrs.items()])) ss = orm.SubscriptionSettings( user=user, enable_notifications=notifications.get_attr('enabled').as_bool(), **attributes) if current_user.id == row.get('id').as_int(): ss.id = current_user.subscription_settings.id ss.save()
def callback(sxu): create = True if sxu.get('id') == '-1': return #print "\n".join(["%s : %s" % i for i in sxu.items()]) if int(sxu.get('id')) == int(owneruid): osqau = orm.User.objects.get(id=1) for assoc in orm.AuthKeyUserAssociation.objects.filter(user=osqau): openids.add(assoc.key) uidmapper[owneruid] = 1 uidmapper[-1] = 1 create = False else: username = unicode(sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu)))))[:30] if username in usernames: #if options.get('mergesimilar', False) and sxu.get('email', 'INVALID') == user_by_name[username].email: # osqau = user_by_name[username] # create = False # uidmapper[sxu.get('id')] = osqau.id #else: inc = 0 while True: inc += 1 totest = "%s %d" % (username[:29 - len(str(inc))], inc) if not totest in usernames: username = totest break sxbadges = sxu.get('badgesummary', None) badges = {'1':'0', '2':'0', '3':'0'} if sxbadges: badges.update(dict([b.split('=') for b in sxbadges.split()])) if create: osqau = orm.User( id = sxu.get('id'), username = username, password = '******', email = sxu.get('email', ''), is_superuser = sxu.get('usertypeid') == '5', is_staff = sxu.get('usertypeid') == '4', is_active = True, date_joined = readTime(sxu.get('creationdate')), last_seen = readTime(sxu.get('lastaccessdate')), about = sxu.get('aboutme', ''), date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None, email_isvalid = int(sxu.get('usertypeid')) > 2, website = sxu.get('websiteurl', ''), reputation = int(sxu.get('reputation')), gold = int(badges['1']), silver = int(badges['2']), bronze = int(badges['3']), real_name = sxu.get('realname', '')[:30], location = sxu.get('location', ''), ) osqau.save() user_joins = orm.Action( action_type = "userjoins", action_date = osqau.date_joined, user = osqau ) user_joins.save() rep = orm.ActionRepute( value = 1, user = osqau, date = osqau.date_joined, action = user_joins ) rep.save() try: orm.SubscriptionSettings.objects.get(user=osqau) except: s = orm.SubscriptionSettings(user=osqau) s.save() uidmapper[osqau.id] = osqau.id else: new_about = sxu.get('aboutme', None) if new_about and osqau.about != new_about: if osqau.about: osqau.about = "%s\n|\n%s" % (osqau.about, new_about) else: osqau.about = new_about osqau.username = sxu.get('displayname', sxu.get('displaynamecleaned', sxu.get('realname', final_username_attempt(sxu)))) osqau.email = sxu.get('email', '') osqau.reputation += int(sxu.get('reputation')) osqau.gold += int(badges['1']) osqau.silver += int(badges['2']) osqau.bronze += int(badges['3']) osqau.date_joined = readTime(sxu.get('creationdate')) osqau.website = sxu.get('websiteurl', '') osqau.date_of_birth = sxu.get('birthday', None) and readTime(sxu['birthday']) or None osqau.location = sxu.get('location', '') osqau.real_name = sxu.get('realname', '') #merged_users.append(osqau.id) osqau.save() usernames.append(osqau.username) openid = sxu.get('openid', None) if openid and openidre.match(openid) and (not openid in openids): assoc = orm.AuthKeyUserAssociation(user=osqau, key=openid, provider="openidurl") assoc.save() openids.add(openid) openidalt = sxu.get('openidalt', None) if openidalt and openidre.match(openidalt) and (not openidalt in openids): assoc = orm.AuthKeyUserAssociation(user=osqau, key=openidalt, provider="openidurl") assoc.save() openids.add(openidalt)