2021-01-16 19:34:19 +00:00
|
|
|
''' non-interactive pages '''
|
|
|
|
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
|
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
2021-01-16 20:48:04 +00:00
|
|
|
from bookwyrm.status import create_generated_note
|
2021-01-16 20:39:51 +00:00
|
|
|
from .helpers import get_user_from_username, object_visible_to_user
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
|
|
class Goal(View):
|
|
|
|
''' track books for the year '''
|
|
|
|
def get(self, request, username, year):
|
|
|
|
''' reading goal page '''
|
|
|
|
user = get_user_from_username(username)
|
|
|
|
year = int(year)
|
|
|
|
goal = models.AnnualGoal.objects.filter(
|
|
|
|
year=year, user=user
|
|
|
|
).first()
|
|
|
|
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-01-16 20:39:51 +00:00
|
|
|
if goal and not object_visible_to_user(request.user, goal):
|
|
|
|
return HttpResponseNotFound()
|
2021-01-16 19:34:19 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
'title': '%s\'s %d Reading' % (user.display_name, year),
|
|
|
|
'goal_form': forms.GoalForm(instance=goal),
|
|
|
|
'goal': goal,
|
|
|
|
'user': user,
|
|
|
|
'year': year,
|
2021-02-01 19:34:08 +00:00
|
|
|
'is_self': request.user == user,
|
2021-01-16 19:34:19 +00:00
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'goal.html', data)
|
|
|
|
|
|
|
|
|
|
|
|
def post(self, request, username, year):
|
|
|
|
''' update or create an annual goal '''
|
|
|
|
user = get_user_from_username(username)
|
|
|
|
if user != request.user:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
year = int(year)
|
|
|
|
goal = models.AnnualGoal.objects.filter(
|
|
|
|
year=year, user=request.user
|
|
|
|
).first()
|
|
|
|
form = forms.GoalForm(request.POST, instance=goal)
|
|
|
|
if not form.is_valid():
|
|
|
|
data = {
|
2021-01-19 15:40:37 +00:00
|
|
|
'title': '%s\'s %d Reading' % (request.user.display_name, year),
|
2021-01-16 19:34:19 +00:00
|
|
|
'goal_form': form,
|
|
|
|
'goal': goal,
|
|
|
|
'year': year,
|
|
|
|
}
|
|
|
|
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-01-16 20:48:04 +00:00
|
|
|
if request.POST.get('post-status'):
|
|
|
|
# create status, if appropraite
|
2021-02-12 19:52:05 +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-02-12 19:52:05 +00:00
|
|
|
template.render({'goal': goal, 'user': request.user}).strip(),
|
2021-01-16 20:48:04 +00:00
|
|
|
privacy=goal.privacy
|
|
|
|
)
|
|
|
|
|
2021-01-16 19:34:19 +00:00
|
|
|
return redirect(request.headers.get('Referer', '/'))
|