From 2a31bf2f0c7d8d5f3031be04a62601b3879516eb Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Sat, 16 Oct 2021 17:35:36 +1100 Subject: [PATCH] delete groups Whoops, forgot to add this functionality earlier. - allow owner to delete a group - change all group lists to closed curation with group=False when group deleted --- bookwyrm/models/list.py | 2 +- .../templates/groups/delete_group_modal.html | 18 +++++++++--------- bookwyrm/urls.py | 1 + bookwyrm/views/__init__.py | 1 + bookwyrm/views/group.py | 19 ++++++++++++++++++- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index 66ec4eee4..978a7a9b3 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -32,7 +32,7 @@ class List(OrderedCollectionMixin, BookWyrmModel): ) group = models.ForeignKey( "Group", - on_delete=models.PROTECT, + on_delete=models.SET_NULL, default=None, blank=True, null=True, diff --git a/bookwyrm/templates/groups/delete_group_modal.html b/bookwyrm/templates/groups/delete_group_modal.html index fd6706157..4bb87ed9b 100644 --- a/bookwyrm/templates/groups/delete_group_modal.html +++ b/bookwyrm/templates/groups/delete_group_modal.html @@ -8,14 +8,14 @@ {% endblock %} {% block modal-footer %} -
- {% csrf_token %} - - - {% trans "Cancel" as button_text %} - {% include 'snippets/toggle/toggle_button.html' with text=button_text controls_text="delete_list" controls_uid=list.id %} -
+
+ {% csrf_token %} + + + {% trans "Cancel" as button_text %} + {% include 'snippets/toggle/toggle_button.html' with text=button_text controls_text="delete_group" controls_uid=group.id %} +
{% endblock %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 49c47ef5a..c81a930b1 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -258,6 +258,7 @@ urlpatterns = [ re_path( r"^group/(?P\d+)(.json)?/?$", views.Group.as_view(), name="group" ), + re_path(r"^group/delete/(?P\d+)/?$", views.delete_group, name="delete-group"), re_path( r"^group/(?P\d+)/add-users/?$", views.FindUsers.as_view(), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 08b846dc8..1e525cb38 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -45,6 +45,7 @@ from .group import ( Group, UserGroups, FindUsers, + delete_group, invite_member, remove_member, accept_membership, diff --git a/bookwyrm/views/group.py b/bookwyrm/views/group.py index 2623361d7..07eabda73 100644 --- a/bookwyrm/views/group.py +++ b/bookwyrm/views/group.py @@ -79,7 +79,7 @@ class UserGroups(View): """create a user group""" form = forms.GroupForm(request.POST) if not form.is_valid(): - return redirect(request.user.local_path + "groups") + return redirect(request.user.local_path + "/groups") group = form.save() # add the creator as a group member models.GroupMember.objects.create(group=group, user=request.user) @@ -134,6 +134,22 @@ class FindUsers(View): return TemplateResponse(request, "groups/find_users.html", data) +@require_POST +@login_required +def delete_group(request, group_id): + """delete a group""" + group = get_object_or_404(models.Group, id=group_id) + + # only the owner can delete a group + group.raise_not_deletable(request.user) + + # deal with any group lists + models.List.objects.filter(group=group).update(curation="closed", group=None) + + group.delete() + return redirect(request.user.local_path + "/groups") + + @require_POST @login_required def invite_member(request): @@ -268,3 +284,4 @@ def reject_membership(request): pass return redirect(request.user.local_path) +