Skip to content

Commit

Permalink
permissions: alembic script to migrate librarian roles.
Browse files Browse the repository at this point in the history
Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Jan 25, 2023
1 parent 3201eef commit e167fd7
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
112 changes: 112 additions & 0 deletions rero_ils/alembic/5f0b086e4b82_patron_role_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019-2022 RERO
# Copyright (C) 2019-2022 UCLouvain
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Patron role migration."""

from logging import getLogger

from rero_ils.modules.patrons.api import Patron, PatronsIndexer, PatronsSearch
from rero_ils.modules.users.models import UserRole

# revision identifiers, used by Alembic.
revision = '5f0b086e4b82'
down_revision = 'eec683a446e5'
branch_labels = ()
depends_on = None

LOGGER = getLogger('alembic')
indexing_chunck_size = 100


def upgrade():
"""Upgrade database."""

def get_new_roles(roles):
if 'system_librarian' in roles:
return [UserRole.FULL_PERMISSIONS]
elif 'librarian' in roles:
return [
UserRole.PROFESSIONAL_READ_ONLY,
UserRole.ACQUISITION_MANAGER,
UserRole.CATALOG_MANAGER,
UserRole.CIRCULATION_MANAGER,
UserRole.USER_MANAGER
]

query = PatronsSearch()\
.filter('terms', roles=['librarian', 'system_librarian'])\
.source(False)
patron_uuids = []

for hit in query.scan():
patron = Patron.get_record(hit.meta.id)
original_roles = patron.get('roles')
migrated_roles = get_new_roles(original_roles)
LOGGER.info(f'* Updating ptrn#{patron.pid} [{patron.formatted_name}]')
LOGGER.info(f'\t - Original roles are : [{original_roles}]')
LOGGER.info(f'\t - New roles are : [{migrated_roles}]')
patron['roles'] = migrated_roles
patron.update(patron, dbcommit=True, reindex=False)
patron_uuids.append(hit.meta.id)

_indexing_records(patron_uuids)


def downgrade():
"""Downgrade database."""

def get_original_roles(roles):
if UserRole.FULL_PERMISSIONS in roles:
return ['system_librarian']
elif any(role in UserRole.LIBRARIAN_ROLES for role in roles):
return ['librarian']

query = PatronsSearch()\
.filter('terms', roles=UserRole.PROFESSIONAL_ROLES)\
.source(False)
patron_uuids = []

for hit in query.scan():
patron = Patron.get_record(hit.meta.id)
current_roles = patron.get('roles')
original_roles = get_original_roles(current_roles)
LOGGER.info(f'* Updating ptrn#{patron.pid} [{patron.formatted_name}]')
LOGGER.info(f'\t - Current roles are : {current_roles}')
LOGGER.info(f'\t - Original roles are : {original_roles}')
patron['roles'] = original_roles
patron.update(patron, dbcommit=True, reindex=False)
patron_uuids.append(hit.meta.id)

_indexing_records(patron_uuids)


def _indexing_records(record_ids):
"""Indexing some record based on record uuid."""
if not record_ids:
return

LOGGER.info(f'Indexing {len(record_ids)} records ....')
indexer = PatronsIndexer()
chunks = [
record_ids[x:x + indexing_chunck_size]
for x in range(0, len(record_ids), indexing_chunck_size)
]
for chuncked_ids in chunks:
indexer.bulk_index(chuncked_ids)
count = indexer.process_bulk_queue()
LOGGER.info(f'{count} records indexed.')
40 changes: 40 additions & 0 deletions rero_ils/alembic/eec683a446e5_merging_rero_ils_branches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019-2022 RERO
# Copyright (C) 2019-2022 UCLouvain
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Merging RERO-ILS branches."""

from logging import getLogger

# revision identifiers, used by Alembic.
revision = 'eec683a446e5'
down_revision = ('fc45b1b998b8', 'a941628259e1')
branch_labels = ()
depends_on = None


LOGGER = getLogger('alembic')


def upgrade():
"""Upgrade database."""
LOGGER.info("Merging commit, nothing to do")


def downgrade():
"""Downgrade database."""
LOGGER.info("Merging commit, nothing to do")

0 comments on commit e167fd7

Please sign in to comment.