@@ -0,0 +1,2824 @@
+%22%22%22%0ASmall migration - Prereg challenge schema q5 mult choice responses have extra trailing space.  This gets confusing when%0Auser trying to update prereg draft via API.%0A%22%22%22%0A%0Aimport sys%0Aimport logging%0A%0Afrom modularodm import Q%0Afrom website.app import init_app%0Afrom scripts import utils as scripts_utils%0Afrom website.models import DraftRegistration, Node, MetaSchema%0Afrom framework.transactions.context import TokuTransaction%0A%0A%0Alogger = logging.getLogger(__name__)%0Alogger.setLevel(logging.INFO)%0A%0Adef migrate_drafts_q5_metadata(schema):%0A    %22%22%22%0A    Finds Prereg Challenge draft registrations and corrects q5 response metadata%0A    %22%22%22%0A    drafts = DraftRegistration.find(Q('registration_schema', 'eq', schema))%0A    total_drafts = drafts.count()%0A    logger.info('Examining %7B%7D drafts for q5 metadata'.format(total_drafts))%0A    draft_count = 0%0A    for draft in drafts:%0A        draft_count += 1%0A        if draft.registration_metadata.get('q5', %7B%7D).get('value', %7B%7D):%0A            draft.registration_metadata%5B'q5'%5D%5B'value'%5D = draft.registration_metadata%5B'q5'%5D%5B'value'%5D.rstrip()%0A            draft.save()%0A            logger.info('%7B%7D/%7B%7D Migrated q5 response for %7B%7D'.format(draft_count, total_drafts, draft._id))%0A        else:%0A            logger.info('%7B%7D/%7B%7D q5 not answered. No change needed for %7B%7D.'.format(draft_count, drafts.count(), draft._id))%0A%0Adef migrate_registrations_q5_metadata(schema):%0A    %22%22%22%0A    Finds Prereg Challenge registrations whose registered_meta includes q5 and corrects%0A    %22%22%22%0A    registrations = Node.find(Q('is_registration', 'eq', True) & Q('registered_schema', 'eq', schema))%0A    total_reg = registrations.count()%0A    logger.info('Examining %7B%7D registrations for q5 metadata'.format(total_reg))%0A    reg_count = 0%0A%0A    for reg in registrations:%0A        reg_count += 1%0A        if reg.registered_meta.get(schema._id, %7B%7D).get('q5', %7B%7D).get('value', %7B%7D):%0A            reg.registered_meta%5Bschema._id%5D%5B'q5'%5D%5B'value'%5D = reg.registered_meta%5Bschema._id%5D%5B'q5'%5D%5B'value'%5D.rstrip()%0A            reg.save()%0A            logger.info('%7B%7D/%7B%7D Migrated q5 response for %7B%7D'.format(reg_count, total_reg, reg._id))%0A        else:%0A            # q5 is a required question, so should be answered, but just in case...%0A            logger.info('%7B%7D/%7B%7D q5 not answered. No change needed for %7B%7D.'.format(reg_count, total_reg, reg._id))%0A%0Adef main(dry=True):%0A    init_app(set_backends=True, routes=False)%0A    if not dry:%0A        scripts_utils.add_file_logger(logger, __file__)%0A    prereg = MetaSchema.find_one(%0A            Q('name', 'eq', %22Prereg Challenge%22))%0A    migrate_drafts_q5_metadata(prereg)%0A    migrate_registrations_q5_metadata(prereg)%0A%0A%0Aif __name__ == '__main__':%0A    dry_run = '--dry' in sys.argv%0A    with TokuTransaction():%0A        main(dry=dry_run)%0A        if dry_run:%0A            raise RuntimeError('Dry run, rolling back transaction.')%0A

Add migration to correct q5 responses for registrations and drafts using Prereg Challenge schema.