From 1ec87199b060767f2ae45fc746d135faa799c8af Mon Sep 17 00:00:00 2001 From: erinspace Date: Thu, 26 Jul 2018 12:00:47 -0400 Subject: [PATCH] Migration to change existing structure of social data --- .../0112_update_social_data_format.py | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 osf/migrations/0112_update_social_data_format.py diff --git a/osf/migrations/0112_update_social_data_format.py b/osf/migrations/0112_update_social_data_format.py new file mode 100644 index 000000000000..8294cf306f1a --- /dev/null +++ b/osf/migrations/0112_update_social_data_format.py @@ -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) + ]