Skip to content

Commit

Permalink
WIP make it configurable per event
Browse files Browse the repository at this point in the history
  • Loading branch information
duartegalvao committed Feb 15, 2024
1 parent 6a61c3a commit 2011004
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ def upgrade():
schema='plugin_jacow'
)

# Populate tables with the current affiliations
for target, source in (('abstract_affiliations', 'event_abstracts.abstract_person_links'),
('contribution_affiliations', 'events.contribution_person_links')):
op.execute(f'''
INSERT INTO plugin_jacow.{target} (person_link_id, affiliation_id, display_order)
SELECT pl.id, COALESCE(pl.affiliation_id, ep.affiliation_id), 0 FROM {source} pl
JOIN events.persons ep ON (ep.id = pl.person_id)
WHERE COALESCE(pl.affiliation_id, ep.affiliation_id) IS NOT NULL
''') # noqa: S608


def downgrade():
op.drop_table('contribution_affiliations', schema='plugin_jacow')
Expand Down
63 changes: 52 additions & 11 deletions indico_jacow/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from indico.modules.events.contributions.models.persons import ContributionPersonLink
from indico.modules.events.contributions.views import WPContributions, WPManageContributions, WPMyContributions
from indico.modules.events.layout.util import MenuEntryData
from indico.modules.events.models.persons import EventPerson
from indico.modules.events.persons.forms import ManagePersonListsForm
from indico.modules.events.persons.schemas import PersonLinkSchema
from indico.util.i18n import _
from indico.web.forms.base import IndicoForm
Expand All @@ -45,13 +47,19 @@ class JACOWPlugin(IndicoPlugin):
default_settings = {
'sync_enabled': False,
}
default_event_settings = {
'multiple_affiliations': False,
}

def init(self):
super().init()
self.template_hook('abstract-list-options', self._inject_abstract_export_button)
self.template_hook('contribution-list-options', self._inject_contribution_export_button)
self.template_hook('custom-affiliation', self._inject_custom_affiliation)
self.connect(signals.core.form_validated, self._form_validated)
self.connect(signals.core.add_form_fields, self._add_person_lists_settings, sender=ManagePersonListsForm)
self.connect(signals.core.form_validated, self._person_lists_form_validated)
self.connect(signals.core.form_validated, self._submission_form_validated, sender=(AbstractForm,
ContributionForm))
self.connect(signals.event.abstract_accepted, self._abstract_accepted)
self.connect(signals.event.hide_affiliation_field, self._hide_affiliation_field)
self.connect(signals.event.sidemenu, self._extend_event_menu)
Expand All @@ -72,19 +80,52 @@ def _inject_contribution_export_button(self, event=None):
csv_url=url_for_plugin('jacow.contributions_csv_export_custom', event),
xlsx_url=url_for_plugin('jacow.contributions_xlsx_export_custom', event))

def _inject_custom_affiliation(self, person=None):
if isinstance(person, (AbstractPersonLink, ContributionPersonLink)):
def _inject_custom_affiliation(self, person):
if (self.event_settings.get(person.event, 'multiple_affiliations') and
isinstance(person, (AbstractPersonLink, ContributionPersonLink))):
return render_plugin_template('custom_affiliation.html', person=person)

def _form_validated(self, form, **kwargs):
if isinstance(form, ContributionForm):
person_links = form.person_link_data.data
affiliations_cls = ContributionAffiliation
elif isinstance(form, AbstractForm):
def _add_person_lists_settings(self, form_cls, form_kwargs, **kwargs):
return (
'multiple_affiliations',
BooleanField(_('Multiple affiliations'), widget=SwitchWidget(),
description=_('Gives submitters the ability to list multiple affiliations per author in '
'abstracts and contributions.'),
default=self.event_settings.get(g.rh.event, 'multiple_affiliations'))
)

def _person_lists_form_validated(self, form, **kwargs):
if not isinstance(form, ManagePersonListsForm):
return
self.event_settings.set(g.rh.event, 'multiple_affiliations', form.ext__multiple_affiliations.data)
if not form.ext__multiple_affiliations.data:
return
# Populate tables with the current affiliations
for target, source in ((AbstractAffiliation, AbstractPersonLink),
(ContributionAffiliation, ContributionPersonLink)):
affiliation_id = db.func.coalesce(source.affiliation_id, EventPerson.affiliation_id)
db.session.execute(
target.__table__.insert().from_select(
['person_link_id', 'affiliation_id', 'display_order'],
db.select([source.id, affiliation_id, 0])
.join(source.person)
.filter(
affiliation_id.isnot(None),
EventPerson.event == g.rh.event,
~source.id.in_(db.select([target.person_link_id]))
)
)
)

def _submission_form_validated(self, form, **kwargs):
if not self.event_settings.get(form.event, 'multiple_affiliations'):
return
if isinstance(form, AbstractForm):
person_links = form.person_links.data
affiliations_cls = AbstractAffiliation
else:
return
person_links = form.person_link_data.data
affiliations_cls = ContributionAffiliation
affiliations_ids = g.pop('jacow_affiliations_ids', {})
for person_link in person_links:
person_affiliations = affiliations_ids.get(person_link.person.email, [])
Expand All @@ -102,8 +143,8 @@ def _abstract_accepted(self, abstract, contribution, **kwargs):
for ja in abstract_person.jacow_affiliations]
db.session.flush()

def _hide_affiliation_field(self, sender, **kwargs):
return True
def _hide_affiliation_field(self, event, **kwargs):
return self.event_settings.get(event, 'multiple_affiliations')

def _extend_event_menu(self, sender, **kwargs):
def _statistics_visible(event):
Expand Down

0 comments on commit 2011004

Please sign in to comment.