Projet

Général

Profil

0001-maelis-add-cache-on-family-data-48477.patch

Nicolas Roche, 25 novembre 2020 11:02

Télécharger (3,48 ko)

Voir les différences:

Subject: [PATCH] maelis: add cache on family data (#48477)

 passerelle/apps/maelis/models.py | 7 +++++++
 tests/test_maelis.py             | 6 ++++++
 2 files changed, 13 insertions(+)
passerelle/apps/maelis/models.py
18 18
from collections import defaultdict
19 19

  
20 20
from urllib.parse import urljoin
21 21

  
22 22
import zeep
23 23
from zeep.wsse.username import UsernameToken
24 24
from zeep.helpers import serialize_object
25 25

  
26
from django.core.cache import cache
26 27
from django.db import models
27 28
from django.utils import timezone
28 29

  
29 30
from django.utils.translation import ugettext_lazy as _
30 31

  
31 32
from passerelle.base.models import BaseResource
32 33
from passerelle.utils.api import endpoint
33 34
from passerelle.utils.jsonresponse import APIError
......
133 134
            return self.link_set.get(name_id=name_id)
134 135
        except Link.DoesNotExist:
135 136
            raise APIError('User not linked to family', err_code='not-found')
136 137

  
137 138
    def get_family_data(self, family_id, school_year=None):
138 139
        if not school_year:
139 140
            # fallback to current year if not provided
140 141
            school_year = utils.get_default_school_year()
142
        cache_key = 'maelis-%s-%s' % (family_id, school_year)
143
        family_data = cache.get(cache_key)
144
        if family_data:
145
            return family_data
146

  
141 147
        family_data = serialize_object(self.call('FamilyService?wsdl',
142 148
                                                 'readFamily',
143 149
                                                 dossierNumber=family_id,
144 150
                                                 schoolYear=school_year))
145 151
        for child in family_data['childInfoList']:
146 152
            utils.normalize_person(child)
153
        cache.set(cache_key, family_data, 3600)
147 154
        return family_data
148 155

  
149 156
    def get_invoices(self, regie_id, name_id):
150 157
        family_id = self.get_link(name_id).family_id
151 158
        return [utils.normalize_invoice(i) for i in self.call(
152 159
            'InvoiceService?wsdl', 'readInvoices',
153 160
            numDossier=family_id, codeRegie=regie_id)]
154 161

  
tests/test_maelis.py
87 87
                                         status_code=200,
88 88
                                         headers={'Content-Type': 'text/xml'})
89 89
    assert Link.objects.count() == 0
90 90
    Link.objects.create(resource=connector, family_id='3264', name_id='local')
91 91
    resp = app.get('/maelis/test/family-info?NameID=local')
92 92
    assert resp.json['data']["number"] == 3264
93 93
    assert resp.json['data']['rl1InfoBean']
94 94
    assert resp.json['data']['childInfoList']
95
    family_data = resp.json['data']
96
    assert mocked_post.call_count == 1
97

  
98
    resp = app.get('/maelis/test/family-info?NameID=local')
99
    assert family_data == resp.json['data']
100
    assert mocked_post.call_count == 1
95 101

  
96 102
    resp = app.get('/maelis/test/children-info?NameID=local')
97 103
    assert resp.json['data']
98 104
    for child in resp.json['data']:
99 105
        assert child['id']
100 106
        assert child['text']
101 107

  
102 108
    resp = app.get('/maelis/test/child-info?NameID=local&childID=21293')
103
-