Projet

Général

Profil

0001-backoffice-list-datasources-from-carddefs-47849.patch

Lauréline Guérin, 05 novembre 2020 14:27

Télécharger (6,82 ko)

Voir les différences:

Subject: [PATCH] backoffice: list datasources from carddefs (#47849)

 tests/admin_pages/test_datasource.py          | 31 +++++++++++++++++++
 wcs/admin/data_sources.py                     |  6 +++-
 wcs/carddef.py                                | 14 ++++-----
 wcs/data_sources.py                           |  2 +-
 .../wcs/backoffice/data-sources.html          | 20 +++++++++++-
 5 files changed, 63 insertions(+), 10 deletions(-)
tests/admin_pages/test_datasource.py
65 65
    app.get('/backoffice/workflows/data-sources/42/', status=404)
66 66

  
67 67

  
68
def test_data_sources_from_carddefs(pub):
69
    create_superuser(pub)
70
    CardDef.wipe()
71
    pub.custom_view_class.wipe()
72

  
73
    app = login(get_app(pub))
74
    resp = app.get('/backoffice/settings/data-sources/')
75
    assert 'Data Sources from Card Models' in resp.text
76
    assert 'There are no data sources from card models.' in resp.text
77

  
78
    carddef = CardDef()
79
    carddef.name = 'foo'
80
    carddef.fields = []
81
    carddef.digest_template = 'foo bar'
82
    carddef.store()
83

  
84
    custom_view = pub.custom_view_class()
85
    custom_view.title = 'datasource card view'
86
    custom_view.formdef = carddef
87
    custom_view.columns = {'list': [{'id': 'id'}]}
88
    custom_view.filters = {}
89
    custom_view.visibility = 'datasource'
90
    custom_view.store()
91

  
92
    resp = app.get('/backoffice/settings/data-sources/')
93
    assert 'Data Sources from Card Models' in resp.text
94
    assert 'There are no data sources from card models.' not in resp.text
95
    assert '<li><a href="http://example.net/backoffice/data/foo/">foo</a></li>' in resp.text
96
    assert '<li><a href="http://example.net/backoffice/data/foo/datasource-card-view/">foo - datasource card view</a></li>' in resp.text
97

  
98

  
68 99
def test_data_sources_new(pub):
69 100
    create_superuser(pub)
70 101
    NamedDataSource.wipe()
wcs/admin/data_sources.py
30 30
from wcs.qommon.form import get_session
31 31
from wcs.qommon import misc
32 32
from wcs.qommon.backoffice.menu import html_top
33
from wcs.carddef import CardDef
33 34
from wcs.data_sources import (NamedDataSource, DataSourceSelectionWidget,
34 35
        get_structured_items)
35 36
from wcs.formdef import FormDef, get_formdefs_of_all_kinds
......
326 327

  
327 328
    def _q_index(self):
328 329
        html_top('datasources', title=_('Data Sources'))
330
        generated_data_sources = list(CardDef.get_carddefs_as_data_source())
331
        generated_data_sources.sort(key=lambda x: misc.simplify(x[1]))
329 332
        return template.QommonTemplateResponse(
330 333
                templates=['wcs/backoffice/data-sources.html'],
331
                context={'data_sources': NamedDataSource.select(order_by='name')})
334
                context={'data_sources': NamedDataSource.select(order_by='name'),
335
                         'generated_data_sources': generated_data_sources})
332 336

  
333 337
    def new(self):
334 338
        get_response().breadcrumb.append( ('new', _('New')) )
wcs/carddef.py
138 138
        return super().store(comment=comment)
139 139

  
140 140
    @classmethod
141
    def get_as_data_source_options(cls):
142
        carddefs = {}
141
    def get_carddefs_as_data_source(cls):
142
        carddefs_by_id = {}
143 143
        for carddef in cls.select(lightweight=True, ignore_errors=True, order_by='name'):
144 144
            if not carddef.digest_template:
145 145
                continue
146 146
            data_source_id = 'carddef:%s' % carddef.url_name
147
            carddefs[carddef.id] = {'url_name': carddef.url_name, 'name': carddef.name}
148
            yield (data_source_id, carddef.name, data_source_id)
147
            carddefs_by_id[carddef.id] = carddef
148
            yield (carddef, carddef.name, data_source_id, None)
149 149
        clauses = [Equal('formdef_type', 'carddef'), Equal('visibility', 'datasource')]
150 150
        for custom_view in get_publisher().custom_view_class.select(clauses):
151
            carddef = carddefs.get(custom_view.formdef_id)
151
            carddef = carddefs_by_id.get(custom_view.formdef_id)
152 152
            if not carddef:
153 153
                continue
154
            data_source_id = 'carddef:%s:%s' % (carddef['url_name'], custom_view.slug)
155
            yield (data_source_id, '%s - %s' % (carddef['name'], custom_view.title), data_source_id)
154
            data_source_id = 'carddef:%s:%s' % (carddef.url_name, custom_view.slug)
155
            yield (carddef, '%s - %s' % (carddef.name, custom_view.title), data_source_id, custom_view)
156 156

  
157 157
    @classmethod
158 158
    def get_data_source_custom_view(cls, data_source_id):
wcs/data_sources.py
62 62
        if allow_named_sources:
63 63
            options.extend([(x.slug, x.name, x.slug) for x in NamedDataSource.select()])
64 64
            from wcs.carddef import CardDef
65
            options.extend(list(CardDef.get_as_data_source_options()))
65
            options.extend([(t[2], t[1], t[2]) for t in CardDef.get_carddefs_as_data_source()])
66 66
            options.sort(key=lambda x: misc.simplify(x[1]))
67 67

  
68 68
        options.insert(0, (None, _('None'), None))
wcs/templates/wcs/backoffice/data-sources.html
9 9
{% endblock %}
10 10

  
11 11
{% block content %}
12
<div class="section">
13
<h2>{% trans "Data Sources from Card Models" %}</h2>
14
{% if generated_data_sources %}
15
<ul class="objects-list single-links">
16
  {% for data_source in generated_data_sources %}
17
  <li><a href="{{ data_source.0.get_url }}{% if data_source.3 %}{{ data_source.3.get_url_slug }}/{% endif %}">{{ data_source.1 }}</a></li>
18
  {% endfor %}
19
</ul>
20
{% else %}
21
<div>
22
{% trans "There are no data sources from card models." %}
23
</div>
24
{% endif %}
25
</div>
26

  
27
<div class="section">
28
<h2>{% trans "Manually Configured Data Sources" %}</h2>
12 29
{% if data_sources %}
13 30
<ul class="objects-list single-links">
14 31
  {% for data_source in data_sources %}
......
16 33
  {% endfor %}
17 34
</ul>
18 35
{% else %}
19
<div class="infonotice">
36
<div>
20 37
{% trans "There are no data sources defined." %}
21 38
</div>
22 39
{% endif %}
40
</div>
23 41
{% endblock %}
24
-