Projet

Général

Profil

0001-planitec-expect-a-dict-in-custom_fields-50549.patch

Emmanuel Cazenave, 26 janvier 2021 13:15

Télécharger (3,6 ko)

Voir les différences:

Subject: [PATCH] planitec: expect a dict in custom_fields (#50549)

 .../migrations/0007_custom_fields_dict.py     | 20 +++++++++++++++++++
 passerelle/contrib/planitech/models.py        |  9 +++++++--
 tests/test_planitech.py                       |  2 +-
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 passerelle/contrib/planitech/migrations/0007_custom_fields_dict.py
passerelle/contrib/planitech/migrations/0007_custom_fields_dict.py
1
from django.db import migrations
2

  
3

  
4
def list_to_dict(apps, schema_editor):
5
    PlanitechConnector = apps.get_model('planitech', 'PlanitechConnector')
6
    for conn in PlanitechConnector.objects.all():
7
        if conn.custom_fields and isinstance(conn.custom_fields, list):
8
            conn.custom_fields = {'places': conn.custom_fields}
9
            conn.save()
10

  
11

  
12
class Migration(migrations.Migration):
13

  
14
    dependencies = [
15
        ('planitech', '0006_text_to_jsonb'),
16
    ]
17

  
18
    operations = [
19
        migrations.RunPython(list_to_dict),
20
    ]
passerelle/contrib/planitech/models.py
306 306
            raise APIError(error_msg)
307 307
        return mste.decode(response.json())
308 308

  
309
    def _get_places_fields(self):
310
        if self.custom_fields:
311
            return self.custom_fields.get('places', [])
312
        return []
313

  
309 314
    def _raw_get_places_referential(self, refresh_cache=False):
310 315
        cache_key = 'planitech-%s-places' % self.id
311 316
        ref = cache.get(cache_key)
......
325 330
                }
326 331
            }
327 332

  
328
            for custom_field in self.custom_fields or []:
333
            for custom_field in self._get_places_fields():
329 334
                field_name = custom_field['name']
330 335
                extensionAttributes[field_name] = {
331 336
                    'name': field_name,
......
380 385
            # Filter on custom fields
381 386
            skip = False
382 387
            for filter_name, filter_value in kwargs.items():
383
                for field in self.custom_fields or []:
388
                for field in self._get_places_fields():
384 389
                    if filter_name == field['name']:
385 390
                        if field['type'] == 'int':
386 391
                            filter_value = int(filter_value)
tests/test_planitech.py
75 75
    connector = PlanitechConnector.objects.create(
76 76
        url='http://example.planitech.com/', username='admin', password='admin',
77 77
        verify_cert=False, slug='slug-planitech',
78
        custom_fields=[{'name': 'some_custom_field', 'type': 'string'}], price_code='T1')
78
        custom_fields={'places': [{'name': 'some_custom_field', 'type': 'string'}]}, price_code='T1')
79 79
    obj_type = ContentType.objects.get_for_model(connector)
80 80
    AccessRight.objects.create(
81 81
        codename='can_access', apiuser=api, resource_type=obj_type, resource_pk=connector.pk)
82
-