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
This commit is contained in:
Hugh Rundle 2021-10-16 17:35:36 +11:00
parent 85784f07d2
commit 2a31bf2f0c
5 changed files with 30 additions and 11 deletions

View file

@ -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,

View file

@ -8,14 +8,14 @@
{% endblock %}
{% block modal-footer %}
<div>
{% csrf_token %}
<input type="hidden" name="id" value="{{ group.id }}">
<button class="button is-danger" type="submit">
{% trans "Delete" %}
</button>
{% trans "Cancel" as button_text %}
{% include 'snippets/toggle/toggle_button.html' with text=button_text controls_text="delete_list" controls_uid=list.id %}
</div>
<form name="delete-group-{{ group.id }}" action="{% url 'delete-group' group.id %}" method="POST">
{% csrf_token %}
<input type="hidden" name="id" value="{{ group.id }}">
<button class="button is-danger" type="submit">
{% trans "Delete" %}
</button>
{% trans "Cancel" as button_text %}
{% include 'snippets/toggle/toggle_button.html' with text=button_text controls_text="delete_group" controls_uid=group.id %}
</form>
{% endblock %}

View file

@ -258,6 +258,7 @@ urlpatterns = [
re_path(
r"^group/(?P<group_id>\d+)(.json)?/?$", views.Group.as_view(), name="group"
),
re_path(r"^group/delete/(?P<group_id>\d+)/?$", views.delete_group, name="delete-group"),
re_path(
r"^group/(?P<group_id>\d+)/add-users/?$",
views.FindUsers.as_view(),

View file

@ -45,6 +45,7 @@ from .group import (
Group,
UserGroups,
FindUsers,
delete_group,
invite_member,
remove_member,
accept_membership,

View file

@ -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)