From 7c0deabcb29ee05bbd76297c7a20ba94682863ca Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Sat, 25 Sep 2021 11:14:04 +1000 Subject: [PATCH] update urls and group view for searching users to add to group --- bookwyrm/urls.py | 6 ++++-- bookwyrm/views/group.py | 44 ++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index ac7257e33..cdf7493f1 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -251,11 +251,13 @@ urlpatterns = [ name="user-following", ), re_path(r"^hide-suggestions/?$", views.hide_suggestions, name="hide-suggestions"), - # lists - re_path(rf"{USER_PATH}/lists/?$", views.UserLists.as_view(), name="user-lists"), + # groups re_path(rf"{USER_PATH}/groups/?$", views.UserGroups.as_view(), name="user-groups"), re_path(r"^create-group/?$", views.create_group, name="create-group"), re_path(r"^group/(?P\d+)(.json)?/?$", views.Group.as_view(), name="group"), + re_path(r"^group/(?P\d+)/find-users/?$", views.FindAndAddUsers.as_view(), name="group-find-users"), + # lists + re_path(rf"{USER_PATH}/lists/?$", views.UserLists.as_view(), name="user-lists"), re_path(r"^list/?$", views.Lists.as_view(), name="lists"), re_path(r"^list/saved/?$", views.SavedLists.as_view(), name="saved-lists"), re_path(r"^list/(?P\d+)(.json)?/?$", views.List.as_view(), name="list"), diff --git a/bookwyrm/views/group.py b/bookwyrm/views/group.py index fd4e7f2cd..c344f92bc 100644 --- a/bookwyrm/views/group.py +++ b/bookwyrm/views/group.py @@ -5,20 +5,17 @@ from urllib.parse import urlencode from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied from django.core.paginator import Paginator -from django.db import IntegrityError, transaction -from django.db.models import Avg, Count, DecimalField, Q, Max -from django.db.models.functions import Coalesce from django.http import HttpResponseNotFound, HttpResponseBadRequest, HttpResponse from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse -from django.urls import reverse from django.utils.decorators import method_decorator from django.views import View from django.views.decorators.http import require_POST +from django.contrib.postgres.search import TrigramSimilarity +from django.db.models.functions import Greatest from bookwyrm import forms, models -from bookwyrm.connectors import connector_manager -from bookwyrm.settings import PAGE_LENGTH +from bookwyrm.suggested_users import suggested_users from .helpers import privacy_filter from .helpers import get_user_from_username @@ -60,6 +57,39 @@ class UserGroups(View): } return TemplateResponse(request, "user/groups.html", data) +@method_decorator(login_required, name="dispatch") +class FindAndAddUsers(View): + """find friends to add to your group""" + """this is taken from the Get Started friend finder""" + + def get(self, request, group_id): + """basic profile info""" + query = request.GET.get("query") + user_results = ( + models.User.viewer_aware_objects(request.user) + .annotate( + similarity=Greatest( + TrigramSimilarity("username", query), + TrigramSimilarity("localname", query), + ) + ) + .filter( + similarity__gt=0.5, + ) + .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 + ) + + data["suggested_users"] = user_results + data["group"] = get_object_or_404(models.Group, id=group_id) + data["query"] = query + return TemplateResponse(request, "groups/find_users.html", data) + @login_required @require_POST def create_group(request): @@ -70,6 +100,6 @@ def create_group(request): return redirect(request.headers.get("Referer", "/")) group = form.save() - # TODO: add user as group member + # add the creator as a group member models.GroupMember.objects.create(group=group, user=request.user) return redirect(group.local_path) \ No newline at end of file