Project

General

Profile

0002-base-add-import-export-UI-15269.patch

Valentin Deniaud, 19 Dec 2019 03:29 PM

Download (7.86 KB)

View differences:

Subject: [PATCH 2/2] base: add import/export UI (#15269)

Site export as well as connector export.
 passerelle/base/forms.py                      |  6 ++++
 passerelle/base/urls.py                       |  8 ++++-
 passerelle/base/views.py                      | 36 ++++++++++++++++---
 passerelle/templates/passerelle/manage.html   |  3 ++
 .../passerelle/manage/service_view.html       |  1 +
 passerelle/urls.py                            |  8 +++--
 passerelle/views.py                           |  8 +++++
 7 files changed, 63 insertions(+), 7 deletions(-)
passerelle/base/forms.py
1 1
from django import forms
2
from django.utils.translation import ugettext_lazy as _
2 3

  
3 4
from .models import ApiUser, AccessRight, AvailabilityParameters
4 5

  
......
27 28
        widgets = {
28 29
            'notification_delays': forms.TextInput,
29 30
        }
31

  
32

  
33
class ImportSiteForm(forms.Form):
34
    site_json = forms.FileField(label=_('Site Export File'))
35
    import_users = forms.BooleanField(label=_('Import users and access rights'), required=False)
passerelle/base/urls.py
2 2

  
3 3
from .views import ApiUserCreateView, ApiUserUpdateView, ApiUserDeleteView, \
4 4
        ApiUserListView, AccessRightDeleteView, AccessRightCreateView, \
5
        LoggingParametersUpdateView, ManageAvailabilityView
5
        LoggingParametersUpdateView, ManageAvailabilityView, ImportSiteView, \
6
        ExportSiteView
6 7

  
7 8
access_urlpatterns = [
8 9
    url(r'^$', ApiUserListView.as_view(), name='apiuser-list'),
......
20 21
        ManageAvailabilityView.as_view(), name='manage-availability')
21 22

  
22 23
]
24

  
25
import_export_urlpatterns = [
26
    url(r'^import$', ImportSiteView.as_view(), name='import-site'),
27
    url(r'^export$', ExportSiteView.as_view(), name='export-site'),
28
]
passerelle/base/views.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import datetime
18
import json
18 19

  
19 20
from dateutil import parser as date_parser
20 21

  
......
25 26
from django.db.models import Q
26 27
from django.forms import models as model_forms
27 28
from django.views.generic import (
28
    DetailView, ListView, CreateView, UpdateView, DeleteView, FormView)
29
from django.http import Http404
29
    View, DetailView, ListView, CreateView, UpdateView, DeleteView, FormView)
30
from django.http import Http404, HttpResponse
30 31
from django.utils.timezone import make_aware
31 32
from django.utils.translation import ugettext_lazy as _
32 33

  
33 34
from .models import ApiUser, AccessRight, LoggingParameters, ResourceStatus, Job
34
from .forms import ApiUserForm, AccessRightForm, AvailabilityParametersForm
35
from .forms import ApiUserForm, AccessRightForm, AvailabilityParametersForm, ImportSiteForm
35 36
from ..views import GenericConnectorMixin
36
from ..utils import get_trusted_services
37
from ..utils import get_trusted_services, import_site, export_site
37 38

  
38 39

  
39 40
class ResourceView(DetailView):
......
277 278
        except Job.DoesNotExist:
278 279
            raise Http404()
279 280
        return context
281

  
282

  
283
class ImportSiteView(FormView):
284
    template_name = 'passerelle/manage/import_site.html'
285
    form_class = ImportSiteForm
286

  
287
    def get_success_url(self):
288
        return reverse('manage-home')
289

  
290
    def form_valid(self, form):
291
        try:
292
            site_json = json.load(self.request.FILES['site_json'])
293
        except ValueError:
294
            form.add_error('site_json', _('File is not in the expected JSON format.'))
295
            return self.form_invalid(form)
296

  
297
        results = import_site(site_json, overwrite=True,
298
                              import_users=form.cleaned_data['import_users'])
299
        return super(ImportSiteView, self).form_valid(form)
300

  
301

  
302
class ExportSiteView(View):
303

  
304
    def get(self, request, *args, **kwargs):
305
        response = HttpResponse(content_type='application/json')
306
        json.dump(export_site(), response, indent=2)
307
        return response
passerelle/templates/passerelle/manage.html
20 20
    {% endfor %}
21 21
  </ul>
22 22
</div>
23
<div style="clear:both;"></div>
24
<a href="{% url 'import-site' %}" rel="popup">{% trans 'Import' %}</a>
25
<a download href="{% url 'export-site' %}">{% trans 'Export' %}</a>
23 26

  
24 27
{% endblock %}
25 28

  
passerelle/templates/passerelle/manage/service_view.html
27 27
{% if object|can_delete:request.user %}
28 28
<a rel="popup" href="{{ object.get_delete_url }}">{% trans 'delete' %}</a>
29 29
{% endif %}
30
<a download href="{% url 'export-connector' connector=object.get_connector_slug slug=object.slug %}">{% trans 'export' %}</a>
30 31
</span>
31 32
{% endblock %}
32 33

  
passerelle/urls.py
9 9
from .views import (HomePageView, ManageView, ManageAddView,
10 10
        GenericCreateConnectorView, GenericDeleteConnectorView,
11 11
        GenericEditConnectorView, GenericEndpointView, GenericConnectorView,
12
        GenericViewLogsConnectorView, GenericLogView,
12
        GenericViewLogsConnectorView, GenericLogView, GenericExportConnectorView,
13 13
        login, logout, menu_json)
14 14
from .base.views import GenericViewJobsConnectorView, GenericJobView
15 15
from .urls_utils import decorated_includes, required, app_enabled, manager_required
16
from .base.urls import access_urlpatterns
16
from .base.urls import access_urlpatterns, import_export_urlpatterns
17 17
from .plugins import register_apps_urls
18 18

  
19 19
from passerelle.apps.pastell import urls as pastell_urls
......
34 34

  
35 35
    url(r'^manage/access/',
36 36
        decorated_includes(manager_required, include(access_urlpatterns))),
37
    url(r'^manage/',
38
        decorated_includes(manager_required, include(import_export_urlpatterns))),
37 39
]
38 40

  
39 41
urlpatterns += required(
......
73 75
                GenericViewJobsConnectorView.as_view(), name='view-jobs-connector'),
74 76
            url(r'^(?P<slug>[\w,-]+)/jobs/(?P<job_pk>\d+)/$',
75 77
                GenericJobView.as_view(), name='view-job'),
78
            url(r'^(?P<slug>[\w,-]+)/export$',
79
                GenericExportConnectorView.as_view(), name='export-connector'),
76 80
        ])))
77 81
]
78 82

  
passerelle/views.py
477 477

  
478 478
    def delete(self, request, *args, **kwargs):
479 479
        return self.get(request, *args, **kwargs)
480

  
481

  
482
class GenericExportConnectorView(GenericConnectorMixin, DetailView):
483

  
484
    def get(self, request, *args, **kwargs):
485
        response = HttpResponse(content_type='application/json')
486
        json.dump({'resources': [self.get_object().export_json()]}, response, indent=2)
487
        return response
480
-