2021-03-31 16:56:26 -04:00
|
|
|
""" Helping new users figure out the lay of the land """
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-03-31 17:53:00 -04:00
|
|
|
from django.db.models import Count, Q
|
2021-03-31 16:56:26 -04:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
|
|
|
|
2021-03-31 17:53:00 -04:00
|
|
|
from bookwyrm import forms, models
|
2021-03-31 16:56:26 -04:00
|
|
|
from bookwyrm.connectors import connector_manager
|
2021-03-31 17:53:00 -04:00
|
|
|
from .helpers import get_suggested_users
|
2021-03-31 16:56:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-03-31 17:53:00 -04:00
|
|
|
class GetStartedProfile(View):
|
|
|
|
""" tell us about yourself """
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
""" basic profile info """
|
|
|
|
data = {
|
|
|
|
"form": forms.EditUserForm(instance=request.user),
|
|
|
|
"next": "get-started-books",
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "get_started/profile.html", data)
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class GetStartedBooks(View):
|
|
|
|
""" name a book, any book, we gotta start somewhere """
|
2021-03-31 16:56:26 -04:00
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
""" info about a book """
|
2021-03-31 17:53:00 -04:00
|
|
|
query = request.GET.get("query")
|
2021-03-31 16:56:26 -04:00
|
|
|
book_results = []
|
|
|
|
if query:
|
|
|
|
book_results = connector_manager.local_search(query, raw=True)[:5]
|
|
|
|
if len(book_results) < 5:
|
2021-03-31 17:53:00 -04:00
|
|
|
popular_books = (
|
|
|
|
models.Edition.objects.exclude(
|
2021-04-01 11:12:38 -04:00
|
|
|
# exclude already shelved
|
|
|
|
Q(
|
|
|
|
parent_work__in=[
|
|
|
|
b.book.parent_work
|
|
|
|
for b in request.user.shelfbook_set.distinct().all()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
| # - or if it's already in search results
|
|
|
|
Q(parent_work__in=[b.parent_work for b in book_results])
|
2021-03-31 17:53:00 -04:00
|
|
|
)
|
|
|
|
.annotate(Count("shelfbook"))
|
|
|
|
.order_by("-shelfbook__count")[: 5 - len(book_results)]
|
|
|
|
)
|
2021-03-31 16:56:26 -04:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"book_results": book_results,
|
|
|
|
"popular_books": popular_books,
|
2021-03-31 17:53:00 -04:00
|
|
|
"next": "get-started-users",
|
2021-03-31 16:56:26 -04:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, "get_started/books.html", data)
|
2021-03-31 17:53:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class GetStartedUsers(View):
|
|
|
|
""" find friends """
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
""" basic profile info """
|
|
|
|
suggested_users = (
|
|
|
|
get_suggested_users(
|
|
|
|
request.user,
|
|
|
|
~Q(id=request.user.id),
|
|
|
|
~Q(followers=request.user),
|
|
|
|
bookwyrm_user=True,
|
|
|
|
)
|
|
|
|
.order_by("shared_books", "-mutuals", "-last_active_date")
|
|
|
|
.all()[:5]
|
|
|
|
)
|
|
|
|
data = {
|
|
|
|
"suggested_users": suggested_users,
|
|
|
|
"next": "get-started-profile",
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "get_started/users.html", data)
|