def migrate_links(self): print cformat('%{white!}migrating links') for avatars in grouper(self._iter_avatars(), 2500, skip_missing=True): avatars = {int(a.id): a for a in avatars} users = ((u, avatars[u.id]) for u in User.find(User.id.in_(avatars))) for user, avatar in committing_iterator( self.flushing_iterator(users, 250), 2500): registrants = set() user_shown = False for type_, entries in avatar.linkedTo.iteritems(): # store registrant roles, in order to avoid duplication below for role, objects in entries.iteritems(): if (type_ == 'category' and role == 'favorite') or type_ == 'group': continue if not objects: continue if type_ == 'registration' and role == 'registrant': registrants |= set(objects) if not user_shown: print cformat( '%{green}+++%{reset} ' '%{white!}{:6d}%{reset} %{cyan}{}%{reset}' ).format(user.id, user.full_name) user_shown = True print cformat( '%{blue!}<->%{reset} ' '%{yellow!}{:4d}%{reset}x %{green!}{:12} %{cyan!}{}%{reset}' ).format(len(objects), type_, role) for obj in objects: try: UserLink.create_link(user, obj, role, type_) except Exception as e: print cformat( '%{red!}!!!%{reset} ' '%{red!}linking failed%{reset} (%{yellow!}{}%{reset}): ' '{}').format(unicode(e), obj) # add old "registrant" entries to registration/registrant for reg in getattr(avatar, 'registrants', {}).itervalues(): if reg.getConference().getOwner( ) and reg not in registrants: UserLink.create_link(user, reg, 'registrant', 'registration') print cformat( '%{cyan!}<->%{reset} ' '%{yellow!} 1%{reset}x %{green!}{:12} %{cyan!}{}%{reset}' ).format('registration', 'registrant')
def unlink_to(self, obj, role): """Removes a link between the user and an object :param obj: a legacy object :param role: the role to use in the link """ return UserLink.remove_link(self, obj, role)
def link_to(self, obj, role): """Adds a link between the user and an object :param obj: a legacy object :param role: the role to use in the link """ return UserLink.create_link(self, obj, role)
def migrate_links(self): print cformat('%{white!}migrating links') for avatars in grouper(self._iter_avatars(), 2500, skip_missing=True): avatars = {int(a.id): a for a in avatars} users = ((u, avatars[u.id]) for u in User.find(User.id.in_(avatars))) for user, avatar in committing_iterator(self.flushing_iterator(users, 250), 2500): registrants = set() user_shown = False for type_, entries in avatar.linkedTo.iteritems(): # store registrant roles, in order to avoid duplication below for role, objects in entries.iteritems(): if (type_ == 'category' and role == 'favorite') or type_ == 'group': continue if not objects: continue if type_ == 'registration' and role == 'registrant': registrants |= set(objects) if not user_shown: print cformat('%{green}+++%{reset} ' '%{white!}{:6d}%{reset} %{cyan}{}%{reset}').format(user.id, user.full_name) user_shown = True print cformat('%{blue!}<->%{reset} ' '%{yellow!}{:4d}%{reset}x %{green!}{:12} %{cyan!}{}%{reset}').format( len(objects), type_, role) for obj in objects: try: UserLink.create_link(user, obj, role, type_) except Exception as e: print cformat('%{red!}!!!%{reset} ' '%{red!}linking failed%{reset} (%{yellow!}{}%{reset}): ' '{}').format(unicode(e), obj) # add old "registrant" entries to registration/registrant for reg in getattr(avatar, 'registrants', {}).itervalues(): if reg.getConference().getOwner() and reg not in registrants: UserLink.create_link(user, reg, 'registrant', 'registration') print cformat('%{cyan!}<->%{reset} ' '%{yellow!} 1%{reset}x %{green!}{:12} %{cyan!}{}%{reset}').format( 'registration', 'registrant')
def get_linked_objects(self, type_, role): """Retrieves linked objects for the user""" return UserLink.get_links(self, type_, role)
def get_linked_roles(self, type_): """Retrieves the roles the user is linked to for a given type""" return UserLink.get_linked_roles(self, type_)