Merge branch 'main' into book-file-links

This commit is contained in:
Mouse Reeve 2022-01-12 06:39:22 -08:00 committed by GitHub
commit e6d9895854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 384 additions and 198 deletions

View file

@ -492,3 +492,19 @@ class SortListForm(forms.Form):
("descending", _("Descending")),
),
)
class ReadThroughForm(CustomForm):
def clean(self):
"""make sure the email isn't in use by a registered user"""
cleaned_data = super().clean()
start_date = cleaned_data.get("start_date")
finish_date = cleaned_data.get("finish_date")
if start_date > finish_date:
self.add_error(
"finish_date", _("Reading finish date cannot be before start date.")
)
class Meta:
model = models.ReadThrough
fields = ["user", "book", "start_date", "finish_date"]

View file

@ -1,5 +1,6 @@
""" progress in a book """
from django.core import validators
from django.core.cache import cache
from django.db import models
from django.db.models import F, Q
@ -30,6 +31,7 @@ class ReadThrough(BookWyrmModel):
def save(self, *args, **kwargs):
"""update user active time"""
cache.delete(f"latest_read_through-{self.user.id}-{self.book.id}")
self.user.update_active_date()
# an active readthrough must have an unset finish date
if self.finish_date:

View file

@ -1,7 +1,6 @@
""" defines relationships between users """
from django.apps import apps
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import models, transaction, IntegrityError
from django.db.models import Q
@ -41,15 +40,12 @@ class UserRelationship(BookWyrmModel):
def save(self, *args, **kwargs):
"""clear the template cache"""
# invalidate the template cache
cache_keys = [
make_template_fragment_key(
"follow_button", [self.user_subject.id, self.user_object.id]
),
make_template_fragment_key(
"follow_button", [self.user_object.id, self.user_subject.id]
),
]
cache.delete_many(cache_keys)
cache.delete_many(
[
f"relationship-{self.user_subject.id}-{self.user_object.id}",
f"relationship-{self.user_object.id}-{self.user_subject.id}",
]
)
super().save(*args, **kwargs)
class Meta:

View file

@ -755,6 +755,13 @@ ol.ordered-list li::before {
padding: 0 0.75em;
}
/* Notifications page
******************************************************************************/
.notification a.icon {
text-decoration: none !important;
}
/* Breadcrumbs
******************************************************************************/

View file

@ -237,29 +237,21 @@
<h2 class="title is-5">{% trans "Your reading activity" %}</h2>
</div>
<div class="column is-narrow">
{% trans "Add read dates" as button_text %}
{% include 'snippets/toggle/open_button.html' with text=button_text icon_with_text="plus" class="is-small" controls_text="add_readthrough" focus="add_readthrough_focus_" %}
<button class="button is-small" data-modal-open="add-readthrough">
<span class="icon icon-plus m-mobile-0" aria-hidden="true"></span>
<span class="is-sr-only-mobile">
{% trans "Add read dates" %}
</span>
</button>
</div>
</header>
<section class="is-hidden box" id="add_readthrough">
<form name="add-readthrough" action="/create-readthrough" method="post">
{% include 'snippets/readthrough_form.html' with readthrough=None %}
<div class="field is-grouped">
<div class="control">
<button class="button is-primary" type="submit">{% trans "Create" %}</button>
</div>
<div class="control">
{% trans "Cancel" as button_text %}
{% include 'snippets/toggle/close_button.html' with text=button_text controls_text="add_readthrough" %}
</div>
</div>
</form>
</section>
{% include "readthrough/readthrough_modal.html" with id="add-readthrough" %}
{% if not readthroughs.exists %}
<p>{% trans "You don't have any reading activity for this book." %}</p>
{% endif %}
{% for readthrough in readthroughs %}
{% include 'book/readthrough.html' with readthrough=readthrough %}
{% include 'readthrough/readthrough_list.html' with readthrough=readthrough %}
{% endfor %}
</section>
<hr aria-hidden="true">

View file

@ -16,10 +16,7 @@
{% with shelf_counter=forloop.counter %}
<li>
<p>
{% if shelf.identifier == 'to-read' %}{% trans "To Read" %}
{% elif shelf.identifier == 'reading' %}{% trans "Currently Reading" %}
{% elif shelf.identifier == 'read' %}{% trans "Read" %}
{% else %}{{ shelf.name }}{% endif %}
{% include "snippets/translated_shelf_name.html" with shelf=shelf %}
</p>
<div class="tabs is-small is-toggle">
<ul>

View file

@ -1,13 +1,17 @@
{% extends 'landing/layout.html' %}
{% load i18n %}
{% load cache %}
{% load bookwyrm_tags %}
{% block panel %}
<div class="block is-hidden-tablet">
<h2 class="title has-text-centered">{% trans "Recent Books" %}</h2>
</div>
{% cache 60 * 60 %}
{% get_current_language as LANGUAGE_CODE %}
{% cache 60 * 60 LANGUAGE_CODE %}
{% get_landing_books as books %}
<section class="tile is-ancestor">
<div class="tile is-vertical is-6">
<div class="tile is-parent">

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% block primary_link %}{% spaceless %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,9 +1,9 @@
{% load bookwyrm_tags %}
{% related_status notification as related_status %}
<div class="box is-shadowless has-background-white-ter {% if notification.id in unread %} is-primary{% endif %}">
<div class="columns is-mobile">
<div class="column is-narrow is-size-3 {% if notification.id in unread%}has-text-white{% else %}has-text-grey{% endif %}">
<a class="has-text-dark" href="{% block primary_link %}{% endblock %}">
<div class="notification {% if notification.id in unread %}has-background-primary{% endif %}">
<div class="columns is-mobile {% if notification.id in unread %}has-text-white{% else %}has-text-grey{% endif %}">
<div class="column is-narrow is-size-3">
<a class="icon" href="{% block primary_link %}{% endblock %}">
{% block icon %}{% endblock %}
</a>
</div>

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}

View file

@ -1,4 +1,4 @@
{% extends 'notifications/items/item_layout.html' %}
{% extends 'notifications/items/layout.html' %}
{% load i18n %}
{% load utilities %}

View file

@ -0,0 +1,15 @@
{% extends 'layout.html' %}
{% load i18n %}
{% load utilities %}
{% block title %}
{% blocktrans trimmed with title=book|book_title %}
Update read dates for "<em>{{ title }}</em>"
{% endblocktrans %}
{% endblock %}
{% block content %}
{% include "readthrough/readthrough_modal.html" with book=book active=True static=True %}
{% endblock %}

View file

@ -4,6 +4,7 @@
{% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<input type="hidden" name="book" value="{{ book.id }}">
<input type="hidden" name="user" value="{{ request.user.id }}">
<div class="field">
<label class="label" tabindex="0" id="add_readthrough_focus_{{ readthrough.id }}" for="id_start_date_{{ readthrough.id }}">
{% trans "Started reading" %}

View file

@ -3,7 +3,7 @@
{% load tz %}
{% load utilities %}
<div class="content">
<div id="hide_edit_readthrough_{{ readthrough.id }}" class="box is-shadowless has-background-white-bis">
<div class="box is-shadowless has-background-white-bis">
<div class="columns">
<div class="column">
{% trans "Progress Updates:" %}
@ -58,7 +58,11 @@
<div class="field has-addons">
<div class="control">
{% trans "Edit read dates" as button_text %}
{% include 'snippets/toggle/toggle_button.html' with class="is-small" text=button_text icon="pencil" controls_text="edit_readthrough" controls_uid=readthrough.id focus="edit_readthrough" %}
<button class="button is-small" type="button" data-modal-open="edit_readthrough_{{ readthrough.id }}">
<span class="icon icon-pencil" title="{{ button_text }}">
<span class="is-sr-only">{{ button_text }}</span>
</span>
</button>
</div>
<div class="control">
{% trans "Delete these read dates" as button_text %}
@ -74,16 +78,7 @@
</div>
</div>
<div class="box is-hidden" id="edit_readthrough_{{ readthrough.id }}" tabindex="0">
<h3 class="title is-5">{% trans "Edit read dates" %}</h3>
<form name="edit-readthrough" action="/edit-readthrough" method="post">
{% include 'snippets/readthrough_form.html' with readthrough=readthrough %}
<div class="field is-grouped">
<button class="button is-primary" type="submit">{% trans "Save" %}</button>
{% trans "Cancel" as button_text %}
{% include 'snippets/toggle/close_button.html' with text=button_text controls_text="edit_readthrough" controls_uid=readthrough.id %}
</div>
</form>
</div>
{% join "delete_readthrough" readthrough.id as modal_id %}
{% include 'book/delete_readthrough_modal.html' with id=modal_id %}
{% join "edit_readthrough" readthrough.id as edit_modal_id %}
{% include "readthrough/readthrough_modal.html" with readthrough=readthrough id=edit_modal_id %}
{% join "delete_readthrough" readthrough.id as delete_modal_id %}
{% include 'readthrough/delete_readthrough_modal.html' with id=delete_modal_id %}

View file

@ -0,0 +1,80 @@
{% extends "components/modal.html" %}
{% load i18n %}
{% load utilities %}
{% block modal-title %}
{% if readthrough %}
{% blocktrans trimmed with title=book|book_title %}
Update read dates for "<em>{{ title }}</em>"
{% endblocktrans %}
{% else %}
{% blocktrans trimmed with title=book|book_title %}
Add read dates for "<em>{{ title }}</em>"
{% endblocktrans %}
{% endif %}
{% endblock %}
{% block modal-form-open %}
<form name="add-readthrough-{{ readthrough.id }}" action="/create-readthrough" method="post">
{% endblock %}
{% block modal-body %}
{% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<input type="hidden" name="book" value="{{ book.id }}">
<input type="hidden" name="user" value="{{ request.user.id }}">
<div class="field">
<label class="label" tabindex="0" id="add_readthrough_focus_{{ readthrough.id }}" for="id_start_date_{{ readthrough.id }}">
{% trans "Started reading" %}
</label>
{% firstof form.start_date.value readthrough.start_date|date:"Y-m-d" as value %}
<input
type="date"
name="start_date"
class="input"
id="id_start_date_{{ readthrough.id }}"
value="{{ value }}"
aria-describedby="desc_start_date"
>
{% include 'snippets/form_errors.html' with errors_list=form.start_date.errors id="desc_start_date" %}
</div>
{# Only show progress for editing existing readthroughs #}
{% if readthrough.id and not readthrough.finish_date %}
{% join "id_progress" readthrough.id as field_id %}
<label class="label" for="{{ field_id }}">
{% trans "Progress" %}
</label>
{% include "snippets/progress_field.html" with id=field_id %}
{% endif %}
<div class="field">
<label class="label" for="id_finish_date_{{ readthrough.id }}">
{% trans "Finished reading" %}
</label>
{% firstof form.finish_date.value readthrough.finish_date|date:"Y-m-d" as value %}
<input
type="date"
name="finish_date"
class="input"
id="id_finish_date_{{ readthrough.id }}"
value="{{ value }}"
aria-describedby="desc_finish_date"
>
{% include 'snippets/form_errors.html' with errors_list=form.finish_date.errors id="desc_finish_date" %}
</div>
{% endblock %}
{% block modal-footer %}
<button class="button is-primary" type="submit">{% trans "Save" %}</button>
{% if not static %}
<button type="button" class="button" data-modal-close>{% trans "Cancel" %}</button>
{% endif %}
{% endblock %}
{% block modal-form-close %}
</form>
{% endblock %}

View file

@ -75,9 +75,11 @@
<form class="mt-1" action="/resolve-book" method="post">
{% csrf_token %}
<input type="hidden" name="remote_id" value="{{ result.key }}">
<button type="submit" class="button is-small is-link">
{% trans "Import book" %}
</button>
<div class="control">
<button type="submit" class="button is-small is-link">
{% trans "Import book" %}
</button>
</div>
</form>
</div>
</div>

View file

@ -19,6 +19,17 @@
</h1>
</header>
<nav class="breadcrumb subtitle" aria-label="breadcrumbs">
<ul>
<li><a href="{% url 'user-feed' user|username %}">{% trans "User profile" %}</a></li>
<li class="is-active">
<a href="#" aria-current="page">
{% include "snippets/translated_shelf_name.html" with shelf=shelf %}
</a>
</li>
</ul>
</nav>
<nav class="block columns is-mobile scroll-x">
<div class="column pr-0">
<div class="tabs">
@ -34,15 +45,7 @@
href="{{ shelf_tab.local_path }}"
{% if shelf_tab.identifier == shelf.identifier %} aria-current="page"{% endif %}
>
{% if shelf_tab.identifier == 'to-read' %}
{% trans "To Read" %}
{% elif shelf_tab.identifier == 'reading' %}
{% trans "Currently Reading" %}
{% elif shelf_tab.identifier == 'read' %}
{% trans "Read" %}
{% else %}
{{ shelf_tab.name }}
{% endif %}
{% include 'user/books_header.html' with shelf=shelf_tab %}
</a>
</li>
{% endfor %}

View file

@ -3,16 +3,15 @@
<summary class="is-flex is-align-items-center is-flex-wrap-wrap is-gap-2">
<span class="mb-0 title {% if size == 'small' %}is-6{% else %}is-5{% endif %} is-flex-shrink-0">
{% trans "Filters" %}
</span>
{% if filters_applied %}
<span class="tag is-success is-light ml-2 mb-0 is-{{ size|default:'normal' }}">
{{ _("Filters are applied") }}
{% trans "Filters are applied" %}
</span>
{% endif %}
{% if request.GET %}
{% if method != "post" and request.GET %}
<span class="mb-0 tags has-addons">
<span class="mb-0 tag is-success is-light is-{{ size|default:'normal' }}">
{% trans "Filters are applied" %}

View file

@ -1,13 +1,18 @@
{% load i18n %}
{% load interaction %}
{% if request.user == user or not request.user.is_authenticated %}
{% elif user in request.user.blocks.all %}
{# nothing to see here -- either it's yourself or your logged out #}
{% else %}
{% get_relationship user as relationship %}
{% if relationship.is_blocked %}
{% include 'snippets/block_button.html' with blocks=True %}
{% else %}
<div class="field{% if not minimal %} has-addons{% else %} mb-0{% endif %}">
<div class="control">
<form action="{% url 'follow' %}" method="POST" class="interaction follow_{{ user.id }} {% if request.user in user.followers.all or request.user in user.follower_requests.all %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
<form action="{% url 'follow' %}" method="POST" class="interaction follow_{{ user.id }} {% if relationship.is_following or relationship.is_follow_pending %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}">
<button class="button is-small{% if not minimal %} is-link{% endif %}" type="submit">
@ -18,10 +23,10 @@
{% endif %}
</button>
</form>
<form action="{% url 'unfollow' %}" method="POST" class="interaction follow_{{ user.id }} {% if not request.user in user.followers.all and not request.user in user.follower_requests.all %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
<form action="{% url 'unfollow' %}" method="POST" class="interaction follow_{{ user.id }} {% if not relationship.is_following and not relationship.is_follow_pending %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}">
{% if user.manually_approves_followers and request.user not in user.followers.all %}
{% if user.manually_approves_followers and not relationship.is_following %}
<button class="button is-small is-danger is-light" type="submit">
{% trans "Undo follow request" %}
</button>
@ -42,4 +47,7 @@
</div>
{% endif %}
</div>
{% endif %}
{% endif %}

View file

@ -25,10 +25,7 @@
<button class="button is-fullwidth is-small shelf-option is-radiusless is-white" type="submit" {% if shelf.identifier == current.identifier %}disabled{% endif %}>
<span>
{% if shelf.identifier == 'to-read' %}{% trans "To Read" %}
{% elif shelf.identifier == 'reading' %}{% trans "Currently Reading" %}
{% elif shelf.identifier == 'read' %}{% trans "Read" %}
{% else %}{{ shelf.name }}{% endif %}
{% include "snippets/translated_shelf_name.html" with shelf=shelf %}
</span>
</button>
</form>
@ -41,20 +38,23 @@
{% trans "Start reading" as button_text %}
{% url 'reading-status' 'start' book.id as fallback_url %}
{% include 'snippets/toggle/toggle_button.html' with class=button_class text=button_text controls_text="start_reading" controls_uid=uuid focus="modal_title_start_reading" disabled=is_current fallback_url=fallback_url %}
{% join "start_reading" uuid as modal_id %}
{% include 'snippets/shelve_button/modal_button.html' with class=button_class fallback_url=fallback_url %}
{% elif shelf.identifier == 'read' %}
{% trans "Read" as button_text %}
{% url 'reading-status' 'finish' book.id as fallback_url %}
{% include 'snippets/toggle/toggle_button.html' with class=button_class text=button_text controls_text="finish_reading" controls_uid=uuid focus="modal_title_finish_reading" disabled=is_current fallback_url=fallback_url %}
{% join "finish_reading" uuid as modal_id %}
{% include 'snippets/shelve_button/modal_button.html' with class=button_class fallback_url=fallback_url %}
{% elif shelf.identifier == 'to-read' %}
{% trans "Want to read" as button_text %}
{% url 'reading-status' 'want' book.id as fallback_url %}
{% include 'snippets/toggle/toggle_button.html' with class=button_class text=button_text controls_text="want_to_read" controls_uid=uuid focus="modal_title_want_to_read" disabled=is_current fallback_url=fallback_url %}
{% join "want_to_read" uuid as modal_id %}
{% include 'snippets/shelve_button/modal_button.html' with class=button_class fallback_url=fallback_url %}
{% endif %}
</li>
@ -95,7 +95,7 @@
{% include 'snippets/reading_modals/start_reading_modal.html' with book=active_shelf.book id=modal_id move_from=current.id refresh=True class="" %}
{% join "finish_reading" uuid as modal_id %}
{% include 'snippets/reading_modals/finish_reading_modal.html' with book=active_shelf.book id=modal_id move_from=current.id readthrough=readthrough refresh=True class="" %}
{% include 'snippets/reading_modals/finish_reading_modal.html' with book=active_shelf.book id=modal_id move_from=current.id readthrough=readthrough refresh=True class="" %}
{% endwith %}
{% endblock %}

View file

@ -0,0 +1,12 @@
{% load i18n %}
{% if shelf.identifier == 'all' %}
{% trans "All books" %}
{% elif shelf.identifier == 'to-read' %}
{% trans "To Read" %}
{% elif shelf.identifier == 'reading' %}
{% trans "Currently Reading" %}
{% elif shelf.identifier == 'read' %}
{% trans "Read" %}
{% else %}
{{ shelf.name }}
{% endif %}

View file

@ -1,16 +1,10 @@
{% load i18n %}
{% if is_self %}
{% if shelf.identifier == 'to-read' %}
{% trans "To Read" %}
{% elif shelf.identifier == 'reading' %}
{% trans "Currently Reading" %}
{% elif shelf.identifier == 'read' %}
{% trans "Read" %}
{% elif shelf.identifier == 'all' %}
{% trans "Your books" %}
{% if shelf.identifier == 'all' %}
{% trans "Your books" %}
{% else %}
{% include "snippets/translated_shelf_name.html" with shelf=shelf %}
{% endif %}
{% else %}
{{ shelf.name }}
{% endif %}
{% else %}
{% blocktrans with username=user.display_name %}{{ username }}'s books{% endblocktrans %}
{% blocktrans with username=user.display_name %}{{ username }}'s books{% endblocktrans %}
{% endif %}

View file

@ -3,6 +3,7 @@ from django import template
from django.db.models import Avg, StdDev, Count, F, Q
from bookwyrm import models
from bookwyrm.utils import cache
from bookwyrm.views.feed import get_suggested_books
@ -130,35 +131,55 @@ def related_status(notification):
@register.simple_tag(takes_context=True)
def active_shelf(context, book):
"""check what shelf a user has a book on, if any"""
if hasattr(book, "current_shelves"):
read_shelves = [
s
for s in book.current_shelves
if s.shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS
]
return read_shelves[0] if len(read_shelves) else {"book": book}
shelf = (
models.ShelfBook.objects.filter(
shelf__user=context["request"].user,
book__parent_work__editions=book,
user = context["request"].user
return (
cache.get_or_set(
f"active_shelf-{user.id}-{book.id}",
lambda u, b: (
models.ShelfBook.objects.filter(
shelf__user=u,
book__parent_work__editions=b,
).first()
),
user,
book,
timeout=15552000,
)
.select_related("book", "shelf")
.first()
or {"book": book}
)
return shelf if shelf else {"book": book}
@register.simple_tag(takes_context=False)
def latest_read_through(book, user):
"""the most recent read activity"""
if hasattr(book, "active_readthroughs"):
return book.active_readthroughs[0] if len(book.active_readthroughs) else None
return cache.get_or_set(
f"latest_read_through-{user.id}-{book.id}",
lambda u, b: (
models.ReadThrough.objects.filter(user=u, book=b, is_active=True)
.order_by("-start_date")
.first()
),
user,
book,
timeout=15552000,
)
return (
models.ReadThrough.objects.filter(user=user, book=book, is_active=True)
.order_by("-start_date")
.first()
@register.simple_tag(takes_context=False)
def get_landing_books():
"""list of books for the landing page"""
return list(
set(
models.Edition.objects.filter(
review__published_date__isnull=False,
review__deleted=False,
review__user__local=True,
review__privacy__in=["public", "unlisted"],
)
.exclude(cover__exact="")
.distinct()
.order_by("-review__published_date")[:6]
)
)

View file

@ -1,8 +1,8 @@
""" template filters for status interaction buttons """
from django import template
from django.core.cache import cache
from bookwyrm import models
from bookwyrm.utils.cache import get_or_set
register = template.Library()
@ -11,20 +11,23 @@ register = template.Library()
@register.filter(name="liked")
def get_user_liked(user, status):
"""did the given user fav a status?"""
return cache.get_or_set(
return get_or_set(
f"fav-{user.id}-{status.id}",
models.Favorite.objects.filter(user=user, status=status).exists(),
259200,
lambda u, s: models.Favorite.objects.filter(user=u, status=s).exists(),
user,
status,
timeout=259200,
)
@register.filter(name="boosted")
def get_user_boosted(user, status):
"""did the given user fav a status?"""
return cache.get_or_set(
return get_or_set(
f"boost-{user.id}-{status.id}",
status.boosters.filter(user=user).exists(),
259200,
lambda u: status.boosters.filter(user=u).exists(),
user,
timeout=259200,
)
@ -32,3 +35,32 @@ def get_user_boosted(user, status):
def get_user_saved_lists(user, book_list):
"""did the user save a list"""
return user.saved_lists.filter(id=book_list.id).exists()
@register.simple_tag(takes_context=True)
def get_relationship(context, user_object):
"""caches the relationship between the logged in user and another user"""
user = context["request"].user
return get_or_set(
f"relationship-{user.id}-{user_object.id}",
get_relationship_name,
user,
user_object,
timeout=259200,
)
def get_relationship_name(user, user_object):
"""returns the relationship type"""
types = {
"is_following": False,
"is_follow_pending": False,
"is_blocked": False,
}
if user_object in user.blocks.all():
types["is_blocked"] = True
elif user_object in user.following.all():
types["is_following"] = True
elif user_object in user.follower_requests.all():
types["is_follow_pending"] = True
return types

View file

@ -231,11 +231,12 @@ class ReadingViews(TestCase):
"finish_date": "2018-03-07",
"book": self.book.id,
"id": "",
"user": self.local_user.id,
},
)
request.user = self.local_user
views.create_readthrough(request)
views.ReadThrough.as_view()(request)
readthrough = models.ReadThrough.objects.get()
self.assertEqual(readthrough.start_date.year, 2017)
self.assertEqual(readthrough.start_date.month, 1)

View file

@ -41,10 +41,8 @@ class ReadThrough(TestCase):
self.assertEqual(self.edition.readthrough_set.count(), 0)
self.client.post(
"/reading-status/start/{}".format(self.edition.id),
{
"start_date": "2020-11-27",
},
f"/reading-status/start/{self.edition.id}",
{"start_date": "2020-11-27"},
)
readthroughs = self.edition.readthrough_set.all()
@ -62,10 +60,8 @@ class ReadThrough(TestCase):
self.assertEqual(self.edition.readthrough_set.count(), 0)
self.client.post(
"/reading-status/start/{}".format(self.edition.id),
{
"start_date": "2020-11-27",
},
f"/reading-status/start/{self.edition.id}",
{"start_date": "2020-11-27"},
)
readthroughs = self.edition.readthrough_set.all()

View file

@ -500,7 +500,11 @@ urlpatterns = [
# reading progress
re_path(r"^edit-readthrough/?$", views.edit_readthrough, name="edit-readthrough"),
re_path(r"^delete-readthrough/?$", views.delete_readthrough),
re_path(r"^create-readthrough/?$", views.create_readthrough),
re_path(
r"^create-readthrough/?$",
views.ReadThrough.as_view(),
name="create-readthrough",
),
re_path(r"^delete-progressupdate/?$", views.delete_progressupdate),
# shelve actions
re_path(

11
bookwyrm/utils/cache.py Normal file
View file

@ -0,0 +1,11 @@
""" Custom handler for caching """
from django.core.cache import cache
def get_or_set(cache_key, function, *args, timeout=None):
"""Django's built-in get_or_set isn't cutting it"""
value = cache.get(cache_key)
if value is None:
value = function(*args)
cache.set(cache_key, value, timeout=timeout)
return value

View file

@ -94,7 +94,7 @@ from .list import Lists, SavedLists, List, Curate, UserLists
from .list import save_list, unsave_list, delete_list, unsafe_embed_list
from .notifications import Notifications
from .outbox import Outbox
from .reading import create_readthrough, delete_readthrough, delete_progressupdate
from .reading import ReadThrough, delete_readthrough, delete_progressupdate
from .reading import ReadingStatus
from .report import Report
from .rss_feed import RssFeed

View file

@ -16,7 +16,6 @@ from django.views.decorators.http import require_POST
from bookwyrm import emailing, forms, models
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views import helpers
# pylint: disable= no-self-use
@ -174,7 +173,6 @@ class InviteRequest(View):
data = {
"request_form": form,
"request_received": received,
"books": helpers.get_landing_books(),
}
return TemplateResponse(request, "landing/landing.html", data)

View file

@ -153,24 +153,6 @@ def is_blocked(viewer, user):
return False
def get_landing_books():
"""list of books for the landing page"""
return list(
set(
models.Edition.objects.filter(
review__published_date__isnull=False,
review__deleted=False,
review__user__local=True,
review__privacy__in=["public", "unlisted"],
)
.exclude(cover__exact="")
.distinct()
.order_by("-review__published_date")[:6]
)
)
def load_date_in_user_tz_as_utc(date_str: str, user: models.User) -> datetime:
"""ensures that data is stored consistently in the UTC timezone"""
if not date_str:

View file

@ -3,7 +3,6 @@ from django.template.response import TemplateResponse
from django.views import View
from bookwyrm import forms
from bookwyrm.views import helpers
from bookwyrm.views.feed import Feed
@ -28,6 +27,5 @@ class Landing(View):
data = {
"register_form": forms.RegisterForm(),
"request_form": forms.InviteRequestForm(),
"books": helpers.get_landing_books(),
}
return TemplateResponse(request, "landing/landing.html", data)

View file

@ -1,7 +1,6 @@
""" the good stuff! the books! """
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import transaction
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect
@ -10,16 +9,16 @@ from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.http import require_POST
from bookwyrm import models
from bookwyrm import forms, models
from bookwyrm.views.shelf.shelf_actions import unshelve
from .status import CreateStatus
from .helpers import get_edition, handle_reading_status, is_api_request
from .helpers import load_date_in_user_tz_as_utc
@method_decorator(login_required, name="dispatch")
# pylint: disable=no-self-use
# pylint: disable=too-many-return-statements
@method_decorator(login_required, name="dispatch")
class ReadingStatus(View):
"""consider reading a book"""
@ -46,12 +45,10 @@ class ReadingStatus(View):
if not identifier:
return HttpResponseBadRequest()
# invalidate the template cache
cache_keys = [
make_template_fragment_key("shelve_button", [request.user.id, book_id]),
make_template_fragment_key("suggested_books", [request.user.id]),
]
cache.delete_many(cache_keys)
# invalidate related caches
cache.delete(
f"active_shelf-{request.user.id}-{book_id}",
)
desired_shelf = get_object_or_404(
models.Shelf, identifier=identifier, user=request.user
@ -118,6 +115,45 @@ class ReadingStatus(View):
return redirect(referer)
@method_decorator(login_required, name="dispatch")
class ReadThrough(View):
"""Add new read dates"""
def get(self, request, book_id, readthrough_id=None):
"""standalone form in case of errors"""
book = get_object_or_404(models.Edition, id=book_id)
form = forms.ReadThroughForm()
data = {"form": form, "book": book}
if readthrough_id:
data["readthrough"] = get_object_or_404(
models.ReadThrough, id=readthrough_id
)
return TemplateResponse(request, "readthrough/readthrough.html", data)
def post(self, request):
"""can't use the form normally because the dates are too finnicky"""
book_id = request.POST.get("book")
normalized_post = request.POST.copy()
normalized_post["start_date"] = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
normalized_post["finish_date"] = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
form = forms.ReadThroughForm(request.POST)
if not form.is_valid():
book = get_object_or_404(models.Edition, id=book_id)
data = {"form": form, "book": book}
if request.POST.get("id"):
data["readthrough"] = get_object_or_404(
models.ReadThrough, id=request.POST.get("id")
)
return TemplateResponse(request, "readthrough/readthrough.html", data)
form.save()
return redirect("book", book_id)
@transaction.atomic
def update_readthrough_on_shelve(
user, annotated_book, status, start_date=None, finish_date=None
@ -159,27 +195,6 @@ def delete_readthrough(request):
return redirect(request.headers.get("Referer", "/"))
@login_required
@require_POST
def create_readthrough(request):
"""can't use the form because the dates are too finnicky"""
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
start_date = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
finish_date = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
models.ReadThrough.objects.create(
user=request.user,
book=book,
start_date=start_date,
finish_date=finish_date,
)
return redirect("book", book.id)
@login_required
@require_POST
def delete_progressupdate(request):

View file

@ -159,6 +159,7 @@ def update_progress(request, book_id): # pylint: disable=unused-argument
@require_POST
def edit_readthrough(request):
"""can't use the form because the dates are too finnicky"""
# TODO: remove this, it duplicates the code in the ReadThrough view
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
readthrough.raise_not_editable(request.user)

View file

@ -6,13 +6,18 @@ markers =
integration: marks tests as requiring external resources (deselect with '-m "not integration"')
env =
SECRET_KEY = beepbeep
DEBUG = false
USE_HTTPS=true
USE_HTTPS = true
DOMAIN = your.domain.here
BOOKWYRM_DATABASE_BACKEND = postgres
MEDIA_ROOT = images/
CELERY_BROKER = ""
REDIS_BROKER_PORT = 6379
REDIS_BROKER_PASSWORD = beep
REDIS_ACTIVITY_PORT = 6379
REDIS_ACTIVITY_PASSWORD = beep
USE_DUMMY_CACHE = true
FLOWER_PORT = 8888
EMAIL_HOST = "smtp.mailgun.org"
EMAIL_PORT = 587
@ -20,4 +25,3 @@ env =
EMAIL_HOST_PASSWORD = ""
EMAIL_USE_TLS = true
ENABLE_PREVIEW_IMAGES = false
USE_S3 = false