Projet

Général

Profil

0001-general-add-global-access-to-cards-data-43328.patch

Frédéric Péters, 26 mai 2020 14:43

Télécharger (3,9 ko)

Voir les différences:

Subject: [PATCH] general: add global access to cards data (#43328)

 wcs/qommon/substitution.py        |  3 ++-
 wcs/qommon/templatetags/qommon.py |  5 +++++
 wcs/variables.py                  | 32 ++++++++++++++++++++++++++++++-
 3 files changed, 38 insertions(+), 2 deletions(-)
wcs/qommon/substitution.py
41 41

  
42 42
    def set_empty(self):
43 43
        from wcs.data_sources import NamedDataSource
44
        from wcs.variables import CardsSource
44 45
        from wcs.wscalls import NamedWsCall
45 46
        from wcs.scripts import Script
46
        self.sources = [NamedDataSource, NamedWsCall, Script]
47
        self.sources = [NamedDataSource, NamedWsCall, CardsSource, Script]
47 48

  
48 49
    @classmethod
49 50
    def register(cls, varname, category=None, comment=None):
wcs/qommon/templatetags/qommon.py
429 429
    return queryset.order_by(attribute)
430 430

  
431 431

  
432
@register.filter
433
def filter_value(queryset, value):
434
    return queryset.apply_partial_filter(value)
435

  
436

  
432 437
@register.filter
433 438
def reproj(coords, projection_name):
434 439
    proj = pyproj.Proj(init='EPSG:4326')
wcs/variables.py
97 97
            return bool(obj._distance < distance)
98 98
        return self._clone(self._criterias + [distance_check])
99 99

  
100
    def apply_partial_filter(self, value):
101
        assert self.pending_attr
102
        for field in self._formdef.fields:
103
            if getattr(field, 'varname', None) == self.pending_attr:
104
                from wcs import sql
105
                criteria = Equal(sql.get_field_id(field), value)
106
                break
107
        else:
108
            raise Exception('invalid filter')
109
        return self._clone(self._criterias + [criteria])
110

  
100 111
    def __getattr__(self, attribute):
101
        # backward compatibility
102 112
        if attribute.startswith('count_status_'):
113
            # backward compatibility
103 114
            status = attribute[len('count_status_'):]
104 115
            return len(self._formdef.data_class().get_ids_with_indexed_value(
105 116
                'status', 'wf-%s' % status))
117
        if attribute.startswith('filter_by_'):
118
            # filter by attribute
119
            qs = self._clone(self._criterias)
120
            qs.pending_attr = attribute[len('filter_by_'):]
121
            return qs
106 122
        if attribute == 'formdef':
107 123
            warnings.warn('Deprecated access to formdef', DeprecationWarning)
108 124
            return self._formdef
......
430 446
        # this gets used to generate an email attachment :/
431 447
        return self._formdata.export_to_json(include_files=include_files)
432 448

  
449
    def get(self, key):
450
        # compatibility with |get filter, to return a field by varname
451
        return getattr(self.var, key, None)
452

  
433 453
    def __getitem__(self, key):
434 454
        try:
435 455
            return getattr(self, key)
......
867 887
            field.id = field.varname or field.id
868 888
        data = self._formdef.workflow_options
869 889
        super(LazyFormDefOptions, self).__init__(fields, data)
890

  
891

  
892
class CardsSource:
893
    @classmethod
894
    def get_substitution_variables(cls):
895
        return {'cards': cls()}
896

  
897
    def __getattr__(self, attr):
898
        from wcs.carddef import CardDef
899
        return LazyFormDef(CardDef.get_by_urlname(attr))
870
-