Commit 5fcbc996 by Máhonfai Bálint

Add group import view

parent a6ace944
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>. # with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import from __future__ import absolute_import
from django.conf.urls import url from django.conf.urls import url
from vm.models import Instance from vm.models import Instance
...@@ -57,11 +58,10 @@ from .views import ( ...@@ -57,11 +58,10 @@ from .views import (
MessageList, MessageDetail, MessageCreate, MessageDelete, MessageList, MessageDetail, MessageCreate, MessageDelete,
EnableTwoFactorView, DisableTwoFactorView, EnableTwoFactorView, DisableTwoFactorView,
AclUserGroupAutocomplete, AclUserAutocomplete, AclUserGroupAutocomplete, AclUserAutocomplete,
RescheduleView, RescheduleView, GroupImportView,
) )
from .views.vm import vm_ops, vm_mass_ops
from .views.node import node_ops from .views.node import node_ops
from .views.vm import vm_ops, vm_mass_ops
urlpatterns = [ urlpatterns = [
url(r'^$', IndexView.as_view(), name="dashboard.index"), url(r'^$', IndexView.as_view(), name="dashboard.index"),
...@@ -198,6 +198,8 @@ urlpatterns = [ ...@@ -198,6 +198,8 @@ urlpatterns = [
name="dashboard.views.remove-all-users"), name="dashboard.views.remove-all-users"),
url(r'^group/create/$', GroupCreate.as_view(), url(r'^group/create/$', GroupCreate.as_view(),
name='dashboard.views.group-create'), name='dashboard.views.group-create'),
url(r'^group/import/$', GroupImportView.as_view(),
name="dashboard.views.group-import"),
url(r'^group/(?P<group_pk>\d+)/permissions/$', url(r'^group/(?P<group_pk>\d+)/permissions/$',
GroupPermissionsView.as_view(), GroupPermissionsView.as_view(),
name="dashboard.views.group-permissions"), name="dashboard.views.group-permissions"),
......
...@@ -18,31 +18,32 @@ from __future__ import unicode_literals, absolute_import ...@@ -18,31 +18,32 @@ from __future__ import unicode_literals, absolute_import
import json import json
import logging import logging
from itertools import chain
import requests
from braces.views import SuperuserRequiredMixin, LoginRequiredMixin
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponse, Http404 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 braces.views import SuperuserRequiredMixin, LoginRequiredMixin
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
from itertools import chain
from vm.models import Instance, InstanceTemplate
from .util import (CheckedDetailView, AclUpdateView, search_user,
saml_available, DeleteViewBase)
from ..forms import ( from ..forms import (
AddGroupMemberForm, AclUserOrGroupAddForm, GroupPermissionForm, AddGroupMemberForm, AclUserOrGroupAddForm, GroupPermissionForm,
GroupCreateForm, GroupProfileUpdateForm, GroupCreateForm, GroupImportForm, GroupProfileUpdateForm,
) )
from ..models import FutureMember, GroupProfile from ..models import FutureMember, GroupProfile
from vm.models import Instance, InstanceTemplate from ..store_api import Store, NoStoreException
from ..tables import GroupListTable from ..tables import GroupListTable
from .util import (CheckedDetailView, AclUpdateView, search_user,
saml_available, DeleteViewBase)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -325,7 +326,6 @@ class GroupDelete(DeleteViewBase): ...@@ -325,7 +326,6 @@ class GroupDelete(DeleteViewBase):
class GroupCreate(GroupCodeMixin, LoginRequiredMixin, TemplateView): class GroupCreate(GroupCodeMixin, LoginRequiredMixin, TemplateView):
form_class = GroupCreateForm form_class = GroupCreateForm
def get_template_names(self): def get_template_names(self):
...@@ -361,16 +361,74 @@ class GroupCreate(GroupCodeMixin, LoginRequiredMixin, TemplateView): ...@@ -361,16 +361,74 @@ class GroupCreate(GroupCodeMixin, LoginRequiredMixin, TemplateView):
savedform.profile.set_level(request.user, 'owner') savedform.profile.set_level(request.user, 'owner')
messages.success(request, _('Group successfully created.')) messages.success(request, _('Group successfully created.'))
if request.is_ajax(): if request.is_ajax():
return HttpResponse(json.dumps({'redirect': return HttpResponse(
savedform.profile.get_absolute_url()}), json.dumps(
content_type="application/json") {'redirect': savedform.profile.get_absolute_url()}
),
content_type="application/json"
)
else: else:
return redirect(savedform.profile.get_absolute_url()) return redirect(savedform.profile.get_absolute_url())
class GroupImportView(LoginRequiredMixin, TemplateView):
form_class = GroupImportForm
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):
if not request.user.has_module_perms('auth'):
raise PermissionDenied()
try:
Store(request.user)
except NoStoreException:
raise PermissionDenied()
if form is None:
form = self.form_class(user=request.user)
context = self.get_context_data(**kwargs)
context.update({
'template': 'dashboard/group-import.html',
'box_title': _('Import a Group'),
'form': form,
'ajax_title': True,
})
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
if not request.user.has_module_perms('auth'):
raise PermissionDenied()
try:
Store(request.user)
except NoStoreException:
raise PermissionDenied()
form = self.form_class(request.POST, user=request.user)
if form.is_valid():
group_path = form.cleaned_data["group_path"]
url = Store(request.user).request_download(group_path)
json_str = requests.get(url).content
profile = GroupProfile.create_from_json(request.user, json_str)
if profile is None:
raise SuspiciousOperation()
if request.is_ajax():
return HttpResponse(
json.dumps({'redirect': profile.get_absolute_url()}),
content_type="application/json"
)
else:
return redirect(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
model = Group model = Group
success_message = _('Group is successfully updated.') success_message = _('Group is successfully updated.')
......
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