Skip to content

Commit

Permalink
datastore: add create_or_update_role func
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 authored and TLGINO committed May 13, 2023
1 parent b2f1cf1 commit e87a446
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
15 changes: 15 additions & 0 deletions invenio_accounts/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from flask_security import SQLAlchemyUserDatastore

from .models import Role
from .proxies import current_db_change_history
from .sessions import delete_user_sessions
from .signals import datastore_post_commit, datastore_pre_commit
Expand Down Expand Up @@ -41,3 +42,17 @@ def mark_changed(self, sid, uid=None, rid=None):
current_db_change_history.updated_users[sid].append(uid)
elif rid:
current_db_change_history.updated_roles[sid].append(uid)

def update_role(self, role):
"""Merge roles."""
return self.db.session.merge(role)

def create_or_udpate_role(self, **kwargs):
"""Creates or updates a role with the provided parameters."""
_id = kwargs.get("id")
existing_role = self.role_model.query.filter_by(id=_id).one_or_none()
if existing_role:
updated_role = Role(**kwargs)
return self.update_role(updated_role)
else:
return self.create_role(**kwargs)
8 changes: 5 additions & 3 deletions invenio_accounts/profiles/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def validate_visibility(value):

def validate_locale(value):
"""Check if the value is a valid locale."""
locales = current_app.extensions["invenio-i18n"].get_locales()
locales = [locale.language for locale in locales]
locales = current_app.extensions.get("invenio-i18n")
if locales:
locales = locales.get_locales()
locales = [locale.language for locale in locales]

if value not in locales:
if locales is not None and value not in locales:
raise ValidationError(message=str(_("Value must be a valid locale.")))
current_app.config["BABEL_DEFAULT_LOCALE"] = value

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ install_requires =
invenio-mail>=1.0.2
invenio-rest>=1.2.4
invenio-theme>=2.0.0
invenio-access>=1.4.5
maxminddb-geolite2>=2017.404
pyjwt>=1.5.0
simplekv>=0.11.2
Expand Down
27 changes: 25 additions & 2 deletions tests/test_invenio_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def test_init_rest():
assert "security_email_templates" in app.blueprints.keys()


@pytest.mark.skip(reason="Cross dependency with invenio-access") # TODO fix this at a later date
@pytest.mark.skip(
reason="Cross dependency with invenio-access"
) # TODO fix this at a later date
def test_alembic(app):
"""Test alembic recipes."""
ext = app.extensions["invenio-db"]
Expand Down Expand Up @@ -126,7 +128,7 @@ def test_datastore_usercreate(app):


def test_datastore_rolecreate(app):
"""Test create user."""
"""Test create role."""
ds = app.extensions["invenio-accounts"].datastore

with app.app_context():
Expand All @@ -137,6 +139,27 @@ def test_datastore_rolecreate(app):
assert 1 == Role.query.filter_by(name="superuser").count()


def test_datastore_create_or_update_role(app):
"""Test create and update role."""
ds = app.extensions["invenio-accounts"].datastore

with app.app_context():
r1 = ds.create_or_udpate_role(id="1", name="superuser", description="1234")
ds.commit()
r2 = ds.find_role("superuser")
assert r1 == r2
assert 1 == Role.query.filter_by(name="superuser").count()

r1 = ds.create_or_udpate_role(
id="1", name="megauser", description="updated description"
)
ds.commit()
r2 = ds.find_role("megauser")
assert r1 == r2
assert r2.description == "updated description"
assert 1 == Role.query.filter_by(name="megauser").count()


def test_datastore_assignrole(app):
"""Create and assign user to role."""
ds = app.extensions["invenio-accounts"].datastore
Expand Down

0 comments on commit e87a446

Please sign in to comment.