Move search export schems back to the indico core

This commit is contained in:
Adrian Moennich 2021-06-18 14:08:32 +02:00
parent 73fb546895
commit b063e00955
4 changed files with 4 additions and 306 deletions

View File

@ -16,13 +16,12 @@ from indico.modules.events import Event
from indico.modules.events.contributions.models.contributions import Contribution
from indico.modules.events.contributions.models.subcontributions import SubContribution
from indico.modules.events.notes.models.notes import EventNote
from indico.modules.search.schemas import EventSchema
from indico.modules.search.schemas import (AttachmentSchema, CategorySchema, ContributionSchema, EventNoteSchema,
EventSchema, SubContributionSchema)
from indico.util.string import strip_tags
from indico.web.flask.util import url_for
from indico_citadel.util import remove_none_entries
from indico_livesync.export_schemas import (AttachmentSchema, CategorySchema, ContributionSchema, EventNoteSchema,
SubContributionSchema)
PRINCIPAL_TYPES = {
@ -204,8 +203,6 @@ class _EventNoteDataSchema(EventNoteSchema):
class Meta:
fields = ('title', 'content', 'user')
title = mm.String(attribute='object.title')
@post_dump
def _transform(self, data, **kwargs):
if desc := data.get('content'):

View File

@ -1,116 +0,0 @@
# This file is part of the Indico plugins.
# Copyright (C) 2002 - 2021 CERN
#
# The Indico plugins are free software; you can redistribute
# them and/or modify them under the terms of the MIT License;
# see the LICENSE file for more details.
from marshmallow import fields
from indico.core.db.sqlalchemy.links import LinkType
from indico.core.marshmallow import mm
from indico.modules.attachments import Attachment
from indico.modules.categories import Category
from indico.modules.events.contributions import Contribution
from indico.modules.events.contributions.models.subcontributions import SubContribution
from indico.modules.events.notes.models.notes import EventNote
from indico.modules.search.base import SearchTarget
from indico.modules.search.schemas import LocationSchema, PersonSchema
from indico.util.marshmallow import NoneRemovingList
from indico.web.flask.util import url_for
class CategorySchema(mm.SQLAlchemyAutoSchema):
class Meta:
model = Category
fields = ('id', 'title', 'url')
url = fields.Function(lambda c: url_for('categories.display', category_id=c['id']))
class AttachmentSchema(mm.SQLAlchemyAutoSchema):
class Meta:
model = Attachment
fields = ('attachment_id', 'folder_id', 'type', 'attachment_type', 'title', 'filename', 'event_id',
'contribution_id', 'subcontribution_id', 'user', 'url', 'category_id', 'category_path',
'modified_dt')
attachment_id = fields.Int(attribute='id')
folder_id = fields.Int(attribute='folder_id')
type = fields.Constant(SearchTarget.attachment.name)
attachment_type = fields.String(attribute='type.name')
filename = fields.String(attribute='file.filename')
event_id = fields.Int(attribute='folder.event.id')
contribution_id = fields.Method('_contribution_id')
subcontribution_id = fields.Int(attribute='folder.subcontribution_id')
user = fields.Nested(PersonSchema)
category_id = fields.Int(attribute='folder.event.category_id')
category_path = fields.List(fields.Nested(CategorySchema), attribute='folder.event.detailed_category_chain')
url = fields.String(attribute='download_url')
def _contribution_id(self, attachment):
if attachment.folder.link_type == LinkType.contribution:
return attachment.folder.contribution_id
elif attachment.folder.link_type == LinkType.subcontribution:
return attachment.folder.subcontribution.contribution_id
return None
class ContributionSchema(mm.SQLAlchemyAutoSchema):
class Meta:
model = Contribution
fields = ('contribution_id', 'type', 'contribution_type', 'event_id', 'title', 'description', 'location',
'persons', 'url', 'category_id', 'category_path', 'start_dt', 'end_dt', 'duration')
contribution_id = fields.Int(attribute='id')
type = fields.Constant(SearchTarget.contribution.name)
contribution_type = fields.String(attribute='type.name')
location = fields.Function(lambda contrib: LocationSchema().dump(contrib))
persons = NoneRemovingList(fields.Nested(PersonSchema), attribute='person_links')
category_id = fields.Int(attribute='event.category_id')
category_path = fields.List(fields.Nested(CategorySchema), attribute='event.detailed_category_chain')
url = fields.Function(lambda contrib: url_for('contributions.display_contribution', contrib, _external=False))
duration = fields.TimeDelta(precision=fields.TimeDelta.MINUTES)
class SubContributionSchema(mm.SQLAlchemyAutoSchema):
class Meta:
model = SubContribution
fields = ('subcontribution_id', 'type', 'title', 'description', 'event_id', 'contribution_id', 'persons',
'location', 'url', 'category_id', 'category_path', 'start_dt', 'end_dt', 'duration')
subcontribution_id = fields.Int(attribute='id')
type = fields.Constant(SearchTarget.subcontribution.name)
event_id = fields.Int(attribute='contribution.event_id')
persons = NoneRemovingList(fields.Nested(PersonSchema), attribute='person_links')
location = fields.Function(lambda subc: LocationSchema().dump(subc.contribution))
category_id = fields.Int(attribute='event.category_id')
category_path = fields.List(fields.Nested(CategorySchema), attribute='event.detailed_category_chain')
url = fields.Function(lambda subc: url_for('contributions.display_subcontribution', subc, _external=False))
start_dt = fields.DateTime(attribute='contribution.start_dt')
end_dt = fields.DateTime(attribute='contribution.end_dt')
duration = fields.TimeDelta(precision=fields.TimeDelta.MINUTES)
class EventNoteSchema(mm.SQLAlchemyAutoSchema):
class Meta:
model = EventNote
fields = ('note_id', 'type', 'content', 'event_id', 'contribution_id', 'subcontribution_id', 'url',
'category_id', 'category_path', 'modified_dt', 'user')
note_id = fields.Int(attribute='id')
type = fields.Constant(SearchTarget.event_note.name)
content = fields.Str(attribute='current_revision.source')
contribution_id = fields.Method('_contribution_id')
subcontribution_id = fields.Int()
category_id = fields.Int(attribute='event.category_id')
category_path = fields.List(fields.Nested(CategorySchema), attribute='event.detailed_category_chain')
url = fields.Function(lambda note: url_for('event_notes.view', note, _external=False))
modified_dt = fields.DateTime(attribute='current_revision.created_dt')
user = fields.Nested(PersonSchema, attribute='current_revision.user')
def _contribution_id(self, note):
if note.link_type == LinkType.contribution:
return note.contribution_id
elif note.link_type == LinkType.subcontribution:
return note.subcontribution.contribution_id

View File

@ -1,183 +0,0 @@
# This file is part of the Indico plugins.
# Copyright (C) 2002 - 2021 CERN
#
# The Indico plugins are free software; you can redistribute
# them and/or modify them under the terms of the MIT License;
# see the LICENSE file for more details.
from datetime import datetime, timedelta
from io import BytesIO
import pytest
from pytz import utc
from indico.modules.attachments.models.attachments import Attachment, AttachmentFile, AttachmentType
from indico.modules.attachments.models.folders import AttachmentFolder
from indico.modules.events.contributions.models.persons import ContributionPersonLink, SubContributionPersonLink
from indico.modules.events.contributions.models.subcontributions import SubContribution
from indico.modules.events.models.persons import EventPerson
from indico.modules.events.notes.models.notes import EventNote, RenderMode
pytest_plugins = 'indico.modules.events.timetable.testing.fixtures'
@pytest.mark.parametrize('scheduled', (False, True))
def test_dump_contribution(db, dummy_user, dummy_event, dummy_contribution, create_entry, scheduled):
from indico_livesync.export_schemas import ContributionSchema
person = EventPerson.create_from_user(dummy_user, dummy_event)
dummy_contribution.person_links.append(ContributionPersonLink(person=person))
dummy_contribution.description = 'A dummy contribution'
extra = {'start_dt': None, 'end_dt': None}
if scheduled:
create_entry(dummy_contribution, utc.localize(datetime(2020, 4, 20, 4, 20)))
extra = {
'start_dt': dummy_contribution.start_dt.isoformat(),
'end_dt': dummy_contribution.end_dt.isoformat(),
}
db.session.flush()
category_id = dummy_contribution.event.category_id
schema = ContributionSchema()
assert schema.dump(dummy_contribution) == {
'description': 'A dummy contribution',
'location': {'address': '', 'room_name': '', 'venue_name': ''},
'persons': [{'affiliation': None, 'name': 'Guinea Pig'}],
'title': 'Dummy Contribution',
'category_id': category_id,
'category_path': [
{'id': 0, 'title': 'Home', 'url': '/'},
{'id': category_id, 'title': 'dummy', 'url': f'/category/{category_id}/'},
],
'contribution_id': dummy_contribution.id,
'duration': 20,
'event_id': 0,
'type': 'contribution',
'url': f'/event/0/contributions/{dummy_contribution.id}/',
**extra
}
@pytest.mark.parametrize('scheduled', (False, True))
def test_dump_subcontribution(db, dummy_user, dummy_event, dummy_contribution, create_entry, scheduled):
from indico_livesync.export_schemas import SubContributionSchema
extra = {'start_dt': None, 'end_dt': None}
if scheduled:
create_entry(dummy_contribution, utc.localize(datetime(2020, 4, 20, 4, 20)))
extra = {
'start_dt': dummy_contribution.start_dt.isoformat(),
'end_dt': dummy_contribution.end_dt.isoformat(),
}
subcontribution = SubContribution(contribution=dummy_contribution, title='Dummy Subcontribution',
description='A dummy subcontribution',
duration=timedelta(minutes=10))
person = EventPerson.create_from_user(dummy_user, dummy_event)
subcontribution.person_links.append(SubContributionPersonLink(person=person))
db.session.flush()
category_id = dummy_contribution.event.category_id
schema = SubContributionSchema()
assert schema.dump(subcontribution) == {
'description': 'A dummy subcontribution',
'location': {'address': '', 'room_name': '', 'venue_name': ''},
'persons': [{'affiliation': None, 'name': 'Guinea Pig'}],
'title': 'Dummy Subcontribution',
'category_id': category_id,
'category_path': [
{'id': 0, 'title': 'Home', 'url': '/'},
{'id': category_id, 'title': 'dummy', 'url': f'/category/{category_id}/'},
],
'contribution_id': dummy_contribution.id,
'duration': 10,
'event_id': 0,
'subcontribution_id': subcontribution.id,
'type': 'subcontribution',
'url': f'/event/0/contributions/{dummy_contribution.id}/subcontributions/{subcontribution.id}',
**extra
}
def test_dump_attachment(db, dummy_user, dummy_contribution):
from indico_livesync.export_schemas import AttachmentSchema
folder = AttachmentFolder(title='Dummy Folder', description='a dummy folder')
file = AttachmentFile(user=dummy_user, filename='dummy_file.txt', content_type='text/plain')
attachment = Attachment(folder=folder, user=dummy_user, title='Dummy Attachment', type=AttachmentType.file,
file=file)
attachment.folder.object = dummy_contribution
attachment.file.save(BytesIO(b'hello world'))
db.session.flush()
category_id = dummy_contribution.event.category_id
schema = AttachmentSchema()
assert schema.dump(attachment) == {
'filename': 'dummy_file.txt',
'title': 'Dummy Attachment',
'user': {'affiliation': None, 'name': 'Guinea Pig'},
'attachment_id': attachment.id,
'attachment_type': 'file',
'category_id': category_id,
'category_path': [
{'id': 0, 'title': 'Home', 'url': '/'},
{'id': category_id, 'title': 'dummy', 'url': f'/category/{category_id}/'},
],
'contribution_id': dummy_contribution.id,
'subcontribution_id': None,
'event_id': 0,
'folder_id': folder.id,
'modified_dt': attachment.modified_dt.isoformat(),
'type': 'attachment',
'url': (
f'/event/0/contributions/'
f'{dummy_contribution.id}/attachments/{folder.id}/{attachment.id}/dummy_file.txt'
),
}
@pytest.mark.parametrize('link_type', ('event', 'contrib', 'subcontrib'))
def test_dump_event_note(db, dummy_user, dummy_event, dummy_contribution, link_type):
from indico_livesync.export_schemas import EventNoteSchema
if link_type == 'event':
ids = {'contribution_id': None, 'subcontribution_id': None}
note = EventNote(object=dummy_event)
url = '/event/0/note/'
elif link_type == 'contrib':
ids = {'contribution_id': dummy_contribution.id, 'subcontribution_id': None}
note = EventNote(object=dummy_contribution)
url = f'/event/0/contributions/{dummy_contribution.id}/note/'
elif link_type == 'subcontrib':
subcontribution = SubContribution(contribution=dummy_contribution, title='Dummy Subcontribution',
duration=timedelta(minutes=10))
db.session.flush()
ids = {
'contribution_id': subcontribution.contribution_id,
'subcontribution_id': subcontribution.id,
}
note = EventNote(object=subcontribution)
url = f'/event/0/contributions/{dummy_contribution.id}/subcontributions/{subcontribution.id}/note/'
note.create_revision(RenderMode.html, 'this is a dummy note', dummy_user)
db.session.flush()
category_id = dummy_event.category_id
schema = EventNoteSchema()
assert schema.dump(note) == {
'content': 'this is a dummy note',
'user': {'affiliation': None, 'name': 'Guinea Pig'},
'category_id': category_id,
'category_path': [
{'id': 0, 'title': 'Home', 'url': '/'},
{'id': category_id, 'title': 'dummy', 'url': f'/category/{category_id}/'},
],
'modified_dt': note.current_revision.created_dt.isoformat(),
'event_id': 0,
'note_id': note.id,
'type': 'event_note',
'url': url,
**ids
}

View File

@ -12,11 +12,11 @@ from pygments import highlight
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.lexers.agile import Python3Lexer
from indico.modules.search.schemas import EventSchema
from indico.modules.search.schemas import (AttachmentSchema, ContributionSchema, EventNoteSchema, EventSchema,
SubContributionSchema)
from indico.util.console import cformat
from indico_livesync import LiveSyncBackendBase, SimpleChange, Uploader, process_records
from indico_livesync.export_schemas import AttachmentSchema, ContributionSchema, EventNoteSchema, SubContributionSchema
lexer = Python3Lexer()