Repository: OpenEnergyPlatform/oeplatform

Issue:
Group creation fails if name exists
This causes a HTTP 500 instead of usable feedback


Discussion:

Resolving patch:
diff --git a/login/forms.py b/login/forms.py
--- a/login/forms.py
+++ b/login/forms.py
@@ -6,11 +6,12 @@
     SetPasswordForm,
     UserChangeForm,
     UserCreationForm,
+
 )
 from django.core.exceptions import ValidationError
 
 from .models import ActivationToken
-from .models import myuser as OepUser
+from .models import myuser as OepUser, UserGroup
 
 
 class CreateUserForm(UserCreationForm):
@@ -87,6 +88,11 @@ def save(self, user):
         user.send_activation_mail(reset_token=True)
 
 
+class GroupForm(forms.ModelForm):
+    class Meta:
+        model = UserGroup
+        fields = ("name", "description")
+
 class GroupUserForm(forms.ModelForm):
     """
      A form for setting members of a group.     
diff --git a/login/urls.py b/login/urls.py
--- a/login/urls.py
+++ b/login/urls.py
@@ -15,10 +15,19 @@
     ),
     url(r"^groups/$", views.GroupManagement.as_view(), name="input"),
     url(
-        r"^groups/(?P<group_id>[\w\d_\s]+)/edit",
+        r"^groups/new/$",
         views.GroupCreate.as_view(),
         name="input",
     ),
+    url(
+        r"^groups/(?P<group_id>[\w\d_\s]+)/edit$",
+        views.GroupCreate.as_view(),
+        name="input",
+    ),
+    url(
+        r"^groups/(?P<group_id>[\w\d_\s]+)/$",
+        views.GroupView.as_view(),
+    ),
     url(
         r"^groups/(?P<group_id>[\w\d_\s]+)/members$",
         views.GroupEdit.as_view(),
diff --git a/login/views.py b/login/views.py
--- a/login/views.py
+++ b/login/views.py
@@ -11,7 +11,7 @@
 
 import login.models as models
 
-from .forms import ChangeEmailForm, CreateUserForm, DetachForm, EditUserForm
+from .forms import ChangeEmailForm, CreateUserForm, DetachForm, EditUserForm, GroupForm
 from .models import ADMIN_PERM, GroupMembership, UserGroup
 from .models import myuser as OepUser
 
@@ -61,15 +61,18 @@ def get(self, request, group_id=None):
         :param user_id: An group id
         :return: Profile renderer
         """
-        group = None
+
         if group_id:
             group = UserGroup.objects.get(id=group_id)
+            form = GroupForm(instance=group)
             membership = get_object_or_404(
                 GroupMembership, group=group, user=request.user
             )
             if membership.level < ADMIN_PERM:
                 raise PermissionDenied
-        return render(request, "login/group_create.html", {"group": group})
+        else:
+            form = GroupForm()
+        return render(request, "login/group_create.html", {"form": form})
 
     def post(self, request, group_id=None):
         """
@@ -81,25 +84,41 @@ def post(self, request, group_id=None):
         :param user_id: An group id
         :return: Profile renderer
         """
-
-        if group_id:
-            group = UserGroup.objects.get(id=group_id)
-            membership = get_object_or_404(
-                GroupMembership, group=group, user=request.user
-            )
-            if membership.level < ADMIN_PERM:
-                raise PermissionDenied
-            group.name = request.POST["name"]
-            group.description = request.POST["description"]
-            group.save()
+        group = UserGroup.objects.get(id=group_id) if group_id else None
+        form = GroupForm(request.POST, instance=group)
+        if form.is_valid():
+            if group_id:
+                membership = get_object_or_404(
+                    GroupMembership, group=group, user=request.user
+                )
+                if membership.level < ADMIN_PERM:
+                    raise PermissionDenied
+            else:
+                group = form.save()
+                membership = GroupMembership.objects.create(
+                    user=request.user, group=group, level=ADMIN_PERM
+                )
+                membership.save()
+            return redirect("/user/groups/{id}".format(id=group.id), {"group": group})
         else:
-            group = UserGroup.objects.create(name=request.POST["name"])
-            group.save()
-            membership = GroupMembership.objects.create(
-                user=request.user, group=group, level=ADMIN_PERM
-            )
-            membership.save()
-        return redirect("/user/groups/{id}/members".format(id=group.id))
+            return render(request, "login/group_create.html", {"form": form})
+
+
+class GroupView(View, LoginRequiredMixin):
+    def get(self, request, group_id):
+        """
+        Load the chosen action(create or edit) for a group.
+        :param request: A HTTP-request object sent by the Django framework.
+        :param user_id: An user id
+        :param user_id: An group id
+        :return: Profile renderer
+        """
+        group = get_object_or_404(UserGroup, pk=group_id)
+        return render(
+            request,
+            "login/group.html",
+            {"group": group},
+        )
 
 
 class GroupEdit(View, LoginRequiredMixin):


Test patch: