From dafda649f121e3f66c71def111371fd224338b17 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Mon, 4 Oct 2021 21:31:28 +1100 Subject: [PATCH] run Black on changed code --- bookwyrm/forms.py | 2 + bookwyrm/models/base_model.py | 5 +- bookwyrm/models/group.py | 46 ++++++-------- bookwyrm/models/list.py | 21 ++++--- bookwyrm/templatetags/bookwyrm_group_tags.py | 6 +- bookwyrm/urls.py | 26 ++++++-- bookwyrm/views/__init__.py | 10 ++- bookwyrm/views/group.py | 66 +++++++++++--------- bookwyrm/views/list.py | 12 +++- bookwyrm/views/user.py | 6 +- 10 files changed, 118 insertions(+), 82 deletions(-) diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index 7ab923fd8..2084b7665 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -294,11 +294,13 @@ class ListForm(CustomForm): model = models.List fields = ["user", "name", "description", "curation", "privacy", "group"] + class GroupForm(CustomForm): class Meta: model = models.Group fields = ["user", "privacy", "name", "description"] + class ReportForm(CustomForm): class Meta: model = models.Report diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 5921d7731..d52c368ec 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -82,7 +82,10 @@ class BookWyrmModel(models.Model): return True # you can see groups of which you are a member - if hasattr(self, "memberships") and self.memberships.filter(user=viewer).exists(): + if ( + hasattr(self, "memberships") + and self.memberships.filter(user=viewer).exists() + ): return True # you can see objects which have a group of which you are a member diff --git a/bookwyrm/models/group.py b/bookwyrm/models/group.py index 3e76a6b7b..9a5d3a6c1 100644 --- a/bookwyrm/models/group.py +++ b/bookwyrm/models/group.py @@ -6,14 +6,15 @@ from bookwyrm.settings import DOMAIN from .base_model import BookWyrmModel from . import fields from .relationship import UserBlocks + # from .user import User + class Group(BookWyrmModel): """A group of users""" name = fields.CharField(max_length=100) - user = fields.ForeignKey( - "User", on_delete=models.PROTECT) + user = fields.ForeignKey("User", on_delete=models.PROTECT) description = fields.TextField(blank=True, null=True) privacy = fields.PrivacyField() @@ -21,26 +22,22 @@ class Group(BookWyrmModel): """don't want the user to be in there in this case""" return f"https://{DOMAIN}/group/{self.id}" + class GroupMember(models.Model): """Users who are members of a group""" + created_date = models.DateTimeField(auto_now_add=True) updated_date = models.DateTimeField(auto_now=True) group = models.ForeignKey( - "Group", - on_delete=models.CASCADE, - related_name="memberships" + "Group", on_delete=models.CASCADE, related_name="memberships" ) user = models.ForeignKey( - "User", - on_delete=models.CASCADE, - related_name="memberships" - ) + "User", on_delete=models.CASCADE, related_name="memberships" + ) class Meta: constraints = [ - models.UniqueConstraint( - fields=["group", "user"], name="unique_membership" - ) + models.UniqueConstraint(fields=["group", "user"], name="unique_membership") ] def save(self, *args, **kwargs): @@ -76,32 +73,25 @@ class GroupMember(models.Model): class GroupMemberInvitation(models.Model): """adding a user to a group requires manual confirmation""" + created_date = models.DateTimeField(auto_now_add=True) group = models.ForeignKey( - "Group", - on_delete=models.CASCADE, - related_name="user_invitations" + "Group", on_delete=models.CASCADE, related_name="user_invitations" ) user = models.ForeignKey( - "User", - on_delete=models.CASCADE, - related_name="group_invitations" - ) + "User", on_delete=models.CASCADE, related_name="group_invitations" + ) class Meta: constraints = [ - models.UniqueConstraint( - fields=["group", "user"], name="unique_invitation" - ) + models.UniqueConstraint(fields=["group", "user"], name="unique_invitation") ] + def save(self, *args, **kwargs): # pylint: disable=arguments-differ """make sure the membership doesn't already exist""" # if there's an invitation for a membership that already exists, accept it # without changing the local database state - if GroupMember.objects.filter( - user=self.user, - group=self.group - ).exists(): + if GroupMember.objects.filter(user=self.user, group=self.group).exists(): self.accept() return @@ -138,7 +128,7 @@ class GroupMemberInvitation(models.Model): GroupMember.from_request(self) model = apps.get_model("bookwyrm.Notification", require_ready=True) - # tell the group owner + # tell the group owner model.objects.create( user=self.group.user, related_user=self.user, @@ -148,7 +138,7 @@ class GroupMemberInvitation(models.Model): # let the other members know about it for membership in self.group.memberships.all(): - member = membership.user + member = membership.user if member != self.user and member != self.group.user: model.objects.create( user=member, diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index 19f9e4f56..46d57c2d0 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -14,12 +14,7 @@ from .group import GroupMember CurationType = models.TextChoices( "Curation", - [ - "closed", - "open", - "curated", - "group" - ], + ["closed", "open", "curated", "group"], ) @@ -70,11 +65,14 @@ class List(OrderedCollectionMixin, BookWyrmModel): if self.user == viewer: return # group members can edit items in group lists - is_group_member = GroupMember.objects.filter(group=self.group, user=viewer).exists() + is_group_member = GroupMember.objects.filter( + group=self.group, user=viewer + ).exists() if is_group_member: return super().raise_not_editable(viewer) + class ListItem(CollectionItemMixin, BookWyrmModel): """ok""" @@ -119,14 +117,17 @@ class ListItem(CollectionItemMixin, BookWyrmModel): user=membership.user, related_user=self.user, related_list_item=self, - notification_type="ADD" - ) + notification_type="ADD", + ) + def raise_not_deletable(self, viewer): """the associated user OR the list owner can delete""" if self.book_list.user == viewer: return # group members can delete items in group lists - is_group_member = GroupMember.objects.filter(group=self.book_list.group, user=viewer).exists() + is_group_member = GroupMember.objects.filter( + group=self.book_list.group, user=viewer + ).exists() if is_group_member: return super().raise_not_deletable(viewer) diff --git a/bookwyrm/templatetags/bookwyrm_group_tags.py b/bookwyrm/templatetags/bookwyrm_group_tags.py index eabca2b41..fde7997e8 100644 --- a/bookwyrm/templatetags/bookwyrm_group_tags.py +++ b/bookwyrm/templatetags/bookwyrm_group_tags.py @@ -12,14 +12,16 @@ def has_groups(user): return models.GroupMember.objects.filter(user=user).exists() + @register.filter(name="is_member") def is_member(group, user): """whether or not the user is a member of this group""" - return models.GroupMember.objects.filter(group=group,user=user).exists() + return models.GroupMember.objects.filter(group=group, user=user).exists() + @register.filter(name="is_invited") def is_invited(group, user): """whether or not the user has a pending invitation to join this group""" - return models.GroupMemberInvitation.objects.filter(group=group,user=user).exists() \ No newline at end of file + return models.GroupMemberInvitation.objects.filter(group=group, user=user).exists() diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 4aea7907b..49c47ef5a 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -255,12 +255,28 @@ urlpatterns = [ re_path(r"^hide-suggestions/?$", views.hide_suggestions, name="hide-suggestions"), # groups re_path(rf"{USER_PATH}/groups/?$", views.UserGroups.as_view(), name="user-groups"), - re_path(r"^group/(?P\d+)(.json)?/?$", views.Group.as_view(), name="group"), - re_path(r"^group/(?P\d+)/add-users/?$", views.FindUsers.as_view(), name="group-find-users"), + re_path( + r"^group/(?P\d+)(.json)?/?$", views.Group.as_view(), name="group" + ), + re_path( + r"^group/(?P\d+)/add-users/?$", + views.FindUsers.as_view(), + name="group-find-users", + ), re_path(r"^add-group-member/?$", views.invite_member, name="invite-group-member"), - re_path(r"^remove-group-member/?$", views.remove_member, name="remove-group-member"), - re_path(r"^accept-group-invitation/?$", views.accept_membership, name="accept-group-invitation"), - re_path(r"^reject-group-invitation/?$", views.reject_membership, name="reject-group-invitation"), + re_path( + r"^remove-group-member/?$", views.remove_member, name="remove-group-member" + ), + re_path( + r"^accept-group-invitation/?$", + views.accept_membership, + name="accept-group-invitation", + ), + re_path( + r"^reject-group-invitation/?$", + views.reject_membership, + name="reject-group-invitation", + ), # lists re_path(rf"{USER_PATH}/lists/?$", views.UserLists.as_view(), name="user-lists"), re_path(r"^list/?$", views.Lists.as_view(), name="lists"), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 9aa88664c..08b846dc8 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -41,7 +41,15 @@ from .follow import follow, unfollow from .follow import accept_follow_request, delete_follow_request from .get_started import GetStartedBooks, GetStartedProfile, GetStartedUsers from .goal import Goal, hide_goal -from .group import Group, UserGroups, FindUsers, invite_member, remove_member, accept_membership, reject_membership +from .group import ( + Group, + UserGroups, + FindUsers, + invite_member, + remove_member, + accept_membership, + reject_membership, +) from .import_data import Import, ImportStatus from .inbox import Inbox from .interaction import Favorite, Unfavorite, Boost, Unboost diff --git a/bookwyrm/views/group.py b/bookwyrm/views/group.py index c184e4957..fc4b64041 100644 --- a/bookwyrm/views/group.py +++ b/bookwyrm/views/group.py @@ -18,6 +18,7 @@ from .helpers import privacy_filter from .helpers import get_user_from_username from bookwyrm.settings import DOMAIN + class Group(View): """group page""" @@ -47,7 +48,8 @@ class Group(View): if not form.is_valid(): return redirect("group", user_group.id) user_group = form.save() - return redirect("group", user_group.id) + return redirect("group", user_group.id) + @method_decorator(login_required, name="dispatch") class UserGroups(View): @@ -56,7 +58,9 @@ class UserGroups(View): def get(self, request, username): """display a group""" user = get_user_from_username(request.user, username) - memberships = models.GroupMember.objects.filter(user=user).all().order_by("-updated_date") + memberships = ( + models.GroupMember.objects.filter(user=user).all().order_by("-updated_date") + ) paginated = Paginator(memberships, 12) data = { @@ -80,10 +84,12 @@ class UserGroups(View): models.GroupMember.objects.create(group=group, user=request.user) return redirect("group", group.id) + @method_decorator(login_required, name="dispatch") class FindUsers(View): """find friends to add to your group""" - """this is mostly borrowed from the Get Started friend finder""" + + #this is mostly borrowed from the Get Started friend finder def get(self, request, group_id): """basic profile info""" @@ -91,25 +97,23 @@ class FindUsers(View): group = models.Group.objects.get(id=group_id) user_results = ( models.User.viewer_aware_objects(request.user) - .exclude(memberships__in=group.memberships.all()) # don't suggest users who are already members + .exclude( + memberships__in=group.memberships.all() + ) # don't suggest users who are already members .annotate( similarity=Greatest( TrigramSimilarity("username", query), TrigramSimilarity("localname", query), ) ) - .filter( - similarity__gt=0.5, - local=True - ) + .filter(similarity__gt=0.5, local=True) .order_by("-similarity")[:5] ) data = {"no_results": not user_results} if user_results.count() < 5: user_results = list(user_results) + suggested_users.get_suggestions( - request.user, - local=True + request.user, local=True ) group = get_object_or_404(models.Group, id=group_id) @@ -121,13 +125,14 @@ class FindUsers(View): return HttpResponseBadRequest() data = { - "suggested_users": user_results, - "group": group, - "query": query, - "requestor_is_manager": request.user == group.user + "suggested_users": user_results, + "group": group, + "query": query, + "requestor_is_manager": request.user == group.user, } return TemplateResponse(request, "groups/find_users.html", data) + @require_POST @login_required def invite_member(request): @@ -145,16 +150,14 @@ def invite_member(request): return HttpResponseBadRequest() try: - models.GroupMemberInvitation.objects.create( - user=user, - group=group - ) + models.GroupMemberInvitation.objects.create(user=user, group=group) except IntegrityError: pass return redirect(user.local_path) + @require_POST @login_required def remove_member(request): @@ -168,15 +171,16 @@ def remove_member(request): if not user: return HttpResponseBadRequest() - is_member = models.GroupMember.objects.filter(group=group,user=user).exists() - is_invited = models.GroupMemberInvitation.objects.filter(group=group,user=user).exists() + is_member = models.GroupMember.objects.filter(group=group, user=user).exists() + is_invited = models.GroupMemberInvitation.objects.filter( + group=group, user=user + ).exists() if is_invited: try: invitation = models.GroupMemberInvitation.objects.get( - user=user, - group=group - ) + user=user, group=group + ) invitation.reject() @@ -186,11 +190,13 @@ def remove_member(request): if is_member: try: - membership = models.GroupMember.objects.get(group=group,user=user) + membership = models.GroupMember.objects.get(group=group, user=user) membership.delete() # remove this user's group-curated lists from the group - models.List.objects.filter(group=group,user=user).update(group=None,curation="closed") + models.List.objects.filter(group=group, user=user).update( + group=None, curation="closed" + ) except IntegrityError: pass @@ -200,7 +206,7 @@ def remove_member(request): notification_type = "LEAVE" if user == request.user else "REMOVE" # let the other members know about it for membership in memberships: - member = membership.user + member = membership.user if member != request.user: model.objects.create( user=member, @@ -219,6 +225,7 @@ def remove_member(request): return redirect(user.local_path) + @require_POST @login_required def accept_membership(request): @@ -228,7 +235,7 @@ def accept_membership(request): if not group: return HttpResponseBadRequest() - invite = models.GroupMemberInvitation.objects.get(group=group,user=request.user) + invite = models.GroupMemberInvitation.objects.get(group=group, user=request.user) if not invite: return HttpResponseBadRequest() @@ -240,6 +247,7 @@ def accept_membership(request): return redirect(group.local_path) + @require_POST @login_required def reject_membership(request): @@ -249,7 +257,7 @@ def reject_membership(request): if not group: return HttpResponseBadRequest() - invite = models.GroupMemberInvitation.objects.get(group=group,user=request.user) + invite = models.GroupMemberInvitation.objects.get(group=group, user=request.user) if not invite: return HttpResponseBadRequest() @@ -259,4 +267,4 @@ def reject_membership(request): except IntegrityError: pass - return redirect(request.user.local_path) \ No newline at end of file + return redirect(request.user.local_path) diff --git a/bookwyrm/views/list.py b/bookwyrm/views/list.py index 88f4d6d82..a312a755e 100644 --- a/bookwyrm/views/list.py +++ b/bookwyrm/views/list.py @@ -183,7 +183,7 @@ class List(View): "query": query or "", "sort_form": forms.SortListForm( {"direction": direction, "sort_by": sort_by} - ) + ), } return TemplateResponse(request, "lists/list.html", data) @@ -287,14 +287,20 @@ def add_book(request): book_list = get_object_or_404(models.List, id=request.POST.get("list")) is_group_member = False if book_list.curation == "group": - is_group_member = models.GroupMember.objects.filter(group=book_list.group, user=request.user).exists() + is_group_member = models.GroupMember.objects.filter( + group=book_list.group, user=request.user + ).exists() book_list.raise_visible_to_user(request.user) book = get_object_or_404(models.Edition, id=request.POST.get("book")) # do you have permission to add to the list? try: - if request.user == book_list.user or is_group_member or book_list.curation == "open": + if ( + request.user == book_list.user + or is_group_member + or book_list.curation == "open" + ): # add the book at the latest order of approved books, before pending books order_max = ( book_list.listitem_set.filter(approved=True).aggregate(Max("order"))[ diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index e85ed3158..808ca7385 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -137,6 +137,7 @@ class Following(View): } return TemplateResponse(request, "user/relationships/following.html", data) + class Groups(View): """list of user's groups view""" @@ -144,9 +145,7 @@ class Groups(View): """list of groups""" user = get_user_from_username(request.user, username) - paginated = Paginator( - models.Group.memberships.filter(user=user) - ) + paginated = Paginator(models.Group.memberships.filter(user=user)) data = { "user": user, "is_self": request.user.id == user.id, @@ -154,6 +153,7 @@ class Groups(View): } return TemplateResponse(request, "user/groups.html", data) + @require_POST @login_required def hide_suggestions(request):