Create model function to update user last active date

This commit is contained in:
Mouse Reeve 2021-09-22 08:35:20 -07:00
parent 5ad315faac
commit 2419942770
4 changed files with 9 additions and 11 deletions

View file

@ -1,7 +1,6 @@
""" like/fav/star a status """ """ like/fav/star a status """
from django.apps import apps from django.apps import apps
from django.db import models from django.db import models
from django.utils import timezone
from bookwyrm import activitypub from bookwyrm import activitypub
from .activitypub_mixin import ActivityMixin from .activitypub_mixin import ActivityMixin
@ -29,8 +28,7 @@ class Favorite(ActivityMixin, BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""update user active time""" """update user active time"""
self.user.last_active_date = timezone.now() self.user.update_active_date()
self.user.save(broadcast=False, update_fields=["last_active_date"])
super().save(*args, **kwargs) super().save(*args, **kwargs)
if self.status.user.local and self.status.user != self.user: if self.status.user.local and self.status.user != self.user:

View file

@ -2,7 +2,6 @@
from django.core import validators from django.core import validators
from django.db import models from django.db import models
from django.db.models import F, Q from django.db.models import F, Q
from django.utils import timezone
from .base_model import BookWyrmModel from .base_model import BookWyrmModel
@ -30,8 +29,7 @@ class ReadThrough(BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""update user active time""" """update user active time"""
self.user.last_active_date = timezone.now() self.user.update_active_date()
self.user.save(broadcast=False, update_fields=["last_active_date"])
super().save(*args, **kwargs) super().save(*args, **kwargs)
def create_update(self): def create_update(self):
@ -65,6 +63,5 @@ class ProgressUpdate(BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""update user active time""" """update user active time"""
self.user.last_active_date = timezone.now() self.user.update_active_date()
self.user.save(broadcast=False, update_fields=["last_active_date"])
super().save(*args, **kwargs) super().save(*args, **kwargs)

View file

@ -195,6 +195,11 @@ class User(OrderedCollectionPageMixin, AbstractUser):
queryset = queryset.exclude(blocks=viewer) queryset = queryset.exclude(blocks=viewer)
return queryset 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): def to_outbox(self, filter_type=None, **kwargs):
"""an ordered collection of statuses""" """an ordered collection of statuses"""
if filter_type: if filter_type:

View file

@ -3,7 +3,6 @@ from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect from django.shortcuts import redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views import View from django.views import View
@ -54,8 +53,7 @@ class Login(View):
if user is not None: if user is not None:
# successful login # successful login
login(request, user) login(request, user)
user.last_active_date = timezone.now() user.update_active_date()
user.save(broadcast=False, update_fields=["last_active_date"])
if request.POST.get("first_login"): if request.POST.get("first_login"):
return redirect("get-started-profile") return redirect("get-started-profile")
return redirect(request.GET.get("next", "/")) return redirect(request.GET.get("next", "/"))