From b648012af5d4f3ec9a7419cac8b482bc3ca1f5b6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 16 Jan 2021 08:18:54 -0800 Subject: [PATCH 1/9] Adds form and models for reading goal --- bookwyrm/forms.py | 5 +++++ bookwyrm/migrations/0036_annualgoal.py | 31 ++++++++++++++++++++++++++ bookwyrm/models/__init__.py | 2 +- bookwyrm/models/user.py | 30 +++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/migrations/0036_annualgoal.py diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index eafbe4071..d57d90f3a 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -188,3 +188,8 @@ class ShelfForm(CustomForm): class Meta: model = models.Shelf fields = ['user', 'name', 'privacy'] + +class GoalForm(CustomForm): + class Meta: + model = models.AnnualGoal + fields = ['user', 'year', 'goal'] diff --git a/bookwyrm/migrations/0036_annualgoal.py b/bookwyrm/migrations/0036_annualgoal.py new file mode 100644 index 000000000..9e86f6243 --- /dev/null +++ b/bookwyrm/migrations/0036_annualgoal.py @@ -0,0 +1,31 @@ +# Generated by Django 3.0.7 on 2021-01-15 23:01 + +import bookwyrm.models.fields +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0035_edition_edition_rank'), + ] + + operations = [ + migrations.CreateModel( + name='AnnualGoal', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_date', models.DateTimeField(auto_now_add=True)), + ('updated_date', models.DateTimeField(auto_now=True)), + ('remote_id', bookwyrm.models.fields.RemoteIdField(max_length=255, null=True, validators=[bookwyrm.models.fields.validate_remote_id])), + ('goal', bookwyrm.models.fields.IntegerField()), + ('year', models.IntegerField(default=2021)), + ('user', bookwyrm.models.fields.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'unique_together': {('user', 'year')}, + }, + ), + ] diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index 48852cfe4..e71a150ba 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -17,7 +17,7 @@ from .readthrough import ReadThrough from .tag import Tag, UserTag -from .user import User, KeyPair +from .user import User, KeyPair, AnnualGoal from .relationship import UserFollows, UserFollowRequest, UserBlocks from .federated_server import FederatedServer diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index ef68f9928..5c59496e4 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -6,6 +6,7 @@ from django.apps import apps from django.contrib.auth.models import AbstractUser from django.db import models from django.dispatch import receiver +from django.utils import timezone from bookwyrm import activitypub from bookwyrm.connectors import get_data @@ -221,6 +222,35 @@ class KeyPair(ActivitypubMixin, BookWyrmModel): return activity_object +class AnnualGoal(BookWyrmModel): + ''' set a goal for how many books you read in a year ''' + user = fields.ForeignKey('User', on_delete=models.PROTECT) + goal = fields.IntegerField() + year = models.IntegerField(default=timezone.now().year) + + class Meta: + ''' unqiueness constraint ''' + unique_together = ('user', 'year') + + def get_remote_id(self): + ''' put the year in the path ''' + return '%s/goal/%d' % (self.user.remote_id, self.year) + + @property + def books(self): + ''' the books you've read this year ''' + return self.user.readthrough_set.filter( + finish_date__year__gte=self.year + ).order_by('finish_date').all() + + @property + def book_count(self): + ''' how many books you've read this year ''' + return self.user.readthrough_set.filter( + finish_date__year__gte=self.year).count() + + + @receiver(models.signals.post_save, sender=User) #pylint: disable=unused-argument def execute_after_save(sender, instance, created, *args, **kwargs): From 3a7271309e9ff23bae05aa7bfa421929ed3b4cab Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 16 Jan 2021 08:19:54 -0800 Subject: [PATCH 2/9] Adds markup for set goal status on feed page --- bookwyrm/templates/feed.html | 39 ++++++++++++++++++- .../snippets/finish_reading_modal.html | 4 +- bookwyrm/templates/snippets/goal_form.html | 32 +++++++++++++++ .../snippets/start_reading_modal.html | 4 +- bookwyrm/views/landing.py | 7 ++++ 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 bookwyrm/templates/snippets/goal_form.html diff --git a/bookwyrm/templates/feed.html b/bookwyrm/templates/feed.html index b77da819b..750576c1a 100644 --- a/bookwyrm/templates/feed.html +++ b/bookwyrm/templates/feed.html @@ -47,8 +47,8 @@

- {% include 'snippets/book_titleby.html' with book=book %} - + {% include 'snippets/book_titleby.html' with book=book %} +

{% include 'snippets/toggle/toggle_button.html' with label="close" controls_text="no-book" class="delete" %}
@@ -67,6 +67,14 @@
{% endif %} + + {% if goal %} +
+
+ {{ goal }} hi +
+
+ {% endif %}
@@ -85,6 +93,33 @@
+ {# announcements and system messages #} + {% if not goal and tab == 'home' %} +
+ {% now 'Y' as year %} +
+
+

+ {{ year }} reading goal +

+
+
+

Set a goal for how many books you'll finish reading in {{ year }}, and track your progress throughout the year.

+ + {% include 'snippets/goal_form.html' %} +
+
+ +
+
+
+ {% endif %} +
+ + {# activity feed #} {% if not activities %}

There aren't any activities right now! Try following a user to get started

{% endif %} diff --git a/bookwyrm/templates/snippets/finish_reading_modal.html b/bookwyrm/templates/snippets/finish_reading_modal.html index 06874c069..79bcd9449 100644 --- a/bookwyrm/templates/snippets/finish_reading_modal.html +++ b/bookwyrm/templates/snippets/finish_reading_modal.html @@ -29,8 +29,8 @@