Repository: ephios-dev/ephios

Issue:
Benutzer bearbeiten
Als Manager möchte ich einen Nutzer bearbeiten. Dabei möchte ich die selben Optionen haben wie beim Anlegen des Nutzers. In der Liste der Nutzer soll es für jeden Nutzer eine entsprechende Schaltfläche geben. Der Nutzer soll über Änderungen per Mail informiert werden.


Discussion:

Resolving patch:
diff --git a/user_management/mail.py b/user_management/mail.py
--- a/user_management/mail.py
+++ b/user_management/mail.py
@@ -11,7 +11,6 @@
 
 
 def send_account_creation_info(userprofile):
-    messages = []
     subject = _("Welcome to JEP!")
     uid = urlsafe_base64_encode(force_bytes(userprofile.id))
     token = default_token_generator.make_token(userprofile)
@@ -24,10 +23,26 @@ def send_account_creation_info(userprofile):
 
     html_content = render_to_string(
         "user_management/new_account_email.html",
-        {"uid": uid, "token": token, "site_url": SITE_URL, "email": userprofile.email,},
+        {"uid": uid, "token": token, "site_url": SITE_URL, "email": userprofile.email},
     )
     message = EmailMultiAlternatives(to=[userprofile.email], subject=subject, body=text_content)
     message.attach_alternative(html_content, "text/html")
-    messages.append(message)
+    message.send()
 
-    mail.get_connection().send_messages(messages)
+
+def send_account_update_info(userprofile):
+    subject = _("JEP account updated")
+    url = reverse("user_management:profile")
+    text_content = _(
+        "You're receiving this email because your account at JEP has been updated.\n"
+        "You can see the changes in your profile: {site_url}{url}\n"
+        "Your username is your email address: {email}\n"
+    ).format(site_url=SITE_URL, url=url, email=userprofile.email)
+
+    html_content = render_to_string(
+        "user_management/account_updated_email.html",
+        {"site_url": SITE_URL, "url": url, "email": userprofile.email},
+    )
+    message = EmailMultiAlternatives(to=[userprofile.email], subject=subject, body=text_content)
+    message.attach_alternative(html_content, "text/html")
+    message.send()
diff --git a/user_management/urls.py b/user_management/urls.py
--- a/user_management/urls.py
+++ b/user_management/urls.py
@@ -9,6 +9,7 @@
     path("groups/<int:pk>/edit", views.GroupUpdateView.as_view(), name="group_edit"),
     path("groups/<int:pk>/delete", views.GroupDeleteView.as_view(), name="group_delete"),
     path("groups/create", views.GroupCreateView.as_view(), name="group_add"),
-    path("users/", views.UserProfileListView.as_view(), name="user_list",),
+    path("users/", views.UserProfileListView.as_view(), name="userprofile_list",),
+    path("users/<int:pk>/edit", views.UserProfileUpdateView.as_view(), name="userprofile_edit",),
     path("users/create/", views.UserProfileCreateView.as_view(), name="userprofile_create",),
 ]
diff --git a/user_management/views.py b/user_management/views.py
--- a/user_management/views.py
+++ b/user_management/views.py
@@ -30,7 +30,7 @@ class UserProfileCreateView(PermissionRequiredMixin, CreateView):
 
     def get_success_url(self):
         messages.success(self.request, _("User added successfully."))
-        return reverse("user_management:user_list")
+        return reverse("user_management:userprofile_list")
 
     def form_valid(self, form):
         response = super().form_valid(form)
@@ -40,6 +40,31 @@ def form_valid(self, form):
         return response
 
 
+class UserProfileUpdateView(PermissionRequiredMixin, UpdateView):
+    model = UserProfile
+    permission_required = "user_management.change_userprofile"
+    template_name = "user_management/userprofile_form.html"
+    form_class = UserProfileForm
+
+    def get_success_url(self):
+        messages.success(self.request, _("User updated successfully."))
+        return reverse("user_management:userprofile_list")
+
+    def form_valid(self, form):
+        response = super().form_valid(form)
+        userprofile = self.object
+        if userprofile.is_active:
+            mail.send_account_update_info(userprofile)
+        return response
+
+    def get_form_kwargs(self):
+        kwargs = super().get_form_kwargs()
+        kwargs["initial"] = {
+            "groups": self.object.groups.all(),
+        }
+        return kwargs
+
+
 class GroupListView(PermissionRequiredMixin, ListView):
     model = Group
     permission_required = "auth.view_group"
@@ -87,6 +112,7 @@ def get_form_kwargs(self):
         return kwargs
 
     def get_success_url(self):
+        messages.success(self.request, _("Group updated successfully."))
         return reverse("user_management:group_list")
 
 


Test patch: