Commit 18d3d7d9 by Bach Dániel

dashboard: rewrite UserCreationView

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