def test_delete_all_by_username(self): assert len(list(self.db.select("bookshelves_books"))) == 3 Bookshelves.delete_all_by_username("@kilgore_trout") assert len(list(self.db.select("bookshelves_books"))) == 1 assert len(list(self.db.select("booknotes"))) == 2 Booknotes.delete_all_by_username('@kilgore_trout') assert len(list(self.db.select("booknotes"))) == 1 assert len(list(self.db.select("ratings"))) == 2 Ratings.delete_all_by_username("@kilgore_trout") assert len(list(self.db.select("ratings"))) == 1 assert len(list(self.db.select("observations"))) == 2 Observations.delete_all_by_username("@kilgore_trout") assert len(list(self.db.select("observations"))) == 1
def anonymize(self, test=False): # Generate new unique username for patron: # Note: Cannot test get_activation_link() locally uuid = (self.get_activation_link()['code'] if self.get_activation_link() else generate_uuid()) new_username = f'anonymous-{uuid}' results = {'new_username': new_username} # Delete all of the patron's book notes: results['booknotes_count'] = Booknotes.delete_all_by_username( self.username, _test=test) # Anonymize patron's username in OL DB tables: results['ratings_count'] = Ratings.update_username(self.username, new_username, _test=test) results['observations_count'] = Observations.update_username( self.username, new_username, _test=test) results['bookshelves_count'] = Bookshelves.update_username( self.username, new_username, _test=test) if not test: patron = self.get_user() email = self.email username = self.username # Remove patron from all usergroups: for grp in patron.usergroups: grp.remove_user(patron.key) # Set preferences to default: patron.save_preferences({'updates': 'no', 'public_readlog': 'no'}) # Clear patron's profile page: data = {'key': patron.key, 'type': '/type/delete'} patron.set_data(data) # Remove account information from store: del web.ctx.site.store[f'account/{username}'] del web.ctx.site.store[f'account/{username}/verify'] del web.ctx.site.store[f'account/{username}/password'] del web.ctx.site.store[f'account-email/{email}'] return results