Adds form and models for reading goal

This commit is contained in:
Mouse Reeve
2021-01-16 08:18:54 -08:00
parent 3866523d57
commit b648012af5
4 changed files with 67 additions and 1 deletions

View File

@ -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

View File

@ -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):