2021-03-08 16:49:10 +00:00
|
|
|
""" non-interactive pages """
|
2021-01-16 19:34:19 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.http import HttpResponseNotFound
|
|
|
|
from django.shortcuts import redirect
|
2021-02-12 19:52:05 +00:00
|
|
|
from django.template.loader import get_template
|
2021-01-16 19:34:19 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
2021-03-18 16:00:25 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
2021-01-16 20:48:04 +00:00
|
|
|
from bookwyrm.status import create_generated_note
|
2021-04-11 16:26:12 +00:00
|
|
|
from .helpers import get_user_from_username
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
2021-03-08 16:49:10 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-01-16 19:34:19 +00:00
|
|
|
class Goal(View):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" track books for the year """
|
|
|
|
|
2021-01-16 19:34:19 +00:00
|
|
|
def get(self, request, username, year):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" reading goal page """
|
2021-02-23 21:12:50 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
2021-01-16 19:34:19 +00:00
|
|
|
year = int(year)
|
2021-03-08 16:49:10 +00:00
|
|
|
goal = models.AnnualGoal.objects.filter(year=year, user=user).first()
|
2021-01-16 19:34:19 +00:00
|
|
|
if not goal and user != request.user:
|
2021-01-16 21:29:28 +00:00
|
|
|
return HttpResponseNotFound()
|
2021-01-16 19:34:19 +00:00
|
|
|
|
2021-04-11 16:26:12 +00:00
|
|
|
if goal and not goal.visible_to_user(request.user):
|
2021-01-16 20:39:51 +00:00
|
|
|
return HttpResponseNotFound()
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"goal_form": forms.GoalForm(instance=goal),
|
|
|
|
"goal": goal,
|
|
|
|
"user": user,
|
|
|
|
"year": year,
|
|
|
|
"is_self": request.user == user,
|
2021-01-16 19:34:19 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
return TemplateResponse(request, "goal.html", data)
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
def post(self, request, username, year):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" update or create an annual goal """
|
2021-02-23 21:12:50 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
2021-01-16 19:34:19 +00:00
|
|
|
if user != request.user:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
year = int(year)
|
2021-03-08 16:49:10 +00:00
|
|
|
goal = models.AnnualGoal.objects.filter(year=year, user=request.user).first()
|
2021-01-16 19:34:19 +00:00
|
|
|
form = forms.GoalForm(request.POST, instance=goal)
|
|
|
|
if not form.is_valid():
|
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"goal_form": form,
|
|
|
|
"goal": goal,
|
|
|
|
"year": year,
|
2021-01-16 19:34:19 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
return TemplateResponse(request, "goal.html", data)
|
2021-01-16 21:29:28 +00:00
|
|
|
goal = form.save()
|
2021-01-16 19:34:19 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
if request.POST.get("post-status"):
|
2021-01-16 20:48:04 +00:00
|
|
|
# create status, if appropraite
|
2021-03-08 16:49:10 +00:00
|
|
|
template = get_template("snippets/generated_status/goal.html")
|
2021-02-04 21:21:55 +00:00
|
|
|
create_generated_note(
|
2021-01-16 20:48:04 +00:00
|
|
|
request.user,
|
2021-03-08 16:49:10 +00:00
|
|
|
template.render({"goal": goal, "user": request.user}).strip(),
|
|
|
|
privacy=goal.privacy,
|
2021-01-16 20:48:04 +00:00
|
|
|
)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
2021-03-18 16:00:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
def hide_goal(request):
|
|
|
|
""" don't keep bugging people to set a goal """
|
|
|
|
request.user.show_goal = False
|
|
|
|
request.user.save(broadcast=False)
|
|
|
|
return redirect(request.headers.get("Referer", "/"))
|