misc: move carddef data source support to carddef module (#44155)

This commit is contained in:
Frédéric Péters 2020-08-22 21:31:09 +02:00
parent d8cd485379
commit 4f8903b6e9
2 changed files with 24 additions and 14 deletions

View File

@ -18,7 +18,7 @@ import sys
import types
from quixote import get_publisher
from .qommon import _, N_
from .qommon import _, N_, misc
from wcs.carddata import CardData
from wcs.formdef import FormDef
@ -134,3 +134,24 @@ class CardDef(FormDef):
def store(self, comment=None):
self.roles = self.backoffice_submission_roles
return super().store(comment=comment)
@classmethod
def get_as_data_source_options(cls):
for carddef in cls.select(lightweight=True, ignore_errors=True, order_by='name'):
if not carddef.digest_template:
continue
data_source_id = 'carddef:%s' % carddef.url_name
yield (data_source_id, carddef.name, data_source_id)
@classmethod
def get_data_source_items(cls, data_source_id):
assert data_source_id.startswith('carddef:')
try:
carddef = cls.get_by_urlname(data_source_id[8:])
except KeyError:
return []
items = [x.get_data_source_structured_item()
for x in carddef.data_class().select()
if not x.is_draft()]
items.sort(key=lambda x: misc.simplify(x['text']))
return items

View File

@ -62,10 +62,7 @@ class DataSourceSelectionWidget(CompositeWidget):
if allow_named_sources:
options.extend([(x.slug, x.name, x.slug) for x in NamedDataSource.select()])
from wcs.carddef import CardDef
options.extend([
('carddef:%s' % x.url_name, x.name, 'carddef:%s' % x.url_name)
for x in CardDef.select(lightweight=True, ignore_errors=True)
if x.digest_template])
options.extend(list(CardDef.get_as_data_source_options()))
options.sort(key=lambda x: misc.simplify(x[1]))
options.insert(0, (None, _('None'), None))
@ -178,15 +175,7 @@ def get_structured_items(data_source, mode=None):
if data_source.get('type') and data_source.get('type').startswith('carddef:'):
# cards
from wcs.carddef import CardDef
try:
carddef = CardDef.get_by_urlname(data_source['type'][8:])
except KeyError:
return []
items = [x.get_data_source_structured_item()
for x in carddef.data_class().select()
if not x.is_draft()]
items.sort(key=lambda x: misc.simplify(x['text']))
return items
return CardDef.get_data_source_items(data_source['type'])
if data_source.get('type') not in ('json', 'jsonp', 'geojson', 'formula'):
# named data source