0001-toulouse-axel-endpoint-to-return-the-list-of-remaini.patch
passerelle/contrib/toulouse_axel/models.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
1 | 2 |
# passerelle - uniform access to multiple data sources and services |
2 | 3 |
# Copyright (C) 2020 Entr'ouvert |
3 | 4 |
# |
... | ... | |
14 | 15 |
# You should have received a copy of the GNU Affero General Public License |
15 | 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 17 | |
18 |
from __future__ import unicode_literals |
|
19 | ||
17 | 20 |
import base64 |
18 | 21 |
import copy |
19 | 22 |
import datetime |
... | ... | |
26 | 29 |
from django.core.cache import cache |
27 | 30 |
from django.db import models |
28 | 31 |
from django.http import HttpResponse |
32 |
from django.utils import dateformat |
|
29 | 33 |
from django.utils.dates import WEEKDAYS |
30 | 34 |
from django.utils.encoding import force_text |
31 | 35 |
from django.utils.translation import ugettext_lazy as _ |
... | ... | |
1128 | 1132 |
activity['id'] = activity['IDACTIVITE'] |
1129 | 1133 |
start_date = datetime.datetime.strptime(activity['DATEENTREE'], utils.json_date_format) |
1130 | 1134 |
end_date = datetime.datetime.strptime(activity['DATESORTIE'], utils.json_date_format) |
1131 |
activity['text'] = u'{} (inscription du {} au {})'.format(
|
|
1135 |
activity['text'] = '{} (inscription du {} au {})'.format( |
|
1132 | 1136 |
activity['LIBELLEACTIVITE'], |
1133 | 1137 |
start_date.strftime(utils.xml_date_format), |
1134 | 1138 |
end_date.strftime(utils.xml_date_format)) |
... | ... | |
1231 | 1235 |
possible_days = self.clae_booking_activity_possible_days(request, NameID, idpersonne, activity_type, booking_date) |
1232 | 1236 |
return {'data': [d['id'] for d in possible_days['data'] if d['prefill'] is True]} |
1233 | 1237 | |
1238 |
@endpoint( |
|
1239 |
description=_("Get list of next weeks for the current reference year"), |
|
1240 |
perm='can_access') |
|
1241 |
def clae_next_weeks(self, request): |
|
1242 |
today = datetime.date.today() |
|
1243 |
current_reference_year = utils.get_reference_year_from_date(today) |
|
1244 |
# starts in 8 days |
|
1245 |
start_date = today + datetime.timedelta(days=8) |
|
1246 |
# but if start_date is a saturday or a sunday, jump to the next monday |
|
1247 |
if start_date.weekday() > 4: |
|
1248 |
start_date = start_date + datetime.timedelta(days=7 - start_date.weekday()) |
|
1249 |
end_date = datetime.date(current_reference_year + 1, 7, 31) |
|
1250 |
weeks = [] |
|
1251 |
while start_date <= end_date: |
|
1252 |
week_end_date = min(end_date, start_date + datetime.timedelta(days=4 - start_date.weekday())) |
|
1253 |
weeks.append({ |
|
1254 |
'id': 'week%s-week%s' % (start_date.strftime('%Y%m%d'), week_end_date.strftime('%Y%m%d')), |
|
1255 |
'text': '%s → %s' % ( |
|
1256 |
dateformat.format(start_date, 'l j F' if start_date.year == week_end_date.year else 'l j F Y'), |
|
1257 |
dateformat.format(week_end_date, 'l j F Y')), |
|
1258 |
'date_debut': start_date.strftime(utils.json_date_format), |
|
1259 |
'date_fin': week_end_date.strftime(utils.json_date_format), |
|
1260 |
'annee_reference': str(current_reference_year), |
|
1261 |
}) |
|
1262 |
# find next monday |
|
1263 |
start_date = start_date + datetime.timedelta(days=7 - start_date.weekday()) |
|
1264 |
return {'data': weeks} |
|
1265 | ||
1234 | 1266 |
@endpoint( |
1235 | 1267 |
description=_("CLAE/Cantine booking"), |
1236 | 1268 |
perm='can_access', |
tests/test_toulouse_axel.py | ||
---|---|---|
2811 | 2811 |
assert resource.are_children_registered(dui='XXX', reference_year=get_reference_year_from_date(today)) == expected |
2812 | 2812 | |
2813 | 2813 | |
2814 |
@pytest.mark.parametrize('today, first_day, last_day', [ |
|
2815 |
# first day of the period |
|
2816 |
('2019-08-01', '2019-08-09', '2019-08-09'), |
|
2817 |
# last day of the period |
|
2818 |
('2020-07-31', None, None), |
|
2819 |
# today+8 is a friday => starts friday, ends friday |
|
2820 |
('2020-03-26', '2020-04-03', '2020-04-03'), |
|
2821 |
# today+8 is a saturday => starts on next monday, ends next friday |
|
2822 |
('2020-03-27', '2020-04-06', '2020-04-10'), |
|
2823 |
# today+8 is a sunday => starts on next monday, ends next friday |
|
2824 |
('2020-03-28', '2020-04-06', '2020-04-10'), |
|
2825 |
# today+8 is a monday => starts monday, ends friday |
|
2826 |
('2020-03-29', '2020-04-06', '2020-04-10'), |
|
2827 |
# today+8 is a tuesday => starts tuesday, ends friday |
|
2828 |
('2020-03-30', '2020-04-07', '2020-04-10'), |
|
2829 |
]) |
|
2830 |
def test_clae_next_week(app, resource, today, first_day, last_day): |
|
2831 |
with freezegun.freeze_time(today): |
|
2832 |
resp = app.get('/toulouse-axel/test/clae_next_weeks') |
|
2833 |
# check first week |
|
2834 |
if first_day is None: |
|
2835 |
assert resp.json['data'] == [] |
|
2836 |
else: |
|
2837 |
assert resp.json['data'][0]['date_debut'] == first_day |
|
2838 |
if last_day is None: |
|
2839 |
assert resp.json['data'] == [] |
|
2840 |
else: |
|
2841 |
assert resp.json['data'][0]['date_fin'] == last_day |
|
2842 | ||
2843 | ||
2844 |
def test_clae_next_week_result(app, resource): |
|
2845 |
with freezegun.freeze_time('2020-04-07'): |
|
2846 |
resp = app.get('/toulouse-axel/test/clae_next_weeks') |
|
2847 |
assert len(resp.json['data']) == 15 |
|
2848 |
assert resp.json['data'][0] == { |
|
2849 |
'id': 'week20200420-week20200424', |
|
2850 |
'text': 'Monday 20 April → Friday 24 April 2020', |
|
2851 |
'date_debut': '2020-04-20', |
|
2852 |
'date_fin': '2020-04-24', |
|
2853 |
'annee_reference': '2019', |
|
2854 |
} |
|
2855 |
assert resp.json['data'][14] == { |
|
2856 |
'id': 'week20200727-week20200731', |
|
2857 |
'text': 'Monday 27 July → Friday 31 July 2020', |
|
2858 |
'date_debut': '2020-07-27', |
|
2859 |
'date_fin': '2020-07-31', |
|
2860 |
'annee_reference': '2019', |
|
2861 |
} |
|
2862 | ||
2863 |
# a week with a new year |
|
2864 |
with freezegun.freeze_time('2019-12-22'): |
|
2865 |
resp = app.get('/toulouse-axel/test/clae_next_weeks') |
|
2866 |
assert resp.json['data'][0] == { |
|
2867 |
'id': 'week20191230-week20200103', |
|
2868 |
'text': 'Monday 30 December 2019 → Friday 3 January 2020', |
|
2869 |
'date_debut': '2019-12-30', |
|
2870 |
'date_fin': '2020-01-03', |
|
2871 |
'annee_reference': '2019', |
|
2872 |
} |
|
2873 | ||
2874 |
# last week is not complete |
|
2875 |
with freezegun.freeze_time('2019-07-01'): |
|
2876 |
resp = app.get('/toulouse-axel/test/clae_next_weeks') |
|
2877 |
assert resp.json['data'][-1] == { |
|
2878 |
'id': 'week20190729-week20190731', |
|
2879 |
'text': 'Monday 29 July → Wednesday 31 July 2019', |
|
2880 |
'date_debut': '2019-07-29', |
|
2881 |
'date_fin': '2019-07-31', |
|
2882 |
'annee_reference': '2018', |
|
2883 |
} |
|
2884 |
with freezegun.freeze_time('2017-07-01'): |
|
2885 |
resp = app.get('/toulouse-axel/test/clae_next_weeks') |
|
2886 |
assert resp.json['data'][-1] == { |
|
2887 |
'id': 'week20170731-week20170731', |
|
2888 |
'text': 'Monday 31 July → Monday 31 July 2017', |
|
2889 |
'date_debut': '2017-07-31', |
|
2890 |
'date_fin': '2017-07-31', |
|
2891 |
'annee_reference': '2016', |
|
2892 |
} |
|
2893 | ||
2894 | ||
2814 | 2895 |
def test_clae_booking_endpoint_axel_error(app, resource, booking_params): |
2815 | 2896 |
Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42') |
2816 | 2897 |
with freezegun.freeze_time('2019-09-01'): |
2817 |
- |