Post-processes statuses composed in modals

This commit is contained in:
Mouse Reeve 2021-09-30 10:00:05 -07:00
parent 009a1fe4fd
commit 75e69eb269
4 changed files with 65 additions and 69 deletions

View file

@ -49,8 +49,7 @@ from .list import save_list, unsave_list, delete_list
from .login import Login, Logout from .login import Login, Logout
from .notifications import Notifications from .notifications import Notifications
from .outbox import Outbox from .outbox import Outbox
from .reading import edit_readthrough, create_readthrough from .reading import create_readthrough, delete_readthrough, delete_progressupdate
from .reading import delete_readthrough, delete_progressupdate
from .reading import ReadingStatus from .reading import ReadingStatus
from .register import Register, ConfirmEmail, ConfirmEmailCode, resend_link from .register import Register, ConfirmEmail, ConfirmEmailCode, resend_link
from .rss_feed import RssFeed from .rss_feed import RssFeed
@ -60,6 +59,7 @@ from .shelf import Shelf
from .shelf import create_shelf, delete_shelf from .shelf import create_shelf, delete_shelf
from .shelf import shelve, unshelve from .shelf import shelve, unshelve
from .status import CreateStatus, DeleteStatus, DeleteAndRedraft, update_progress from .status import CreateStatus, DeleteStatus, DeleteAndRedraft, update_progress
from .status import edit_readthrough
from .updates import get_notification_count, get_unread_status_count from .updates import get_notification_count, get_unread_status_count
from .user import User, Followers, Following, hide_suggestions from .user import User, Followers, Following, hide_suggestions
from .wellknown import * from .wellknown import *

View file

@ -1,5 +1,10 @@
""" helper functions used in various views """ """ helper functions used in various views """
import re import re
from datetime import datetime
import dateutil.parser
import dateutil.tz
from dateutil.parser import ParserError
from requests import HTTPError from requests import HTTPError
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django.db.models import Q from django.db.models import Q
@ -178,3 +183,15 @@ def get_landing_books():
.order_by("-review__published_date")[:6] .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:
return None
user_tz = dateutil.tz.gettz(user.preferred_timezone)
date = dateutil.parser.parse(date_str, ignoretz=True)
try:
return date.replace(tzinfo=user_tz).astimezone(dateutil.tz.UTC)
except ParserError:
return None

View file

@ -1,9 +1,4 @@
""" the good stuff! the books! """ """ the good stuff! the books! """
from datetime import datetime
import dateutil.parser
import dateutil.tz
from dateutil.parser import ParserError
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.db import transaction from django.db import transaction
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
@ -13,8 +8,10 @@ from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from bookwyrm import forms, models from bookwyrm import models
from .status import CreateStatus
from .helpers import get_edition, handle_reading_status, is_api_request 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") @method_decorator(login_required, name="dispatch")
@ -88,13 +85,7 @@ class ReadingStatus(View):
if request.POST.get("post-status"): if request.POST.get("post-status"):
# is it a comment? # is it a comment?
if request.POST.get("content"): if request.POST.get("content"):
form = forms.CommentForm(request.POST) return CreateStatus.as_view()(request, "comment")
if form.is_valid():
form.save()
else:
# uh oh
raise Exception(form.errors)
else:
privacy = request.POST.get("privacy") privacy = request.POST.get("privacy")
handle_reading_status(request.user, desired_shelf, book, privacy) handle_reading_status(request.user, desired_shelf, book, privacy)
@ -133,45 +124,6 @@ def update_readthrough_on_shelve(
active_readthrough.save() active_readthrough.save()
@login_required
@require_POST
def edit_readthrough(request):
"""can't use the form because the dates are too finnicky"""
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
readthrough.raise_not_editable(request.user)
readthrough.start_date = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
readthrough.finish_date = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
progress = request.POST.get("progress")
try:
progress = int(progress)
readthrough.progress = progress
except (ValueError, TypeError):
pass
progress_mode = request.POST.get("progress_mode")
try:
progress_mode = models.ProgressMode(progress_mode)
readthrough.progress_mode = progress_mode
except ValueError:
pass
readthrough.save()
# record the progress update individually
# use default now for date field
readthrough.create_update()
if is_api_request(request):
return HttpResponse()
return redirect(request.headers.get("Referer", "/"))
@login_required @login_required
@require_POST @require_POST
def delete_readthrough(request): def delete_readthrough(request):
@ -204,18 +156,6 @@ def create_readthrough(request):
return redirect("book", book.id) return redirect("book", book.id)
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:
return None
user_tz = dateutil.tz.gettz(user.preferred_timezone)
date = dateutil.parser.parse(date_str, ignoretz=True)
try:
return date.replace(tzinfo=user_tz).astimezone(dateutil.tz.UTC)
except ParserError:
return None
@login_required @login_required
@require_POST @require_POST
def delete_progressupdate(request): def delete_progressupdate(request):

View file

@ -18,7 +18,7 @@ from bookwyrm.sanitize_html import InputHtmlParser
from bookwyrm.settings import DOMAIN from bookwyrm.settings import DOMAIN
from bookwyrm.utils import regex from bookwyrm.utils import regex
from .helpers import handle_remote_webfinger, is_api_request from .helpers import handle_remote_webfinger, is_api_request
from .reading import edit_readthrough from .helpers import load_date_in_user_tz_as_utc
# pylint: disable= no-self-use # pylint: disable= no-self-use
@ -145,6 +145,45 @@ def update_progress(request, book_id): # pylint: disable=unused-argument
return edit_readthrough(request) return edit_readthrough(request)
@login_required
@require_POST
def edit_readthrough(request):
"""can't use the form because the dates are too finnicky"""
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
readthrough.raise_not_editable(request.user)
readthrough.start_date = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
readthrough.finish_date = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
progress = request.POST.get("progress")
try:
progress = int(progress)
readthrough.progress = progress
except (ValueError, TypeError):
pass
progress_mode = request.POST.get("progress_mode")
try:
progress_mode = models.ProgressMode(progress_mode)
readthrough.progress_mode = progress_mode
except ValueError:
pass
readthrough.save()
# record the progress update individually
# use default now for date field
readthrough.create_update()
if is_api_request(request):
return HttpResponse()
return redirect(request.headers.get("Referer", "/"))
def find_mentions(content): def find_mentions(content):
"""detect @mentions in raw status content""" """detect @mentions in raw status content"""
if not content: if not content: