2021-09-23 21:49:25 +00:00
|
|
|
"""group views"""
|
|
|
|
from typing import Optional
|
|
|
|
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 bookwyrm import forms, models
|
|
|
|
from bookwyrm.connectors import connector_manager
|
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
2021-09-24 04:12:36 +00:00
|
|
|
from .helpers import privacy_filter
|
2021-09-23 21:49:25 +00:00
|
|
|
from .helpers import get_user_from_username
|
|
|
|
|
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)
|
|
|
|
# groups = privacy_filter(
|
|
|
|
# request.user, groups, privacy_levels=["public", "followers"]
|
|
|
|
# )
|
|
|
|
|
2021-09-24 04:12:36 +00:00
|
|
|
|
|
|
|
data = {
|
2021-09-24 10:34:11 +00:00
|
|
|
"group": group,
|
2021-09-24 04:12:36 +00:00
|
|
|
"list_form": forms.GroupForm(),
|
|
|
|
"path": "/group",
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "groups/group.html", data)
|
|
|
|
|
2021-09-24 10:34:11 +00:00
|
|
|
# @method_decorator(login_required, name="dispatch")
|
|
|
|
# # pylint: disable=unused-argument
|
|
|
|
# def post(self, request):
|
|
|
|
# """create a book_list"""
|
|
|
|
# form = forms.ListForm(request.POST)
|
|
|
|
# if not form.is_valid():
|
|
|
|
# return redirect("lists")
|
|
|
|
# book_list = form.save()
|
2021-09-24 04:12:36 +00:00
|
|
|
|
2021-09-24 10:34:11 +00:00
|
|
|
# return redirect(book_list.local_path)
|
2021-09-24 04:12:36 +00:00
|
|
|
|
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 05:04:52 +00:00
|
|
|
# groups = models.GroupMember.objects.filter(user=user)
|
2021-09-24 10:34:11 +00:00
|
|
|
groups = models.Group.objects.filter(members=user)
|
2021-09-24 05:04:52 +00:00
|
|
|
# groups = privacy_filter(request.user, groups)
|
2021-09-23 21:49:25 +00:00
|
|
|
paginated = Paginator(groups, 12)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"user": user,
|
|
|
|
"is_self": request.user.id == user.id,
|
|
|
|
"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
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def create_group(request):
|
|
|
|
"""user groups"""
|
|
|
|
form = forms.GroupForm(request.POST)
|
|
|
|
if not form.is_valid():
|
|
|
|
print("invalid!")
|
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
|
|
|
|
|
|
|
group = form.save()
|
|
|
|
return redirect(group.local_path)
|