Merge pull request #1249 from bookwyrm-social/goal-page

Handle goal pages for unset years
This commit is contained in:
Mouse Reeve 2021-08-04 14:16:45 -06:00 committed by GitHub
commit 708a813905
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 10 deletions

View file

@ -17,9 +17,9 @@
{% block panel %}
<section class="block">
{% if user == request.user %}
{% now 'Y' as current_year %}
{% if user == request.user and year == current_year %}
<div class="block">
{% now 'Y' as year %}
<section class="card {% if goal %}is-hidden{% endif %}" id="show-edit-goal">
<header class="card-header">
<h2 class="card-header-title has-background-primary has-text-white" tabindex="0" id="edit-form-header">

View file

@ -39,6 +39,7 @@ class GoalViews(TestCase):
)
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
self.year = timezone.now().year
models.SiteSettings.objects.create()
def test_goal_page_no_goal(self):
@ -47,7 +48,7 @@ class GoalViews(TestCase):
request = self.factory.get("")
request.user = self.rat
result = view(request, self.local_user.localname, 2020)
result = view(request, self.local_user.localname, self.year)
self.assertEqual(result.status_code, 404)
def test_goal_page_no_goal_self(self):
@ -56,7 +57,7 @@ class GoalViews(TestCase):
request = self.factory.get("")
request.user = self.local_user
result = view(request, self.local_user.localname, 2020)
result = view(request, self.local_user.localname, self.year)
result.render()
self.assertIsInstance(result, TemplateResponse)
@ -66,7 +67,7 @@ class GoalViews(TestCase):
request = self.factory.get("")
request.user = self.anonymous_user
result = view(request, self.local_user.localname, 2020)
result = view(request, self.local_user.localname, self.year)
self.assertEqual(result.status_code, 302)
def test_goal_page_public(self):
@ -94,13 +95,13 @@ class GoalViews(TestCase):
def test_goal_page_private(self):
"""view a user's private goal"""
models.AnnualGoal.objects.create(
user=self.local_user, year=2020, goal=15, privacy="followers"
user=self.local_user, year=self.year, goal=15, privacy="followers"
)
view = views.Goal.as_view()
request = self.factory.get("")
request.user = self.rat
result = view(request, self.local_user.localname, 2020)
result = view(request, self.local_user.localname, self.year)
self.assertEqual(result.status_code, 404)
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
@ -112,19 +113,19 @@ class GoalViews(TestCase):
{
"user": self.local_user.id,
"goal": 10,
"year": 2020,
"year": self.year,
"privacy": "unlisted",
"post-status": True,
},
)
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.local_user.localname, 2020)
view(request, self.local_user.localname, self.year)
goal = models.AnnualGoal.objects.get()
self.assertEqual(goal.user, self.local_user)
self.assertEqual(goal.goal, 10)
self.assertEqual(goal.year, 2020)
self.assertEqual(goal.year, self.year)
self.assertEqual(goal.privacy, "unlisted")
status = models.GeneratedNote.objects.get()

View file

@ -4,6 +4,7 @@ from django.http import HttpResponseNotFound
from django.shortcuts import redirect
from django.template.loader import get_template
from django.template.response import TemplateResponse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.http import require_POST
@ -26,6 +27,10 @@ class Goal(View):
if not goal and user != request.user:
return HttpResponseNotFound()
current_year = timezone.now().year
if not goal and year != timezone.now().year:
return redirect("user-goal", username, current_year)
if goal and not goal.visible_to_user(request.user):
return HttpResponseNotFound()