Commit e5d0098a by Máhonfai Bálint

Add group export view and template

parent 35e2670f
{% load crispy_forms_tags %}
{% load i18n %}
<p class="text-muted">
{% trans "Export a group to the user store with the given filename." %}
</p>
<form method="POST" data-group_pk="{{ group.pk }}" action="{% url "dashboard.views.group-export" group_pk=group.pk %}">
{% csrf_token %}
{% crispy form %}
</form>
...@@ -31,6 +31,7 @@ from django.http import HttpResponse, Http404 ...@@ -31,6 +31,7 @@ from django.http import HttpResponse, Http404
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic import UpdateView, TemplateView from django.views.generic import UpdateView, TemplateView
from django.views.generic.detail import SingleObjectMixin
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
from itertools import chain from itertools import chain
...@@ -39,7 +40,7 @@ from .util import (CheckedDetailView, AclUpdateView, search_user, ...@@ -39,7 +40,7 @@ from .util import (CheckedDetailView, AclUpdateView, search_user,
saml_available, DeleteViewBase) saml_available, DeleteViewBase)
from ..forms import ( from ..forms import (
AddGroupMemberForm, AclUserOrGroupAddForm, GroupPermissionForm, AddGroupMemberForm, AclUserOrGroupAddForm, GroupPermissionForm,
GroupCreateForm, GroupImportForm, GroupProfileUpdateForm, GroupCreateForm, GroupImportForm, GroupProfileUpdateForm, GroupExportForm,
) )
from ..models import FutureMember, GroupProfile from ..models import FutureMember, GroupProfile
from ..store_api import Store, NoStoreException from ..store_api import Store, NoStoreException
...@@ -427,6 +428,79 @@ class GroupImportView(LoginRequiredMixin, TemplateView): ...@@ -427,6 +428,79 @@ class GroupImportView(LoginRequiredMixin, TemplateView):
return self.get(request, form, *args, **kwargs) return self.get(request, form, *args, **kwargs)
class GroupExportView(LoginRequiredMixin, SingleObjectMixin, TemplateView):
form_class = GroupExportForm
model = Group
pk_url_kwarg = "group_pk"
def __init__(self):
super(GroupExportView, self).__init__()
self.object = None
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/_modal.html']
else:
return ['dashboard/nojs-wrapper.html']
def get(self, request, form=None, *args, **kwargs):
self.object = self.get_object()
if not self.object.profile.has_level(request.user, 'operator'):
raise PermissionDenied()
try:
Store(request.user)
except NoStoreException:
raise PermissionDenied()
if form is None:
form = self.form_class(group_name=self.object.name)
context = self.get_context_data(**kwargs)
context.update({
'group': self.object,
'template': 'dashboard/group-export.html',
'box_title': _('Export Group'),
'form': form,
'ajax_title': True,
})
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
group = self.object
if not group.profile.has_level(request.user, 'operator'):
raise PermissionDenied()
try:
Store(request.user)
except NoStoreException:
raise PermissionDenied()
form = self.form_class(request.POST, group_name=self.object.name)
if form.is_valid():
name = form.cleaned_data["exported_name"]
group_json = group.profile.convert_to_json()
store = Store(request.user)
url = store.request_upload("/")
data = {'data': (name + '.group', group_json)}
requests.post(url, files=data)
success_message = _("Group successfully exported.")
if request.is_ajax():
response = {
'message': success_message,
'redirect': group.profile.get_absolute_url()
}
return HttpResponse(
json.dumps(response),
content_type="application/json"
)
else:
messages.success(request, success_message)
return redirect(group.profile.get_absolute_url())
else:
return self.get(request, form, *args, **kwargs)
class GroupProfileUpdate(SuccessMessageMixin, GroupCodeMixin, class GroupProfileUpdate(SuccessMessageMixin, GroupCodeMixin,
LoginRequiredMixin, UpdateView): LoginRequiredMixin, UpdateView):
form_class = GroupProfileUpdateForm form_class = GroupProfileUpdateForm
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment