From 2298a478cfac84af5983bb9b1043ed63f64a619e Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 11 Jul 2022 16:02:18 -0700 Subject: [PATCH] Moves annual goal to its own file The user model file is just,, very long --- .../migrations/0122_alter_annualgoal_year.py | 4 +- bookwyrm/models/__init__.py | 3 +- bookwyrm/models/annual_goal.py | 67 +++++++++++++++++++ bookwyrm/models/user.py | 64 +----------------- 4 files changed, 74 insertions(+), 64 deletions(-) create mode 100644 bookwyrm/models/annual_goal.py diff --git a/bookwyrm/migrations/0122_alter_annualgoal_year.py b/bookwyrm/migrations/0122_alter_annualgoal_year.py index 90af5fccf..60e40a54d 100644 --- a/bookwyrm/migrations/0122_alter_annualgoal_year.py +++ b/bookwyrm/migrations/0122_alter_annualgoal_year.py @@ -14,6 +14,8 @@ class Migration(migrations.Migration): migrations.AlterField( model_name="annualgoal", name="year", - field=models.IntegerField(default=bookwyrm.models.user.get_current_year), + field=models.IntegerField( + default=bookwyrm.models.annual_goal.get_current_year + ), ), ] diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index a8a84f095..ae7000162 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -17,7 +17,8 @@ from .attachment import Image from .favorite import Favorite from .readthrough import ReadThrough, ProgressUpdate, ProgressMode -from .user import User, KeyPair, AnnualGoal +from .user import User, KeyPair +from .annual_goal import AnnualGoal from .relationship import UserFollows, UserFollowRequest, UserBlocks from .report import Report, ReportComment from .federated_server import FederatedServer diff --git a/bookwyrm/models/annual_goal.py b/bookwyrm/models/annual_goal.py new file mode 100644 index 000000000..53c041141 --- /dev/null +++ b/bookwyrm/models/annual_goal.py @@ -0,0 +1,67 @@ +""" How many books do you want to read this year """ +from django.core.validators import MinValueValidator +from django.db import models +from django.utils import timezone + +from bookwyrm.models.status import Review +from .base_model import BookWyrmModel +from . import fields, Review + + +def get_current_year(): + """sets default year for annual goal to this year""" + return timezone.now().year + + +class AnnualGoal(BookWyrmModel): + """set a goal for how many books you read in a year""" + + user = models.ForeignKey("User", on_delete=models.PROTECT) + goal = models.IntegerField(validators=[MinValueValidator(1)]) + year = models.IntegerField(default=get_current_year) + privacy = models.CharField( + max_length=255, default="public", choices=fields.PrivacyLevels + ) + + class Meta: + """unqiueness constraint""" + + unique_together = ("user", "year") + + def get_remote_id(self): + """put the year in the path""" + return f"{self.user.remote_id}/goal/{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, + finish_date__year__lt=self.year + 1, + ) + .order_by("-finish_date") + .all() + ) + + @property + def ratings(self): + """ratings for books read this year""" + book_ids = [r.book.id for r in self.books] + reviews = Review.objects.filter( + user=self.user, + book__in=book_ids, + ) + return {r.book.id: r.rating for r in reviews} + + @property + def progress(self): + """how many books you've read this year""" + count = self.user.readthrough_set.filter( + finish_date__year__gte=self.year, + finish_date__year__lt=self.year + 1, + ).count() + return { + "count": count, + "percent": int(float(count / self.goal) * 100), + } diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index dce74022c..f23eed781 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -5,7 +5,6 @@ from urllib.parse import urlparse from django.apps import apps from django.contrib.auth.models import AbstractUser, Group from django.contrib.postgres.fields import ArrayField, CICharField -from django.core.validators import MinValueValidator from django.dispatch import receiver from django.db import models, transaction from django.utils import timezone @@ -16,7 +15,7 @@ import pytz from bookwyrm import activitypub from bookwyrm.connectors import get_data, ConnectorException from bookwyrm.models.shelf import Shelf -from bookwyrm.models.status import Status, Review +from bookwyrm.models.status import Status from bookwyrm.preview_images import generate_user_preview_image_task from bookwyrm.settings import DOMAIN, ENABLE_PREVIEW_IMAGES, USE_HTTPS, LANGUAGES from bookwyrm.signatures import create_key_pair @@ -25,7 +24,7 @@ from bookwyrm.utils import regex from .activitypub_mixin import OrderedCollectionPageMixin, ActivitypubMixin from .base_model import BookWyrmModel, DeactivationReason, new_access_code from .federated_server import FederatedServer -from . import fields, Review +from . import fields FeedFilterChoices = [ @@ -414,65 +413,6 @@ class KeyPair(ActivitypubMixin, BookWyrmModel): return super().save(*args, **kwargs) -def get_current_year(): - """sets default year for annual goal to this year""" - return timezone.now().year - - -class AnnualGoal(BookWyrmModel): - """set a goal for how many books you read in a year""" - - user = models.ForeignKey("User", on_delete=models.PROTECT) - goal = models.IntegerField(validators=[MinValueValidator(1)]) - year = models.IntegerField(default=get_current_year) - privacy = models.CharField( - max_length=255, default="public", choices=fields.PrivacyLevels - ) - - class Meta: - """unqiueness constraint""" - - unique_together = ("user", "year") - - def get_remote_id(self): - """put the year in the path""" - return f"{self.user.remote_id}/goal/{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, - finish_date__year__lt=self.year + 1, - ) - .order_by("-finish_date") - .all() - ) - - @property - def ratings(self): - """ratings for books read this year""" - book_ids = [r.book.id for r in self.books] - reviews = Review.objects.filter( - user=self.user, - book__in=book_ids, - ) - return {r.book.id: r.rating for r in reviews} - - @property - def progress(self): - """how many books you've read this year""" - count = self.user.readthrough_set.filter( - finish_date__year__gte=self.year, - finish_date__year__lt=self.year + 1, - ).count() - return { - "count": count, - "percent": int(float(count / self.goal) * 100), - } - - @app.task(queue="low_priority") def set_remote_server(user_id): """figure out the user's remote server in the background"""