Projet

Général

Profil

0002-atal-add-new-comments-endpoint-41533.patch

Emmanuel Cazenave, 09 avril 2020 16:00

Télécharger (5,59 ko)

Voir les différences:

Subject: [PATCH 2/2] atal: add 'new-comments' endpoint (#41533)

 passerelle/apps/atal/models.py | 55 +++++++++++++++++++++++++++++++++-
 tests/test_atal.py             | 51 +++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+), 1 deletion(-)
passerelle/apps/atal/models.py
18 18
import binascii
19 19

  
20 20
from django.db import models
21
from django.utils import dateformat
21
from django.utils import dateformat, dateparse
22 22
from django.utils.encoding import force_text
23 23
from django.utils.six.moves import urllib
24 24
from django.utils.translation import ugettext_lazy as _
......
305 305
            **data
306 306
        )
307 307
        return {}
308

  
309
    @endpoint(
310
        methods=['get'], perm='can_access', example_pattern='{demande_number}/',
311
        pattern='^(?P<demande_number>\w+)/$', name='new-comments',
312
        parameters={
313
            'demande_number': DEMANDE_NUMBER_PARAM,
314
        }
315
    )
316
    def new_comments(self, request, demande_number, last_datetime):
317

  
318
        def issup(datetime1, datetime2):
319
            if datetime1.tzinfo is None or datetime2.tzinfo is None:
320
                datetime1 = datetime1.replace(tzinfo=None)
321
                datetime2 = datetime2.replace(tzinfo=None)
322
            return datetime1 > datetime2
323

  
324
        last_datetime = dateparse.parse_datetime(last_datetime)
325
        if last_datetime is None:
326
            raise APIError('Wrong datetime format')
327

  
328
        demand_details = helpers.serialize_object(
329
            self._soap_call(
330
                wsdl='DemandeService', method='retrieveDetailsDemande',
331
                demandeNumberParam=demande_number)
332
        )
333
        if not demand_details:
334
            raise APIError('Could not get comments')
335

  
336
        new_comments, all_comments = [], []
337
        responses = (demand_details.get('reponses') or {}).get('Reponse') or []
338
        if responses:
339
            for response in responses:
340
                comment = {
341
                    'text': response.get('commentaires'),
342
                    'date': None,
343
                    'date_raw': None
344
                }
345
                dateobj = None
346
                if 'dateReponse' in response:
347
                    dateobj = response['dateReponse']
348
                    comment['date'] = dateformat.format(dateobj, DATE_FORMAT)
349
                    comment['date_raw'] = dateformat.format(dateobj, 'c')
350

  
351
                all_comments.append(comment)
352
                if dateobj and issup(dateobj, last_datetime):
353
                    new_comments.append(comment)
354

  
355
        return {
356
            'data': {
357
                'new_comments': new_comments,
358
                'all_comments': all_comments
359
            }
360
        }
tests/test_atal.py
1 1
# coding: utf-8
2 2

  
3 3
import base64
4
from datetime import datetime
4 5
import os
5 6

  
6 7
from django.contrib.contenttypes.models import ContentType
8
from django.utils.http import urlencode
7 9
import mock
8 10
import pytest
9 11

  
......
323 325
    }
324 326
    assert data['works_comments'][1] == last_comment
325 327
    assert data['works_comment'] == last_comment
328

  
329

  
330
def test_new_comments(app, connector, monkeypatch):
331
    import passerelle.utils
332

  
333
    wsdl_response = mock.Mock(
334
        content=get_file('DemandeService.wsdl'), status_code=200,
335
        headers={'Content-Type': 'text/xml'}
336
    )
337
    monkeypatch.setattr(passerelle.utils.Request, 'get', mock.Mock(return_value=wsdl_response))
338

  
339
    api_response = mock.Mock(
340
        content=get_file('details_demande_response_with_comments.xml'), status_code=200,
341
        headers={'Content-Type': 'text/xml'}
342
    )
343
    monkeypatch.setattr(passerelle.utils.Request, 'post', mock.Mock(return_value=api_response))
344

  
345
    all_comments = [
346
        {
347
            'text': 'OK',
348
            'date': 'Thursday 24 October 2019, 16:48',
349
            'date_raw': '2019-10-24T16:48:34+02:00'
350
        }, {
351
            'text': 'bonjour atal',
352
            'date': 'Thursday 24 October 2019, 16:51',
353
            'date_raw': '2019-10-24T16:51:37+02:00'
354
        }
355
    ]
356

  
357
    last_datetime = datetime(year=2019, month=10, day=23)
358
    params = urlencode({'last_datetime': last_datetime})
359
    response = app.get('/atal/slug-atal/new-comments/DIT18050001/?%s' % params)
360
    assert response.json['err'] == 0
361
    assert response.json['data']['new_comments'] == all_comments
362
    assert response.json['data']['all_comments'] == all_comments
363

  
364
    last_datetime = datetime(year=2019, month=10, day=24, hour=16, minute=49)
365
    params = urlencode({'last_datetime': last_datetime})
366
    response = app.get('/atal/slug-atal/new-comments/DIT18050001/?%s' % params)
367
    assert response.json['err'] == 0
368
    assert response.json['data']['new_comments'] == all_comments[1:]
369
    assert response.json['data']['all_comments'] == all_comments
370

  
371
    last_datetime = datetime(year=2019, month=10, day=24, hour=16, minute=52)
372
    params = urlencode({'last_datetime': last_datetime})
373
    response = app.get('/atal/slug-atal/new-comments/DIT18050001/?%s' % params)
374
    assert response.json['err'] == 0
375
    assert response.json['data']['new_comments'] == []
376
    assert response.json['data']['all_comments'] == all_comments
326
-