From b648012af5d4f3ec9a7419cac8b482bc3ca1f5b6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 16 Jan 2021 08:18:54 -0800 Subject: [PATCH] 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):