From 9d5f8e0b3397125213c928331be25eb578bc2027 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 30 Apr 2020 15:17:33 +0200
Subject: [PATCH 2/3] toulouse_axel: order endpoints (#17729)

---
 passerelle/base/models.py                     |  2 +-
 passerelle/contrib/toulouse_axel/models.py    | 45 +++++++++++++++++++
 .../passerelle/manage/service_view.html       |  7 +++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/passerelle/base/models.py b/passerelle/base/models.py
index dcafd8fd..cc9a4820 100644
--- a/passerelle/base/models.py
+++ b/passerelle/base/models.py
@@ -290,7 +290,7 @@ class BaseResource(models.Model):
         status = self.get_availability_status()
         return (status and status.down())
 
-    @endpoint(description=_('Check service availability'))
+    @endpoint(description=_('Check service availability'), display_order=-1)
     def up(self, request, **kwargs):
         if self.down():
             raise APIError('service not available')
diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 06fa3eee..cb5d0d62 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -52,6 +52,8 @@ class ToulouseAxel(BaseResource):
 
     category = _('Business Process Connectors')
 
+    _category_ordering = ['DUI', 'CAN-CLA', _('Invoices')]
+
     class Meta:
         verbose_name = _('Toulouse Axel')
 
@@ -60,6 +62,7 @@ class ToulouseAxel(BaseResource):
         response.raise_for_status()
 
     @endpoint(
+        display_order=1,
         description=_('Lock a resource'),
         perm='can_access',
         parameters={
@@ -73,6 +76,7 @@ class ToulouseAxel(BaseResource):
         return {'key': key, 'locked': True, 'locker': lock.locker, 'lock_date': lock.lock_date}
 
     @endpoint(
+        display_order=2,
         description=_('Unlock a resource'),
         perm='can_access',
         parameters={
@@ -87,6 +91,7 @@ class ToulouseAxel(BaseResource):
             return {'key': key, 'locked': False}
 
     @endpoint(
+        display_order=3,
         description=_('Get the lock status of a resource'),
         perm='can_access',
         parameters={
@@ -122,12 +127,15 @@ class ToulouseAxel(BaseResource):
         return management_dates
 
     @endpoint(
+        display_order=4,
         description=_("Get dates of the update management"),
         perm='can_access')
     def management_dates(self, request):
         return {'data': self.get_management_dates()}
 
     @endpoint(
+        display_category='DUI',
+        display_order=1,
         description=_('Create link between user and Toulouse Axel'),
         perm='can_access',
         parameters={
@@ -183,6 +191,8 @@ class ToulouseAxel(BaseResource):
             raise APIError('Person not found', err_code='not-found')
 
     @endpoint(
+        display_category='DUI',
+        display_order=2,
         description=_('Delete link between user and Toulouse Axel'),
         methods=['post'],
         perm='can_access',
@@ -196,6 +206,7 @@ class ToulouseAxel(BaseResource):
         return {'link': link_id, 'deleted': True, 'dui': link.dui}
 
     @endpoint(
+        display_order=5,
         description=_("Get a referential"),
         perm='can_access',
         pattern=r'^(?P<code>[\w-]+)/?$',
@@ -260,6 +271,8 @@ class ToulouseAxel(BaseResource):
         return family_data
 
     @endpoint(
+        display_category='DUI',
+        display_order=3,
         description=_("Get information about user's family"),
         perm='can_access',
         parameters={
@@ -271,6 +284,8 @@ class ToulouseAxel(BaseResource):
         return {'data': family_data}
 
     @endpoint(
+        display_category='DUI',
+        display_order=4,
         description=_("Get information about children"),
         perm='can_access',
         parameters={
@@ -282,6 +297,8 @@ class ToulouseAxel(BaseResource):
         return {'data': family_data.get('ENFANT', [])}
 
     @endpoint(
+        display_category='DUI',
+        display_order=5,
         description=_("Get information about a child"),
         perm='can_access',
         parameters={
@@ -299,6 +316,8 @@ class ToulouseAxel(BaseResource):
         raise APIError('Child not found', err_code='not-found')
 
     @endpoint(
+        display_category='DUI',
+        display_order=6,
         description=_("Get information about a child's contacts"),
         perm='can_access',
         parameters={
@@ -475,6 +494,8 @@ class ToulouseAxel(BaseResource):
             post_data.pop(flag)
 
     @endpoint(
+        display_category='DUI',
+        display_order=7,
         description=_("Update information about user's family"),
         perm='can_access',
         parameters={
@@ -581,6 +602,8 @@ class ToulouseAxel(BaseResource):
                 return invoice
 
     @endpoint(
+        display_category=_('Invoices'),
+        display_order=1,
         name='regie',
         perm='can_access',
         pattern=r'^(?P<regie_id>[\w-]+)/invoices/?$',
@@ -595,6 +618,8 @@ class ToulouseAxel(BaseResource):
         return {'data': invoices_data}
 
     @endpoint(
+        display_category=_('Invoices'),
+        display_order=2,
         name='regie',
         perm='can_access',
         pattern=r'^(?P<regie_id>[\w-]+)/invoices/history/?$',
@@ -609,6 +634,8 @@ class ToulouseAxel(BaseResource):
         return {'data': invoices_data}
 
     @endpoint(
+        display_category=_('Invoices'),
+        display_order=3,
         name='regie',
         perm='can_access',
         pattern=r'^(?P<regie_id>[\w-]+)/invoice/(?P<invoice_id>(historical-)?\w+-\d+)/?$',
@@ -629,6 +656,8 @@ class ToulouseAxel(BaseResource):
         return {'data': invoice}
 
     @endpoint(
+        display_category=_('Invoices'),
+        display_order=4,
         name='regie',
         perm='can_access',
         pattern=r'^(?P<regie_id>[\w-]+)/invoice/(?P<invoice_id>(historical-)?\w+-\d+)/pdf/?$',
@@ -673,6 +702,8 @@ class ToulouseAxel(BaseResource):
         return response
 
     @endpoint(
+        display_category=_('Invoices'),
+        display_order=5,
         name='regie',
         methods=['post'],
         perm='can_access',
@@ -847,6 +878,8 @@ class ToulouseAxel(BaseResource):
         return child_activities
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=1,
         description=_("Get information about CLAE activities of all children for the year"),
         perm='can_access',
         parameters={
@@ -868,6 +901,8 @@ class ToulouseAxel(BaseResource):
         return {'data': list(children_activities.values())}
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=2,
         description=_("Get the list of CLAE booked activities of a child, for a period"),
         perm='can_access',
         parameters={
@@ -926,6 +961,8 @@ class ToulouseAxel(BaseResource):
         return {'data': result}
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=3,
         description=_("Get possible days to book an activity of a child, for a period"),
         perm='can_access',
         parameters={
@@ -987,6 +1024,8 @@ class ToulouseAxel(BaseResource):
         return {'data': activity_days}
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=4,
         description=_("Get annual possible days to book an activity of a child"),
         perm='can_access',
         parameters={
@@ -1025,6 +1064,8 @@ class ToulouseAxel(BaseResource):
         return {'data': activity_days}
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=5,
         description=_("Get booked days for an activity of a child, for a period"),
         perm='can_access',
         parameters={
@@ -1039,6 +1080,8 @@ class ToulouseAxel(BaseResource):
         return {'data': [d['id'] for d in possible_days['data'] if d['prefill'] is True]}
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=6,
         description=_("CLAE/Cantine booking"),
         perm='can_access',
         parameters={
@@ -1150,6 +1193,8 @@ class ToulouseAxel(BaseResource):
         }
 
     @endpoint(
+        display_category='CAN-CLA',
+        display_order=7,
         description=_("CLAE/Cantine annual booking"),
         perm='can_access',
         parameters={
diff --git a/passerelle/templates/passerelle/manage/service_view.html b/passerelle/templates/passerelle/manage/service_view.html
index 136f2888..52a888fb 100644
--- a/passerelle/templates/passerelle/manage/service_view.html
+++ b/passerelle/templates/passerelle/manage/service_view.html
@@ -64,6 +64,13 @@
   {% block endpoints %}
     <ul class="endpoints">
     {% for endpoint in object.get_endpoints_infos %}
+      {% ifchanged endpoint.display_category %}
+      {% if endpoint.display_category %}
+        </ul>
+        <h4><span class="description">{{ endpoint.display_category }}</h4>
+        <ul class="endpoints">
+      {% endif %}
+      {% endifchanged %}
       {% if endpoint.show %}
         {% include "passerelle/manage/endpoint.html" with endpoint=endpoint %}
       {% endif %}
-- 
2.20.1

