def fix_location(slug, cursor=None, total=0): # Don't try to lookup slugs that are the empty string. # hint they don't exist! if not slug: return location_slug = slug location = Location.get_or_insert(location_slug) projects = set([]) if cursor: # we are looping Grab the existing project list so we don't # wipe out the earlier runs work location_p = getattr(location, 'projects', []) projects = set(location_p) cursor = Cursor(urlsafe=cursor) people = User.query().filter(User.location_slug == location_slug) # Go through the users in chucks models, next_cursor, more = people.fetch_page(100, start_cursor=cursor) for model in models: user_projects = getattr(model, 'projects', []) user_total = getattr(model, 'total', 0) # Do a little math to figure out how many commits they have commits = user_total - (len(user_projects) * 10) if commits > 0: logging.info('Adding %s to %s', commits, location_slug) total += commits # Add the users projects to the project set (this filters duplicates) projects.update(user_projects) # Run update in a transaction projects = list(projects) total = total + (len(projects) * 10) @ndb.transactional def txn(): location = Location.get_or_insert(location_slug) location.total = total location.projects = projects location.put() txn() if more: # We have more people to loop through!! return deferred.defer(fix_location, location_slug, cursor=next_cursor.urlsafe(), total=total)
def txn(): location = Location.get_or_insert(location_slug) location.total = total location.projects = projects location.put()