From 2419942770f5da34898a4d93f7765bf6d1658b78 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 22 Sep 2021 08:35:20 -0700 Subject: [PATCH] Create model function to update user last active date --- bookwyrm/models/favorite.py | 4 +--- bookwyrm/models/readthrough.py | 7 ++----- bookwyrm/models/user.py | 5 +++++ bookwyrm/views/login.py | 4 +--- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/bookwyrm/models/favorite.py b/bookwyrm/models/favorite.py index 9ab300b3c..0f7e2d985 100644 --- a/bookwyrm/models/favorite.py +++ b/bookwyrm/models/favorite.py @@ -1,7 +1,6 @@ """ like/fav/star a status """ from django.apps import apps from django.db import models -from django.utils import timezone from bookwyrm import activitypub from .activitypub_mixin import ActivityMixin @@ -29,8 +28,7 @@ class Favorite(ActivityMixin, BookWyrmModel): def save(self, *args, **kwargs): """update user active time""" - self.user.last_active_date = timezone.now() - self.user.save(broadcast=False, update_fields=["last_active_date"]) + self.user.update_active_date() super().save(*args, **kwargs) if self.status.user.local and self.status.user != self.user: diff --git a/bookwyrm/models/readthrough.py b/bookwyrm/models/readthrough.py index 343d3c115..e1090f415 100644 --- a/bookwyrm/models/readthrough.py +++ b/bookwyrm/models/readthrough.py @@ -2,7 +2,6 @@ from django.core import validators from django.db import models from django.db.models import F, Q -from django.utils import timezone from .base_model import BookWyrmModel @@ -30,8 +29,7 @@ class ReadThrough(BookWyrmModel): def save(self, *args, **kwargs): """update user active time""" - self.user.last_active_date = timezone.now() - self.user.save(broadcast=False, update_fields=["last_active_date"]) + self.user.update_active_date() super().save(*args, **kwargs) def create_update(self): @@ -65,6 +63,5 @@ class ProgressUpdate(BookWyrmModel): def save(self, *args, **kwargs): """update user active time""" - self.user.last_active_date = timezone.now() - self.user.save(broadcast=False, update_fields=["last_active_date"]) + self.user.update_active_date() super().save(*args, **kwargs) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index ad43270c2..637baa6ee 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -195,6 +195,11 @@ class User(OrderedCollectionPageMixin, AbstractUser): queryset = queryset.exclude(blocks=viewer) return queryset + def update_active_date(self): + """this user is here! they are doing things!""" + self.last_active_date = timezone.now() + self.save(broadcast=False, update_fields=["last_active_date"]) + def to_outbox(self, filter_type=None, **kwargs): """an ordered collection of statuses""" if filter_type: diff --git a/bookwyrm/views/login.py b/bookwyrm/views/login.py index 6c40e4cef..e96d421a0 100644 --- a/bookwyrm/views/login.py +++ b/bookwyrm/views/login.py @@ -3,7 +3,6 @@ from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from django.template.response import TemplateResponse -from django.utils import timezone from django.utils.decorators import method_decorator from django.utils.translation import gettext_lazy as _ from django.views import View @@ -54,8 +53,7 @@ class Login(View): if user is not None: # successful login login(request, user) - user.last_active_date = timezone.now() - user.save(broadcast=False, update_fields=["last_active_date"]) + user.update_active_date() if request.POST.get("first_login"): return redirect("get-started-profile") return redirect(request.GET.get("next", "/"))