From 4bc041837b7728298620287c8fa75cc5fb41e45d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 18 Dec 2018 15:22:19 +0100 Subject: [PATCH 5/5] manager: add ous export (#29162) --- src/authentic2/manager/ou_views.py | 18 ++++++++++++++++ .../templates/authentic2/manager/ous.html | 1 + src/authentic2/manager/urls.py | 3 +++ tests/test_ou_manager.py | 21 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 tests/test_ou_manager.py diff --git a/src/authentic2/manager/ou_views.py b/src/authentic2/manager/ou_views.py index 1d6e0107..ed8a001e 100644 --- a/src/authentic2/manager/ou_views.py +++ b/src/authentic2/manager/ou_views.py @@ -1,8 +1,12 @@ +import json + from django_rbac.utils import get_ou_model from django.http import HttpResponseRedirect from django.contrib import messages from django.utils.translation import ugettext as _ +from authentic2 import data_transfer + from . import tables, views, forms @@ -13,6 +17,7 @@ class OrganizationalUnitView(views.BaseTableView): search_form_class = forms.NameSearchForm permissions = ['a2_rbac.search_organizationalunit'] title = _('Organizational units') + formats = ['json'] listing = OrganizationalUnitView.as_view() @@ -74,3 +79,16 @@ class OrganizationalUnitDeleteView(views.BaseDeleteView): **kwargs) delete = OrganizationalUnitDeleteView.as_view() + + +class RolesExportView(views.ExportMixin, OrganizationalUnitView): + def get(self, request, *args, **kwargs): + export = data_transfer.export_site( + data_transfer.ExportContext( + ou_qs=self.get_table_data(), + export_roles=False, + export_ous=True)) + return self.export_response(json.dumps(export), 'application/json', 'json') + + +export = RolesExportView.as_view() diff --git a/src/authentic2/manager/templates/authentic2/manager/ous.html b/src/authentic2/manager/templates/authentic2/manager/ous.html index 3d0e2884..e6da200c 100644 --- a/src/authentic2/manager/templates/authentic2/manager/ous.html +++ b/src/authentic2/manager/templates/authentic2/manager/ous.html @@ -26,4 +26,5 @@ {% with row_link=1 %} {% render_table table "authentic2/manager/table.html" %} {% endwith %} + {% include "authentic2/manager/export_include.html" with export_view_name="a2-manager-ou-export" %} {% endblock %} diff --git a/src/authentic2/manager/urls.py b/src/authentic2/manager/urls.py index 0c1df367..c0646d5d 100644 --- a/src/authentic2/manager/urls.py +++ b/src/authentic2/manager/urls.py @@ -105,6 +105,9 @@ urlpatterns = required( name='a2-manager-ou-edit'), url(r'^organizational-units/(?P\d+)/delete/$', ou_views.delete, name='a2-manager-ou-delete'), + url(r'^organizational-units/export/(?Pjson)/$', + ou_views.export, + name='a2-manager-ou-export'), # Services url(r'^services/$', service_views.listing, diff --git a/tests/test_ou_manager.py b/tests/test_ou_manager.py new file mode 100644 index 00000000..3945434a --- /dev/null +++ b/tests/test_ou_manager.py @@ -0,0 +1,21 @@ +from utils import login + + +def test_manager_ou_export(app, admin, ou1, role_ou1, ou2, role_ou2): + response = login(app, admin, 'a2-manager-ous') + + export_response = response.click('JSON') + export = export_response.json + + assert export.keys() == ['ous'] + assert len(export['ous']) == 3 + assert set([ou['slug'] for ou in export['ous']]) == set(['default', 'ou1', 'ou2']) + + response.form.set('search-text', 'ou1') + search_response = response.form.submit() + + export_response = search_response.click('JSON') + export = export_response.json + + assert len(export['ous']) == 1 + assert export['ous'][0]['slug'] == 'ou1' -- 2.18.0