2021-09-23 21:49:25 +00:00
|
|
|
"""group views"""
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-09-25 07:34:44 +00:00
|
|
|
from django.db import IntegrityError
|
2021-09-23 21:49:25 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-09-25 07:34:44 +00:00
|
|
|
from django.http import HttpResponseNotFound, HttpResponseBadRequest
|
2021-09-23 21:49:25 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
from django.views.decorators.http import require_POST
|
2021-09-25 01:14:04 +00:00
|
|
|
from django.contrib.postgres.search import TrigramSimilarity
|
|
|
|
from django.db.models.functions import Greatest
|
2021-09-23 21:49:25 +00:00
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
2021-09-25 01:14:04 +00:00
|
|
|
from bookwyrm.suggested_users import suggested_users
|
2021-09-25 07:34:44 +00:00
|
|
|
from .helpers import privacy_filter # TODO:
|
2021-09-23 21:49:25 +00:00
|
|
|
from .helpers import get_user_from_username
|
2021-09-27 05:34:14 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2021-09-23 21:49:25 +00:00
|
|
|
|
2021-09-24 04:12:36 +00:00
|
|
|
class Group(View):
|
|
|
|
"""group page"""
|
|
|
|
|
2021-09-24 10:34:11 +00:00
|
|
|
def get(self, request, group_id):
|
2021-09-24 04:12:36 +00:00
|
|
|
"""display a group"""
|
|
|
|
|
2021-09-24 10:34:11 +00:00
|
|
|
group = models.Group.objects.get(id=group_id)
|
2021-09-26 05:56:02 +00:00
|
|
|
lists = models.List.objects.filter(group=group).order_by("-updated_date")
|
2021-09-27 05:34:14 +00:00
|
|
|
lists = privacy_filter(request.user, lists)
|
2021-09-24 04:12:36 +00:00
|
|
|
data = {
|
2021-09-24 10:34:11 +00:00
|
|
|
"group": group,
|
2021-09-26 05:56:02 +00:00
|
|
|
"lists": lists,
|
2021-09-24 04:12:36 +00:00
|
|
|
"list_form": forms.GroupForm(),
|
|
|
|
"path": "/group",
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "groups/group.html", data)
|
|
|
|
|
2021-09-27 05:34:14 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def post(self, request, group_id):
|
|
|
|
"""edit a group"""
|
|
|
|
user_group = get_object_or_404(models.Group, id=group_id)
|
|
|
|
form = forms.GroupForm(request.POST, instance=user_group)
|
|
|
|
if not form.is_valid():
|
|
|
|
return redirect("group", user_group.id)
|
|
|
|
user_group = form.save()
|
|
|
|
return redirect("group", user_group.id)
|
|
|
|
|
2021-09-23 21:49:25 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class UserGroups(View):
|
|
|
|
"""a user's groups page"""
|
|
|
|
|
|
|
|
def get(self, request, username):
|
|
|
|
"""display a group"""
|
|
|
|
user = get_user_from_username(request.user, username)
|
2021-09-24 11:57:01 +00:00
|
|
|
groups = models.Group.objects.filter(members=user).order_by("-updated_date")
|
2021-09-27 05:34:14 +00:00
|
|
|
groups = privacy_filter(request.user, groups)
|
2021-09-23 21:49:25 +00:00
|
|
|
paginated = Paginator(groups, 12)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"user": user,
|
2021-09-25 10:25:30 +00:00
|
|
|
"is_self": request.user.id == user.id, # CHECK is this relevant here?
|
2021-09-23 21:49:25 +00:00
|
|
|
"groups": paginated.get_page(request.GET.get("page")),
|
2021-09-24 04:12:36 +00:00
|
|
|
"group_form": forms.GroupForm(),
|
2021-09-24 10:34:11 +00:00
|
|
|
"path": user.local_path + "/group",
|
2021-09-23 21:49:25 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "user/groups.html", data)
|
2021-09-24 10:34:11 +00:00
|
|
|
|
2021-09-27 05:34:14 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def post(self, request, username):
|
|
|
|
"""create a user group"""
|
|
|
|
form = forms.GroupForm(request.POST)
|
|
|
|
if not form.is_valid():
|
2021-09-27 06:39:12 +00:00
|
|
|
return redirect(request.user.local_path + "groups")
|
2021-09-27 05:34:14 +00:00
|
|
|
group = form.save()
|
|
|
|
# add the creator as a group member
|
|
|
|
models.GroupMember.objects.create(group=group, user=request.user)
|
|
|
|
return redirect("group", group.id)
|
|
|
|
|
2021-09-25 01:14:04 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-09-25 07:34:44 +00:00
|
|
|
class FindUsers(View):
|
2021-09-25 01:14:04 +00:00
|
|
|
"""find friends to add to your group"""
|
2021-09-25 07:34:44 +00:00
|
|
|
"""this is mostly taken from the Get Started friend finder"""
|
2021-09-25 01:14:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2021-09-25 10:25:30 +00:00
|
|
|
group = get_object_or_404(models.Group, id=group_id)
|
|
|
|
|
2021-09-25 01:14:04 +00:00
|
|
|
data["suggested_users"] = user_results
|
2021-09-25 10:25:30 +00:00
|
|
|
data["group"] = group
|
2021-09-25 01:14:04 +00:00
|
|
|
data["query"] = query
|
2021-09-27 05:34:14 +00:00
|
|
|
data["requestor_is_manager"] = request.user == group.user
|
2021-09-25 01:14:04 +00:00
|
|
|
return TemplateResponse(request, "groups/find_users.html", data)
|
|
|
|
|
2021-09-25 07:34:44 +00:00
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
def add_member(request):
|
|
|
|
"""add a member to the group"""
|
|
|
|
|
|
|
|
# TODO: if groups become AP values we need something like get_group_from_group_fullname
|
|
|
|
# group = get_object_or_404(models.Group, id=request.POST.get("group"))
|
|
|
|
group = models.Group.objects.get(id=request.POST["group"])
|
|
|
|
if not group:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
user = get_user_from_username(request.user, request.POST["user"])
|
|
|
|
if not user:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2021-09-27 05:34:14 +00:00
|
|
|
if not group.user == request.user:
|
2021-09-25 07:34:44 +00:00
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
models.GroupMember.objects.create(
|
|
|
|
group=group,
|
|
|
|
user=user
|
|
|
|
)
|
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return redirect(user.local_path)
|
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
def remove_member(request):
|
|
|
|
"""remove a member from the group"""
|
|
|
|
|
|
|
|
# TODO: if groups become AP values we need something like get_group_from_group_fullname
|
|
|
|
# group = get_object_or_404(models.Group, id=request.POST.get("group"))
|
|
|
|
group = models.Group.objects.get(id=request.POST["group"])
|
|
|
|
if not group:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
user = get_user_from_username(request.user, request.POST["user"])
|
|
|
|
if not user:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
2021-09-27 05:34:14 +00:00
|
|
|
if not group.user == request.user:
|
2021-09-25 07:34:44 +00:00
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
membership = models.GroupMember.objects.get(group=group,user=user)
|
|
|
|
membership.delete()
|
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
print("no integrity")
|
|
|
|
pass
|
|
|
|
|
|
|
|
return redirect(user.local_path)
|