-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenConceptLab/ocl_issues#412 | scripts to import v1 users and orgs
- Loading branch information
1 parent
a7c621a
commit 487ab29
Showing
15 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import json | ||
|
||
from django.core.management import BaseCommand | ||
from pydash import get | ||
|
||
from core.common.tasks import populate_indexes | ||
from core.orgs.models import Organization | ||
from pprint import pprint | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'import v1 orgs' | ||
|
||
total = 0 | ||
processed = 0 | ||
created = [] | ||
existed = [] | ||
failed = [] | ||
|
||
@staticmethod | ||
def log(msg): | ||
print("*******{}*******".format(msg)) | ||
|
||
def handle(self, *args, **options): | ||
FILE_PATH = '/code/core/importers/v1_dump/data/exported_orgs.json' | ||
lines = open(FILE_PATH, 'r').readlines() | ||
|
||
self.log('STARTING ORGS IMPORT') | ||
self.total = len(lines) | ||
self.log('TOTAL: {}'.format(self.total)) | ||
|
||
for line in lines: | ||
data = json.loads(line) | ||
original_data = data | ||
self.processed += 1 | ||
_id = data.pop('_id') | ||
created_at = data.pop('created_at') | ||
updated_at = data.pop('updated_at') | ||
data['internal_reference_id'] = get(_id, '$oid') | ||
data['created_at'] = get(created_at, '$date') | ||
data['updated_at'] = get(updated_at, '$date') | ||
mnemonic = data.get('mnemonic') | ||
self.log("Processing: {} ({}/{})".format(mnemonic, self.processed, self.total)) | ||
if Organization.objects.filter(mnemonic=mnemonic).exists(): | ||
self.existed.append(original_data) | ||
else: | ||
org = Organization.objects.create(**data) | ||
if org: | ||
self.created.append(original_data) | ||
else: | ||
self.failed.append(original_data) | ||
|
||
populate_indexes.delay(['orgs']) | ||
|
||
self.log( | ||
"Result: Created: {} | Existed: {} | Failed: {}".format( | ||
len(self.created), len(self.existed), len(self.failed) | ||
) | ||
) | ||
if self.existed: | ||
self.log("Existed") | ||
pprint(self.existed) | ||
|
||
if self.failed: | ||
self.log("Failed") | ||
pprint(self.failed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import json | ||
from pprint import pprint | ||
|
||
from django.core.management import BaseCommand | ||
from pydash import get | ||
|
||
from core.common.tasks import populate_indexes | ||
from core.orgs.models import Organization | ||
from core.users.models import UserProfile | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'import v1 users' | ||
|
||
total = 0 | ||
processed = 0 | ||
created = [] | ||
existed = [] | ||
updated = [] | ||
failed = [] | ||
|
||
@staticmethod | ||
def log(msg): | ||
print("*******{}*******".format(msg)) | ||
|
||
def handle(self, *args, **options): | ||
FILE_PATH = '/code/core/importers/v1_dump/data/exported_users.json' | ||
lines = open(FILE_PATH, 'r').readlines() | ||
|
||
self.log('STARTING USERS IMPORT') | ||
self.total = len(lines) | ||
self.log('TOTAL: {}'.format(self.total)) | ||
|
||
for line in lines: | ||
data = json.loads(line) | ||
original_data = data | ||
self.processed += 1 | ||
data.pop('_id') | ||
_id = data.pop('user_id') | ||
# created_at = data.pop('created_at') | ||
# updated_at = data.pop('updated_at') | ||
last_login = data.pop('last_login') | ||
date_joined = data.pop('date_joined') | ||
full_name = data.pop('full_name') or '' | ||
name_parts = list(set(full_name.split(' '))) | ||
first_name = data.pop('first_name', '') or ' '.join(name_parts[:-1]) | ||
last_name = data.pop('last_name', '') or name_parts[-1] | ||
orgs = data.pop('organizations', []) | ||
password = data.pop('password') | ||
hashed_password = data.pop('hashed_password') | ||
password = password or hashed_password | ||
data['verified'] = data.pop('verified_email', True) | ||
|
||
data['last_name'] = last_name | ||
data['internal_reference_id'] = get(_id, '$oid') | ||
# data['created_at'] = get(created_at, '$date') | ||
# data['updated_at'] = get(updated_at, '$date') | ||
data['date_joined'] = get(date_joined, '$date') | ||
data['last_login'] = get(last_login, '$date') | ||
data['first_name'] = first_name | ||
username = data.get('username') | ||
self.log("Processing: {} ({}/{})".format(username, self.processed, self.total)) | ||
queryset = UserProfile.objects.filter(username=username) | ||
if queryset.exists(): | ||
self.updated.append(original_data) | ||
user = queryset.first() | ||
user.organizations.set(Organization.objects.filter(internal_reference_id__in=orgs)) | ||
else: | ||
try: | ||
user = UserProfile.objects.create(**data) | ||
if user: | ||
self.created.append(original_data) | ||
user.password = password | ||
user.save() | ||
user.organizations.set(Organization.objects.filter(internal_reference_id__in=orgs)) | ||
else: | ||
self.failed.append(original_data) | ||
except Exception as ex: | ||
self.failed.append({**original_data, 'errors': ex.args}) | ||
populate_indexes.delay(['users', 'orgs']) | ||
self.log( | ||
"Result: Created: {} | Updated: {} | Failed: {}".format( | ||
len(self.created), len(self.updated), len(self.failed), | ||
) | ||
) | ||
if self.existed: | ||
self.log("Updated") | ||
pprint(self.updated) | ||
|
||
if self.failed: | ||
self.log("Failed") | ||
pprint(self.failed) |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
mongoexport --db ocl --collection orgs_organization --fields=_id,mnemonic,website,created_at,updated_at,is_active,uri,extras,location,public_access,company,name -o ../data/exported_orgs.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
|
||
mongo "localhost:27017/ocl" ./export_user.js | ||
mongoexport --db ocl --collection export.results -o ../data/exported_users.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
db.export.results.drop(); | ||
|
||
cursor = db.auth_user.find(); | ||
|
||
while (cursor.hasNext()) { | ||
auth_user = cursor.next(); | ||
profiles = db.users_userprofile.find({"user_id": auth_user._id}, {company: 1, location: 1, _id: 0, verified_email: 1, user_id: 1, hashed_password: 1, full_name: 1, preferred_locale: 1, organizations: 1, extras: 1, public_access: 1, verified_email: 1}); | ||
if (profiles.hasNext()) { | ||
user_profile = profiles.next(); | ||
db.export.results.insert(Object.assign({}, auth_user, user_profile)); | ||
} | ||
} | ||
|
||
print(db.export.results.count() + " matching documents found"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 3.0.9 on 2021-03-04 04:13 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0011_userprofile_verification_token'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='userprofile', | ||
name='first_name', | ||
field=models.CharField(blank=True, max_length=100), | ||
), | ||
migrations.AlterField( | ||
model_name='userprofile', | ||
name='last_name', | ||
field=models.CharField(blank=True, max_length=100), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters