-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration to change existing structure of social data
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.13 on 2018-07-25 20:39 | ||
from __future__ import unicode_literals | ||
|
||
import logging | ||
import progressbar | ||
|
||
from django.db import migrations | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
SOCIAL_LIST_FIELDS = [ | ||
'github', | ||
'profileWebsites', | ||
'linkedIn', | ||
'twitter' | ||
] | ||
|
||
SOCIAL_STRING_FIELDS = [ | ||
'orcid', | ||
'researcherID', | ||
'impactStory' | ||
'scholar', | ||
'ssrn', | ||
'researchGate', | ||
'baiduScholar', | ||
'academiaProfileID' | ||
] | ||
|
||
|
||
def update_social_fields(state, schema): | ||
OSFUser = state.get_model('osf', 'osfuser') | ||
# If a user has no social info, it is stored as an empty dict | ||
users_with_social = OSFUser.objects.exclude(social={}) | ||
users_to_update = users_with_social.count() | ||
logger.info('Updating social fields for {} users'.format(users_to_update)) | ||
progress_bar = progressbar.ProgressBar(maxval=users_to_update).start() | ||
|
||
users_updated = 0 | ||
for user in users_with_social: | ||
new_social = {} | ||
for key, value in user.social.iteritems(): | ||
if key in SOCIAL_LIST_FIELDS: | ||
if isinstance(value, list): | ||
new_social[key] = value | ||
else: | ||
new_social[key] = [value] | ||
else: | ||
new_social[key] = value | ||
user.social = new_social | ||
user.save() | ||
users_updated += 1 | ||
progress_bar.update(users_updated) | ||
|
||
progress_bar.finish() | ||
logger.info('Finished updating social fields for {} users'.format(users_updated)) | ||
|
||
|
||
def reset_social_fields(state, schema): | ||
OSFUser = state.get_model('osf', 'osfuser') | ||
users_with_social = OSFUser.objects.exclude(social={}) | ||
users_to_update = users_with_social.count() | ||
logger.info('Updating social fields for {} users'.format(users_to_update)) | ||
progress_bar = progressbar.ProgressBar(maxval=users_to_update).start() | ||
|
||
users_updated = 0 | ||
for user in users_with_social: | ||
old_social = {} | ||
for key, value in user.social.iteritems(): | ||
if key in SOCIAL_LIST_FIELDS and key != 'profileWebsites': | ||
old_social[key] = value[0] | ||
else: | ||
old_social[key] = value | ||
user.social = old_social | ||
user.save() | ||
users_updated += 1 | ||
progress_bar.update(users_updated) | ||
|
||
progress_bar.finish() | ||
logger.info('Updated social field for {} users'.format(users_updated)) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('osf', '0111_auto_20180605_1240'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_social_fields, reset_social_fields) | ||
] |