Skip to content

Commit

Permalink
Migration to change existing structure of social data
Browse files Browse the repository at this point in the history
  • Loading branch information
erinspace committed Jul 26, 2018
1 parent 694466c commit 1ec8719
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions osf/migrations/0112_update_social_data_format.py
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)
]

0 comments on commit 1ec8719

Please sign in to comment.