Projet

Général

Profil

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

Emmanuel Cazenave, 13 avril 2020 18:11

Télécharger (5,94 ko)

Voir les différences:

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

 passerelle/apps/atal/models.py | 56 +++++++++++++++++++++++++++++++++-
 tests/test_atal.py             | 54 ++++++++++++++++++++++++++++++++
 2 files changed, 109 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=None):
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
        if last_datetime is not None:
325
            last_datetime = dateparse.parse_datetime(last_datetime)
326
            if last_datetime is None:
327
                raise APIError('Wrong datetime format', http_status=400)
328

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

  
337
        new_comments, all_comments, last_date = [], [], None
338
        responses = (demand_details.get('reponses') or {}).get('Reponse') or []
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
                last_date = comment['date_raw']
351

  
352
            all_comments.append(comment)
353
            if dateobj and issup(dateobj, last_datetime) or last_datetime is None:
354
                new_comments.append(comment)
355
        return {
356
            'data': {
357
                'new_comments': new_comments,
358
                'all_comments': all_comments,
359
                'last_date': last_date
360
            }
361
        }
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
    assert response.json['data']['last_date'] == '2019-10-24T16:51:37+02:00'
364

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

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