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.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-05-22 21:05:59 +00:00
|
|
|
from bookwyrm.suggested_users import suggested_users
|
2021-09-28 18:23:47 +00:00
|
|
|
from .preferences.edit_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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""tell us about yourself"""
|
2021-03-31 21:53:00 +00:00
|
|
|
|
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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""basic profile info"""
|
2021-03-31 21:53:00 +00:00
|
|
|
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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""update your profile"""
|
2021-04-01 15:32:06 +00:00
|
|
|
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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelve some books"""
|
2021-04-01 16:02:48 +00:00
|
|
|
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)
|
2021-09-27 23:04:40 +00:00
|
|
|
shelf.raise_not_editable(request.user)
|
|
|
|
|
2021-04-01 16:02:48 +00:00
|
|
|
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):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""find friends"""
|
2021-03-31 21:53:00 +00:00
|
|
|
|
|
|
|
def get(self, request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""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-05-22 21:05:59 +00:00
|
|
|
data = {"no_results": not user_results}
|
2021-04-01 16:39:05 +00:00
|
|
|
|
|
|
|
if user_results.count() < 5:
|
2021-05-22 21:05:59 +00:00
|
|
|
user_results = list(user_results) + suggested_users.get_suggestions(
|
|
|
|
request.user
|
|
|
|
)
|
2021-04-02 02:56:53 +00:00
|
|
|
|
2021-05-22 21:05:59 +00:00
|
|
|
data["suggested_users"] = user_results
|
2021-05-22 16:55:38 +00:00
|
|
|
return TemplateResponse(request, "get_started/users.html", data)
|