Skip to content

Commit

Permalink
global: make forward compatible to sqlalchemy >= 2
Browse files Browse the repository at this point in the history
  • Loading branch information
utnapischtim committed Nov 12, 2024
1 parent 9909924 commit ac43903
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 66 deletions.
9 changes: 6 additions & 3 deletions invenio_communities/communities/dumpers/featured.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -14,6 +14,7 @@

from datetime import datetime

from invenio_db import db
from invenio_records.dumpers import SearchDumperExt

from invenio_communities.communities.records.models import CommunityFeatured
Expand All @@ -30,7 +31,8 @@ def dump(self, record, data):
"""Dump featured entries."""
now_ = datetime.utcnow()
future_entries = (
CommunityFeatured.query.filter(
db.session.query(CommunityFeatured)
.filter(
CommunityFeatured.community_id == record.id,
CommunityFeatured.start_date > now_,
)
Expand All @@ -39,7 +41,8 @@ def dump(self, record, data):
)

past_entries = (
CommunityFeatured.query.filter(
db.session.query(CommunityFeatured)
.filter(
CommunityFeatured.community_id == record.id,
CommunityFeatured.start_date <= now_,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2022 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -41,9 +42,11 @@ def resolve(self, pid_value, registered_only=True):
raise PIDDoesNotExistError("comid", "")

with db.session.no_autoflush: # avoid flushing the current session
model = self.record_cls.model_cls.query.filter_by(
**{field_name: pid_value}
).one_or_none()
model = (
db.session.query(self.record_cls.model_cls)
.filter_by(**{field_name: pid_value})
.one_or_none()
)
if model is None:
raise PIDDoesNotExistError("comid", str(pid_value))
record = self.record_cls(model.data, model=model)
Expand Down
8 changes: 6 additions & 2 deletions invenio_communities/communities/services/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This file is part of Invenio.
# Copyright (C) 2016-2024 CERN.
# Copyright (C) 2022 Northwestern University.
# Copyright (C) 2022-2023 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -152,7 +152,11 @@ class OAISetComponent(ServiceComponent):
"""Service component for OAI set integration."""

def _retrieve_set(self, slug):
return OAISet.query.filter(OAISet.spec == self._create_set_spec(slug)).first()
return (
db.session.query(OAISet)
.filter(OAISet.spec == self._create_set_spec(slug))
.first()
)

def _create_set_spec(self, community_slug):
oai_sets_prefix = current_app.config["COMMUNITIES_OAI_SETS_PREFIX"]
Expand Down
17 changes: 12 additions & 5 deletions invenio_communities/communities/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from flask import current_app
from invenio_cache.decorators import cached_with_expiration
from invenio_db import db
from invenio_records_resources.proxies import current_service_registry
from invenio_records_resources.services.base import LinksTemplate
from invenio_records_resources.services.records import (
Expand Down Expand Up @@ -295,7 +296,9 @@ def _get_featured_entry(self, raise_error=True, **kwargs):
"""Retrieve featured entry based on provided arguments."""
errors = []
try:
featured_entry = CommunityFeatured.query.filter_by(**kwargs).one()
featured_entry = (
db.session.query(CommunityFeatured).filter_by(**kwargs).one()
)
except NoResultFound as e:
if raise_error:
raise CommunityFeaturedEntryDoesNotExistError(kwargs)
Expand Down Expand Up @@ -345,10 +348,14 @@ def featured_list(self, identity, community_id):
# Permissions
self.require_permission(identity, "featured_list", record=record)

featured_entries = CommunityFeatured.query.filter(
CommunityFeatured.community_id == record.id,
).paginate(
per_page=1000,
featured_entries = (
db.session.query(CommunityFeatured)
.filter(
CommunityFeatured.community_id == record.id,
)
.paginate(
per_page=1000,
)
)

return self.config.result_list_cls_featured(
Expand Down
2 changes: 1 addition & 1 deletion invenio_communities/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2016-2024 CERN.
# Copyright (C) 2021 Graz University of Technology.
# Copyright (C) 2021-2024 Graz University of Technology.
# Copyright (C) 2021 TU Wien.
# Copyright (C) 2022 Northwestern University.
#
Expand Down
16 changes: 10 additions & 6 deletions invenio_communities/members/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Copyright (C) 2022 Northwestern University.
# Copyright (C) 2022 CERN.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -105,7 +105,7 @@ def get_memberships_from_group_ids(cls, identity, group_ids):
def get_memberships(cls, identity):
"""Get community memberships for a given identity."""
group_ids = []
user = User.query.filter(User.id == identity.id).one_or_none()
user = db.session.query(User).filter(User.id == identity.id).one_or_none()
if user:
group_ids = [r.id for r in user.roles]

Expand All @@ -119,9 +119,11 @@ def get_member_by_request(cls, request_id):
"""Get a membership by request id."""
assert request_id is not None
with db.session.no_autoflush:
obj = cls.model_cls.query.filter(
cls.model_cls.request_id == request_id
).one()
obj = (
db.session.query(cls.model_cls)
.filter(cls.model_cls.request_id == request_id)
.one()
)
return cls(obj.data, model=obj)

@classmethod
Expand All @@ -139,7 +141,9 @@ def get_members(cls, community_id, members=None):
raise InvalidMemberError(m)

with db.session.no_autoflush:
q = cls.model_cls.query.filter(cls.model_cls.community_id == community_id)
q = db.session.query(cls.model_cls).filter(
cls.model_cls.community_id == community_id
)

# Apply user and group query if applicable
user_q = cls.model_cls.user_id.in_(user_ids)
Expand Down
6 changes: 4 additions & 2 deletions invenio_communities/members/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Copyright (C) 2022 Northwestern University.
# Copyright (C) 2022-2024 CERN.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -105,7 +105,9 @@ def query_memberships(cls, user_id=None, group_ids=None, active=True):
@classmethod
def count_members(cls, community_id, role=None, active=True):
"""Count number of members."""
q = cls.query.filter(cls.community_id == community_id, cls.active == active)
q = db.session.query(cls).filter(
cls.community_id == community_id, cls.active == active
)
if role is not None:
q = q.filter(cls.role == role)
return q.count()
Expand Down
5 changes: 3 additions & 2 deletions invenio_communities/members/services/components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -10,6 +10,7 @@

from flask_principal import Identity
from invenio_accounts.models import Role
from invenio_db import db
from invenio_records_resources.services.records.components import ServiceComponent

from invenio_communities.members.records.api import MemberMixin
Expand Down Expand Up @@ -59,7 +60,7 @@ def accept_invite(self, identity, record=None, data=None, **kwargs):
def members_add(self, identity, record=None, community=None, data=None, **kwargs):
"""On member add (only for groups)."""
if record["type"] == "group":
role = Role.query.filter_by(id=record["id"]).one_or_none()
role = db.session.query(Role).filter_by(id=record["id"]).one_or_none()
if role.is_managed:
users = role.users.all()
for user in users:
Expand Down
18 changes: 13 additions & 5 deletions invenio_communities/members/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Copyright (C) 2022 Northwestern University.
# Copyright (C) 2022-2024 CERN.
# Copyright (C) 2022-2023 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -14,6 +14,7 @@
from flask import current_app
from invenio_access.permissions import system_identity
from invenio_accounts.models import Role
from invenio_db import db
from invenio_i18n import gettext as _
from invenio_notifications.services.uow import NotificationOp
from invenio_records_resources.services import LinksTemplate
Expand Down Expand Up @@ -533,6 +534,7 @@ def update(self, identity, community_id, data, uow=None, refresh=False):

# Perform updates (and check permissions)
for m in members:
print(f"MemberService.update m: {m}")
self._update(identity, community, m, role, visible, uow)

# Make sure we're not left owner-less if a role was changed.
Expand Down Expand Up @@ -731,12 +733,18 @@ def rebuild_index(self, identity, uow=None):
Note: Skips (soft) deleted records.
"""
members = self.record_cls.model_cls.query.filter_by(is_deleted=False).all()
members = (
db.session.query(self.record_cls.model_cls)
.filter_by(is_deleted=False)
.all()
)
self.indexer.bulk_index([member.id for member in members])

archived_invitations = ArchivedInvitation.model_cls.query.filter_by(
is_deleted=False
).all()
archived_invitations = (
db.session.query(ArchivedInvitation.model_cls)
.filter_by(is_deleted=False)
.all()
)
self.archive_indexer.bulk_index([inv.id for inv in archived_invitations])

return True
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""Field context."""

from invenio_db import db
from invenio_records.systemfields import SystemFieldContext


Expand All @@ -25,4 +27,6 @@ class CommunitiesFieldContext(SystemFieldContext):

def query_by_community(self, community_or_id):
"""Query community-record relations for a given community."""
return self.field._m2m_model_class.query.filter(community_id=community_or_id)
return db.session.query(self.field._m2m_model_class).filter(
community_id=community_or_id
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021-2024 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
Expand Down Expand Up @@ -89,9 +90,11 @@ def remove(self, community_or_id):
community_id = self._to_id(community_or_id)

# Delete M2M row.
res = self._m2m_model_cls.query.filter_by(
community_id=community_id, record_id=self._record_id
).delete()
res = (
db.session.query(self._m2m_model_cls)
.filter_by(community_id=community_id, record_id=self._record_id)
.delete()
)
if res != 1:
raise ValueError("The record has not been added to the community.")

Expand All @@ -105,7 +108,9 @@ def remove(self, community_or_id):
def clear(self):
"""Clear all communities from the record."""
# Remove all associations
res = self._m2m_model_cls.query.filter_by(record_id=self._record_id).delete()
db.session.query(self._m2m_model_cls).filter_by(
record_id=self._record_id
).delete()
self._communities_ids = set()
self._default_id = None
self._communities_cache = {}
Expand Down
12 changes: 8 additions & 4 deletions invenio_communities/requests/user_moderation/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# Copyright (C) 2023-2024 CERN.
# Copyright (C) 2023 TU Wien.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -10,6 +11,7 @@
from collections import defaultdict

from invenio_access.permissions import Identity, system_identity
from invenio_db import db
from invenio_i18n import lazy_gettext as _
from invenio_pidstore.errors import PIDDoesNotExistError
from invenio_search.engine import dsl
Expand Down Expand Up @@ -41,7 +43,9 @@ def _get_communities_for_user(user_id):
comm_owners = defaultdict(list)
for comm_owner in [
mem_cls(m.data, model=m)
for m in mem_model_cls.query.filter(mem_model_cls.role == "owner").all()
for m in db.session.query(mem_model_cls)
.filter(mem_model_cls.role == "owner")
.all()
]:
comm_owners[comm_owner.community_id].append(comm_owner)

Expand All @@ -55,9 +59,9 @@ def _get_communities_for_user(user_id):
# resolve the communities in question
communities = [
comm_cls(m.data, model=m)
for m in comm_model_cls.query.filter(
comm_model_cls.id.in_(relevant_comm_ids)
).all()
for m in db.session.query(comm_model_cls)
.filter(comm_model_cls.id.in_(relevant_comm_ids))
.all()
]

return communities
Expand Down
4 changes: 3 additions & 1 deletion invenio_communities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2016-2022 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -12,6 +13,7 @@
from flask_principal import Identity
from invenio_accounts.models import Role
from invenio_accounts.proxies import current_db_change_history
from invenio_db import db

from .generators import CommunityRoleNeed
from .proxies import current_communities, current_identities_cache
Expand Down Expand Up @@ -105,7 +107,7 @@ def on_datastore_post_commit(sender, session):
on_user_membership_change(Identity(user_id))

for role_id in current_db_change_history.sessions[sid].deleted_roles:
role = Role.query.filter_by(id=role_id).one_or_none()
role = db.session.query(Role).filter_by(id=role_id).one_or_none()
users = role.users.all()
for user in users:
on_user_membership_change(Identity(user.id))
Loading

0 comments on commit ac43903

Please sign in to comment.