def find_missing_files(): with_files = models.Node.find( Q('files_current', 'exists', True) | Q('files_versions', 'exists', True)) for node in with_files: for fname, fid in node.files_current.items(): fobj = models.NodeFile.load(fid) if fobj is None: print 'Inconsistency: File object {} not found in MongoDB'.format( fid, ) continue try: version = len(node.files_versions[fname]) - 1 except KeyError: print 'Inconsistency: File name {} not in files_versions of node {} ({})'.format( fname, node.title, node._primary_key, ) continue try: node.file(fobj.path, version=version) except: print 'Inconsistency: Could not load file {} ({}) on node {} ({})'.format( fobj.path, fobj._primary_key, node.title, node._primary_key, )
def impute_creator(dry_run=True): no_creator = models.Node.find( Q('creator', 'eq', None) & Q('contributors', 'ne', [])) for node in no_creator: print u'Imputing creator {} for node title {}'.format( node.contributors[0].fullname, node.title, ) if not dry_run: node.creator = node.contributors[0] node.save()
def test_get_targets_referent_points_to_nothing(self): node = NodeFactory() bad_guid = Guid(referent=node) bad_guid.save() Node.remove(Q('_id', 'eq', node._id)) targets = list(get_targets()) assert_in(bad_guid, targets) assert_not_in(self.nontarget_guid, targets)
def impute_wiki_date(dry_run=True): no_date = models.NodeWikiPage.find(Q('date', 'eq', None)) for wiki in no_date: oid = ObjectId(wiki._primary_key) imputed_date = oid.generation_time print u'Imputing date {} for wiki ID {}'.format( imputed_date.strftime('%c'), wiki._primary_key, ) if not dry_run: wiki._fields['date'].__set__(wiki, imputed_date, safe=True) wiki.save()
def impute_log_date(dry_run=True): no_date = models.NodeLog.find(Q('date', 'eq', None)) for log in no_date: oid = ObjectId(log._primary_key) imputed_date = oid.generation_time print u'Imputing date {} for log ID {}'.format( imputed_date.strftime('%c'), log._primary_key, ) if not dry_run: log._fields['date'].__set__(log, imputed_date, safe=True) log.save()
def find_bad_registrations(): """Find registrations with unexpected numbers of template keys or outdated templates. """ registrations = models.Node.find(Q('is_registration', 'eq', True)) for registration in registrations: meta = registration.registered_meta or {} keys = meta.keys() if len(keys) != 1: print 'Inconsistency: Number of keys on project {} ({}) != 1'.format( registration.title, registration._primary_key, ) continue if keys[0] not in known_schemas: print 'Inconsistency: Registration schema {} on project {} ({}) not in known schemas'.format( keys[0], registration.title, registration._primary_key, )
from website.app import init_app from website.models import Node, User from framework import Q from framework.analytics import piwik app = init_app('website.settings', set_backends=True) # NOTE: This is a naive implementation for migration, requiring a POST request # for every user and every node. It is possible to bundle these together in a # single request, but it would require duplication of logic and strict error # checking of the result. Doing it this way is idempotent, and allows any # exceptions raised to halt the process with a usable error message. for user in User.find(): if user.piwik_token: continue piwik.create_user(user) for node in Node.find( Q('is_public', 'eq', True) & Q('is_deleted', 'eq', False)): if node.piwik_site_id: continue piwik._provision_node(node._id)
Performed on production by sloria on 2014-08-15 at 11.45AM. 892 invalid GUID objects were removed. """ import sys from nose.tools import * # noqa from framework import Q from framework.guid.model import Guid from website.app import init_app from tests.base import OsfTestCase from tests.factories import TagFactory, NodeFactory QUERY = Q('referent.1', 'eq', "tag") def main(): # Set up storage backends init_app(routes=False) targets = get_targets() if 'dry' in sys.argv: print('{n} invalid GUID objects will be removed.'.format( n=targets.count())) sys.exit(0) else: do_migration() if get_targets().count() == 0: print('All invalid references removed.') else: