2021-03-31 20:56:26 +00:00
|
|
|
""" Helping new users figure out the lay of the land """
|
2021-04-01 16:02:48 +00:00
|
|
|
import re
|
|
|
|
|
2021-03-31 20:56:26 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-04-01 16:39:05 +00:00
|
|
|
from django.contrib.postgres.search import TrigramSimilarity
|
|
|
|
from django.db.models.functions import Greatest
|
2021-03-31 21:53:00 +00:00
|
|
|
from django.db.models import Count, Q
|
2021-04-01 16:02:48 +00:00
|
|
|
from django.http import HttpResponseNotFound
|
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-03-31 20:56:26 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
|
2021-03-31 21:53:00 +00:00
|
|
|
from bookwyrm import forms, models
|
2021-03-31 20:56:26 +00:00
|
|
|
from bookwyrm.connectors import connector_manager
|
2021-03-31 21:53:00 +00:00
|
|
|
from .helpers import get_suggested_users
|
2021-04-01 15:32:06 +00:00
|
|
|
from .user import save_user_form
|
2021-03-31 20:56:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-03-31 21:53:00 +00:00
|
|
|
class GetStartedProfile(View):
|
|
|
|
""" tell us about yourself """
|
|
|
|
|
2021-04-01 16:02:48 +00:00
|
|
|
next_view = "get-started-books"
|
|
|
|
|
2021-03-31 21:53:00 +00:00
|
|
|
def get(self, request):
|
|
|
|
""" basic profile info """
|
|
|
|
data = {
|
2021-04-01 15:32:06 +00:00
|
|
|
"form": forms.LimitedEditUserForm(instance=request.user),
|
2021-04-01 16:02:48 +00:00
|
|
|
"next": self.next_view,
|
2021-03-31 21:53:00 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "get_started/profile.html", data)
|
|
|
|
|
2021-04-01 15:32:06 +00:00
|
|
|
def post(self, request):
|
|
|
|
""" update your profile """
|
|
|
|
form = forms.LimitedEditUserForm(
|
|
|
|
request.POST, request.FILES, instance=request.user
|
|
|
|
)
|
|
|
|
if not form.is_valid():
|
|
|
|
data = {"form": form, "next": "get-started-books"}
|
|
|
|
return TemplateResponse(request, "get_started/profile.html", data)
|
|
|
|
save_user_form(form)
|
2021-04-01 16:02:48 +00:00
|
|
|
return redirect(self.next_view)
|
2021-04-01 15:32:06 +00:00
|
|
|
|
2021-03-31 21:53:00 +00:00
|
|
|
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class GetStartedBooks(View):
|
|
|
|
""" name a book, any book, we gotta start somewhere """
|
2021-03-31 20:56:26 +00:00
|
|
|
|
2021-04-01 16:02:48 +00:00
|
|
|
next_view = "get-started-users"
|
|
|
|
|
2021-03-31 20:56:26 +00:00
|
|
|
def get(self, request):
|
|
|
|
""" info about a book """
|
2021-03-31 21:53:00 +00:00
|
|
|
query = request.GET.get("query")
|
2021-04-02 14:11:35 +00:00
|
|
|
book_results = popular_books = []
|
2021-03-31 20:56:26 +00:00
|
|
|
if query:
|
|
|
|
book_results = connector_manager.local_search(query, raw=True)[:5]
|
|
|
|
if len(book_results) < 5:
|
2021-03-31 21:53:00 +00:00
|
|
|
popular_books = (
|
|
|
|
models.Edition.objects.exclude(
|
2021-04-01 15:12:38 +00:00
|
|
|
# exclude already shelved
|
|
|
|
Q(
|
|
|
|
parent_work__in=[
|
|
|
|
b.book.parent_work
|
|
|
|
for b in request.user.shelfbook_set.distinct().all()
|
|
|
|
]
|
|
|
|
)
|
2021-04-01 16:02:48 +00:00
|
|
|
| Q( # and exclude if it's already in search results
|
|
|
|
parent_work__in=[b.parent_work for b in book_results]
|
|
|
|
)
|
2021-03-31 21:53:00 +00:00
|
|
|
)
|
|
|
|
.annotate(Count("shelfbook"))
|
|
|
|
.order_by("-shelfbook__count")[: 5 - len(book_results)]
|
|
|
|
)
|
2021-03-31 20:56:26 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"book_results": book_results,
|
|
|
|
"popular_books": popular_books,
|
2021-04-01 16:02:48 +00:00
|
|
|
"next": self.next_view,
|
2021-03-31 20:56:26 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "get_started/books.html", data)
|
2021-03-31 21:53:00 +00:00
|
|
|
|
2021-04-01 16:02:48 +00:00
|
|
|
def post(self, request):
|
|
|
|
""" shelve some books """
|
|
|
|
shelve_actions = [
|
|
|
|
(k, v)
|
|
|
|
for k, v in request.POST.items()
|
|
|
|
if re.match(r"\d+", k) and re.match(r"\d+", v)
|
|
|
|
]
|
|
|
|
for (book_id, shelf_id) in shelve_actions:
|
|
|
|
book = get_object_or_404(models.Edition, id=book_id)
|
|
|
|
shelf = get_object_or_404(models.Shelf, id=shelf_id)
|
|
|
|
if shelf.user != request.user:
|
|
|
|
# hmmmmm
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
models.ShelfBook.objects.create(book=book, shelf=shelf, user=request.user)
|
|
|
|
return redirect(self.next_view)
|
|
|
|
|
2021-03-31 21:53:00 +00:00
|
|
|
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class GetStartedUsers(View):
|
|
|
|
""" find friends """
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
""" basic profile info """
|
2021-04-01 16:39:05 +00:00
|
|
|
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,
|
2021-03-31 21:53:00 +00:00
|
|
|
)
|
2021-04-01 16:39:05 +00:00
|
|
|
.order_by("-similarity")[:5]
|
2021-03-31 21:53:00 +00:00
|
|
|
)
|
2021-04-01 16:39:05 +00:00
|
|
|
|
|
|
|
if user_results.count() < 5:
|
2021-04-02 02:56:53 +00:00
|
|
|
suggested_users = get_suggested_users(request.user)
|
|
|
|
|
2021-03-31 21:53:00 +00:00
|
|
|
data = {
|
2021-04-01 16:39:05 +00:00
|
|
|
"suggested_users": list(user_results) + list(suggested_users),
|
2021-03-31 21:53:00 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "get_started/users.html", data)
|