Commit 18d3d7d9 by Bach Dániel

dashboard: rewrite UserCreationView

parent 0df9f5df
......@@ -59,7 +59,7 @@ from django.utils.translation import string_concat
from .validators import domain_validator
from dashboard.models import ConnectCommand
from dashboard.models import ConnectCommand, create_profile
LANGUAGES_WITH_CODE = ((l[0], string_concat(l[1], " (", l[0], ")"))
for l in LANGUAGES)
......@@ -1254,10 +1254,19 @@ class CirclePasswordChangeForm(PasswordChangeForm):
class UserCreationForm(OrgUserCreationForm):
def __init__(self, *args, **kwargs):
choices = kwargs.pop('choices')
group = kwargs.pop('default')
super(UserCreationForm, self).__init__(*args, **kwargs)
self.fields['groups'] = forms.ModelMultipleChoiceField(
queryset=choices, initial=[group], required=False,
label=_('Groups'))
class Meta:
model = User
fields = ("username", 'email', 'first_name', 'last_name')
fields = ("username", 'email', 'first_name', 'last_name', 'groups')
@property
def helper(self):
......@@ -1272,6 +1281,8 @@ class UserCreationForm(OrgUserCreationForm):
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
create_profile(user)
user.groups.add(*self.cleaned_data["groups"])
return user
......
......@@ -78,7 +78,7 @@
<h3>
{% trans "User list" %}
{% if perms.auth.add_user %}
<a href="{% url "dashboard.views.create-user" group.pk %}" class="btn btn-success pull-right">
<a href="{% url "dashboard.views.user-create" %}?group_pk={{ group.pk}}" class="btn btn-success pull-right">
{% trans "Create user" %}
</a>
{% endif %}
......
......@@ -11,7 +11,7 @@
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<a href="{% url "dashboard.views.template-choose" %}" class="pull-right btn btn-success btn-xs template-choose">
<a href="{% url "dashboard.views.user-create" %}" class="pull-right btn btn-success btn-xs template-choose">
<i class="fa fa-plus"></i> {% trans "new user" %}
</a>
<h3 class="no-margin"><i class="fa fa-user"></i> {% trans "Users" %}</h3>
......
......@@ -64,6 +64,9 @@ urlpatterns = patterns(
url(r'^$', IndexView.as_view(), name="dashboard.index"),
url(r"^profile/list/$", UserList.as_view(),
name="dashboard.views.user-list"),
url(r'^profile/create/$',
UserCreationView.as_view(),
name="dashboard.views.user-create"),
url(r'^lease/(?P<pk>\d+)/$', LeaseDetail.as_view(),
name="dashboard.views.lease-detail"),
url(r'^lease/create/$', LeaseCreate.as_view(),
......@@ -177,9 +180,6 @@ urlpatterns = patterns(
name="dashboard.views.remove-future-user"),
url(r'^group/create/$', GroupCreate.as_view(),
name='dashboard.views.group-create'),
url(r'^group/(?P<group_pk>\d+)/create/$',
UserCreationView.as_view(),
name="dashboard.views.create-user"),
url(r'^group/(?P<group_pk>\d+)/permissions/$',
GroupPermissionsView.as_view(),
name="dashboard.views.group-permissions"),
......
......@@ -52,7 +52,7 @@ from ..forms import (
UserKeyForm, CirclePasswordChangeForm, ConnectCommandForm,
UserListSearchForm,
)
from ..models import Profile, GroupProfile, ConnectCommand, create_profile
from ..models import Profile, GroupProfile, ConnectCommand
from ..tables import (
UserKeyListTable, ConnectCommandListTable, UserListTable,
)
......@@ -274,28 +274,24 @@ class UserCreationView(LoginRequiredMixin, PermissionRequiredMixin,
permission_required = "auth.add_user"
def get_success_url(self):
reverse('dashboard.views.group-detail', args=[self.group.pk])
return reverse('dashboard.views.profile', args=[self.object.username])
def get_group(self, group_pk):
self.group = get_object_or_404(Group, pk=group_pk)
if not self.group.profile.has_level(self.request.user, 'owner'):
raise PermissionDenied()
def get(self, *args, **kwargs):
self.get_group(kwargs.pop('group_pk'))
return super(UserCreationView, self).get(*args, **kwargs)
def post(self, *args, **kwargs):
group_pk = kwargs.pop('group_pk')
self.get_group(group_pk)
ret = super(UserCreationView, self).post(*args, **kwargs)
if self.object:
create_profile(self.object)
self.object.groups.add(self.group)
return redirect(
reverse('dashboard.views.group-detail', args=[group_pk]))
def get_form_kwargs(self):
profiles = GroupProfile.get_objects_with_level(
'owner', self.request.user)
choices = Group.objects.filter(groupprofile__in=profiles)
group_pk = self.request.GET.get('group_pk')
if group_pk:
try:
default = choices.get(pk=group_pk)
except (ValueError, Group.DoesNotExist):
raise Http404()
else:
return ret
default = None
val = super(UserCreationView, self).get_form_kwargs()
val.update({'choices': choices, 'default': default})
return val
class ProfileView(LoginRequiredMixin, DetailView):
......
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