Get rid of find() and its friends

see indico/indico#4684
This commit is contained in:
Adrian Moennich 2021-01-19 17:45:41 +01:00
parent 1056a61307
commit 26e1981705
14 changed files with 31 additions and 30 deletions

View File

@ -34,7 +34,7 @@ def available_backends():
def agents():
"""Lists the currently active agents"""
print('The following LiveSync agents are active:')
agent_list = LiveSyncAgent.find().order_by(LiveSyncAgent.backend_name, db.func.lower(LiveSyncAgent.name)).all()
agent_list = LiveSyncAgent.query.order_by(LiveSyncAgent.backend_name, db.func.lower(LiveSyncAgent.name)).all()
table_data = [['ID', 'Name', 'Backend', 'Initial Export', 'Queue']]
for agent in agent_list:
initial = (cformat('%{green!}done%{reset}') if agent.initial_data_exported else
@ -60,7 +60,7 @@ def agents():
@click.option('--force', is_flag=True, help="Perform export even if it has already been done once.")
def initial_export(agent_id, force):
"""Performs the initial data export for an agent"""
agent = LiveSyncAgent.find_first(id=agent_id)
agent = LiveSyncAgent.get(agent_id)
if agent is None:
print('No such agent')
return
@ -73,7 +73,7 @@ def initial_export(agent_id, force):
print(cformat('To re-run it, use %{yellow!}--force%{reset}'))
return
agent.create_backend().run_initial_export(Event.find(is_deleted=False))
agent.create_backend().run_initial_export(Event.query.filter_by(is_deleted=False))
agent.initial_data_exported = True
db.session.commit()
@ -84,9 +84,9 @@ def initial_export(agent_id, force):
def run(agent_id, force=False):
"""Runs the livesync agent"""
if agent_id is None:
agent_list = LiveSyncAgent.find_all()
agent_list = LiveSyncAgent.query.all()
else:
agent = LiveSyncAgent.find_first(id=agent_id)
agent = LiveSyncAgent.get(agent_id)
if agent is None:
print('No such agent')
return

View File

@ -20,7 +20,7 @@ from indico_livesync.models.agents import LiveSyncAgent
def extend_plugin_details():
agents = LiveSyncAgent.find().order_by(LiveSyncAgent.name, LiveSyncAgent.id).all()
agents = LiveSyncAgent.query.order_by(LiveSyncAgent.name, LiveSyncAgent.id).all()
return render_plugin_template('plugin_details_extra.html', agents=agents, backends=current_plugin.backend_classes)

View File

@ -42,7 +42,7 @@ class MARCXMLGenerator:
self.xml_generator.initXml()
self.xml_generator.openTag('collection', [['xmlns', 'http://www.loc.gov/MARC21/slim']])
# This is horrible. but refactoring all the code in the indico core would be just as bad.
admin = User.find_first(is_admin=True)
admin = User.query.filter_by(is_admin=True).first()
self.output_generator = outputGenerator(admin, self.xml_generator)
def safe_add_object(self, obj, deleted=False):

View File

@ -82,19 +82,19 @@ def _process_cascaded_category_contents(records):
# Protection changes are handled differently, as there may not be the need to re-generate the record
if category_prot_records:
for categ in Category.find(Category.id.in_(category_prot_records)):
for categ in Category.query.filter(Category.id.in_(category_prot_records)):
cte = categ.get_protection_parent_cte()
# Update only children that inherit
inheriting_categ_children = (Event.query
.join(cte, db.and_((Event.category_id == cte.c.id),
(cte.c.protection_parent == categ.id))))
inheriting_direct_children = Event.find((Event.category_id == categ.id) & Event.is_inheriting)
inheriting_direct_children = Event.query.filter((Event.category_id == categ.id) & Event.is_inheriting)
changed_events.update(itertools.chain(inheriting_direct_children, inheriting_categ_children))
# Add move operations and explicitly-passed event records
if category_move_records:
changed_events.update(Event.find(Event.category_chain_overlaps(category_move_records)))
changed_events.update(Event.query.filter(Event.category_chain_overlaps(category_move_records)))
yield from _process_cascaded_event_contents(records, additional_events=changed_events)
@ -119,14 +119,14 @@ def _process_cascaded_event_contents(records, additional_events=None):
event_records = {rec.event_id for rec in records if rec.type == EntryType.event}
if event_records:
changed_events.update(Event.find(Event.id.in_(event_records)))
changed_events.update(Event.query.filter(Event.id.in_(event_records)))
yield from changed_events
# Sessions are added (explicitly changed only, since they don't need to be sent anywhere)
if session_records:
changed_contributions.update(Contribution
.find(Contribution.session_id.in_(session_records), ~Contribution.is_deleted))
changed_contributions.update(Contribution.query
.filter(Contribution.session_id.in_(session_records), ~Contribution.is_deleted))
# Contributions are added (implictly + explicitly changed)
changed_event_ids = {ev.id for ev in changed_events}
@ -134,7 +134,7 @@ def _process_cascaded_event_contents(records, additional_events=None):
condition = Contribution.event_id.in_(changed_event_ids) & ~Contribution.is_deleted
if contribution_records:
condition = db.or_(condition, Contribution.id.in_(contribution_records))
contrib_query = Contribution.find(condition).options(joinedload('subcontributions'))
contrib_query = Contribution.query.filter(condition).options(joinedload('subcontributions'))
for contribution in contrib_query:
yield contribution
@ -142,5 +142,5 @@ def _process_cascaded_event_contents(records, additional_events=None):
# Same for subcontributions
if subcontribution_records:
changed_subcontributions.update(SubContribution.find(SubContribution.id.in_(subcontribution_records)))
changed_subcontributions.update(SubContribution.query.filter(SubContribution.id.in_(subcontribution_records)))
yield from changed_subcontributions

View File

@ -18,7 +18,7 @@ from indico_livesync.util import clean_old_entries
def scheduled_update():
from indico_livesync.plugin import LiveSyncPlugin
clean_old_entries()
for agent in LiveSyncAgent.find_all():
for agent in LiveSyncAgent.query.all():
if agent.backend is None:
LiveSyncPlugin.logger.warning('Skipping agent %s; backend not found', agent.name)
continue

View File

@ -63,8 +63,9 @@ def clean_old_entries():
if not queue_entry_ttl:
return
expire_threshold = now_utc() - timedelta(days=queue_entry_ttl)
LiveSyncQueueEntry.find(LiveSyncQueueEntry.processed,
LiveSyncQueueEntry.timestamp < expire_threshold).delete(synchronize_session='fetch')
query = LiveSyncQueueEntry.query.filter(LiveSyncQueueEntry.processed,
LiveSyncQueueEntry.timestamp < expire_threshold)
query.delete(synchronize_session='fetch')
@memoize_request

View File

@ -24,13 +24,13 @@ def test_clean_old_entries(dummy_event, db, dummy_agent):
db.session.flush()
# Nothing deleted with the setting's default value
clean_old_entries()
assert LiveSyncQueueEntry.find().count() == 20
assert LiveSyncQueueEntry.query.count() == 20
# Nothing deleted when explicitly set to 0 (which is the default)
LiveSyncPlugin.settings.set('queue_entry_ttl', 0)
clean_old_entries()
assert LiveSyncQueueEntry.find().count() == 20
assert LiveSyncQueueEntry.query.count() == 20
# Only the correct entries deleted, and no unprocessed ones
LiveSyncPlugin.settings.set('queue_entry_ttl', 3)
clean_old_entries()
assert LiveSyncQueueEntry.find(processed=False).count() == 10
assert LiveSyncQueueEntry.find(processed=True).count() == 3
assert LiveSyncQueueEntry.query.filter_by(processed=False).count() == 10
assert LiveSyncQueueEntry.query.filter_by(processed=True).count() == 3

View File

@ -37,7 +37,7 @@ class RHPaypalIPN(RH):
def _process_args(self):
self.token = request.args['token']
self.registration = Registration.find_first(uuid=self.token)
self.registration = Registration.query.filter_by(uuid=self.token).first()
if not self.registration:
raise BadRequest

View File

@ -101,7 +101,7 @@ class PiwikPlugin(IndicoPlugin):
params['event_id'] = request.view_args['confId']
contrib_id = request.view_args.get('contrib_id')
if contrib_id is not None and str(contrib_id).isdigit():
contribution = Contribution.find_first(event_id=params['event_id'], id=contrib_id)
contribution = Contribution.query.filter_by(event_id=params['event_id'], id=contrib_id).first()
if contribution:
cid = (contribution.legacy_mapping.legacy_contribution_id if contribution.legacy_mapping
else contribution.id)

View File

@ -25,7 +25,7 @@ class RHEventPreviewIPyNB(RH):
raise Forbidden
def _process_args(self):
self.attachment = Attachment.find_one(id=request.view_args['attachment_id'], is_deleted=False)
self.attachment = Attachment.query.filter_by(id=request.view_args['attachment_id'], is_deleted=False).one()
def _process(self):
config = Config()

View File

@ -22,7 +22,7 @@ def cli():
def rooms(status=None):
"""Lists all Vidyo rooms"""
room_query = VCRoom.find(type='vidyo')
room_query = VCRoom.query.filter_by(type='vidyo')
table_data = [['ID', 'Name', 'Status', 'Vidyo ID', 'Extension']]
if status:

View File

@ -322,7 +322,7 @@ class VidyoPlugin(VCPluginMixin, IndicoPlugin):
def _merge_users(self, target, source, **kwargs):
super()._merge_users(target, source, **kwargs)
for ext in VidyoExtension.find(owned_by_user=source):
for ext in VidyoExtension.query.filter_by(owned_by_user=source):
ext.owned_by_user = target
flag_modified(ext.vc_room, 'data')

View File

@ -34,7 +34,7 @@ def find_old_vidyo_rooms(max_room_event_age):
.group_by(VCRoom.id))
# non-deleted rooms with no recent associations
return VCRoom.find_all(VCRoom.status != VCRoomStatus.deleted, ~VCRoom.id.in_(recently_used))
return VCRoom.query.filter(VCRoom.status != VCRoomStatus.deleted, ~VCRoom.id.in_(recently_used)).all()
def notify_owner(plugin, vc_room):

View File

@ -33,7 +33,7 @@ def iter_user_identities(user):
def get_user_from_identifier(settings, identifier):
"""Get an actual User object from an identifier"""
providers = list(auth.strip() for auth in settings.get('authenticators').split(','))
identities = Identity.find_all(Identity.provider.in_(providers), Identity.identifier == identifier)
identities = Identity.query.filter(Identity.provider.in_(providers), Identity.identifier == identifier).all()
if identities:
return sorted(identities, key=lambda x: providers.index(x.provider))[0].user
for provider in providers:
@ -48,7 +48,7 @@ def get_user_from_identifier(settings, identifier):
emails = {email.lower() for email in identity_info.data.getlist('email') if email}
if not emails:
continue
user = User.find_first(~User.is_deleted, User.all_emails.in_(list(emails)))
user = User.query.filter(~User.is_deleted, User.all_emails.in_(list(emails))).first()
if user:
return user