Change notification backend
This commit is contained in:
parent
5f97491f0d
commit
7f854c40dd
15 changed files with 188 additions and 134 deletions
|
@ -6,7 +6,7 @@ from judge.models.choices import (
|
|||
MATH_ENGINES_CHOICES,
|
||||
TIMEZONE,
|
||||
)
|
||||
from judge.models.comment import Comment, CommentLock, CommentVote, Notification
|
||||
from judge.models.comment import Comment, CommentLock, CommentVote
|
||||
from judge.models.contest import (
|
||||
Contest,
|
||||
ContestMoss,
|
||||
|
@ -58,6 +58,7 @@ from judge.models.volunteer import VolunteerProblemVote
|
|||
from judge.models.pagevote import PageVote, PageVoteVoter
|
||||
from judge.models.bookmark import BookMark, MakeBookMark
|
||||
from judge.models.course import Course
|
||||
from judge.models.notification import Notification, NotificationProfile
|
||||
|
||||
revisions.register(Profile, exclude=["points", "last_access", "ip", "rating"])
|
||||
revisions.register(Problem, follow=["language_limits"])
|
||||
|
|
|
@ -177,29 +177,3 @@ class CommentLock(models.Model):
|
|||
|
||||
def __str__(self):
|
||||
return str(self.page)
|
||||
|
||||
|
||||
class Notification(models.Model):
|
||||
owner = models.ForeignKey(
|
||||
Profile,
|
||||
verbose_name=_("owner"),
|
||||
related_name="notifications",
|
||||
on_delete=CASCADE,
|
||||
)
|
||||
time = models.DateTimeField(verbose_name=_("posted time"), auto_now_add=True)
|
||||
comment = models.ForeignKey(
|
||||
Comment, null=True, verbose_name=_("comment"), on_delete=CASCADE
|
||||
)
|
||||
read = models.BooleanField(verbose_name=_("read"), default=False)
|
||||
category = models.CharField(verbose_name=_("category"), max_length=1000)
|
||||
html_link = models.TextField(
|
||||
default="",
|
||||
verbose_name=_("html link to comments, used for non-comments"),
|
||||
max_length=1000,
|
||||
)
|
||||
author = models.ForeignKey(
|
||||
Profile,
|
||||
null=True,
|
||||
verbose_name=_("who trigger, used for non-comment"),
|
||||
on_delete=CASCADE,
|
||||
)
|
||||
|
|
61
judge/models/notification.py
Normal file
61
judge/models/notification.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.db.models import CASCADE, F
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from judge.models import Profile, Comment
|
||||
from judge.caching import cache_wrapper
|
||||
|
||||
|
||||
class Notification(models.Model):
|
||||
owner = models.ForeignKey(
|
||||
Profile,
|
||||
verbose_name=_("owner"),
|
||||
related_name="notifications",
|
||||
on_delete=CASCADE,
|
||||
)
|
||||
time = models.DateTimeField(verbose_name=_("posted time"), auto_now_add=True)
|
||||
category = models.CharField(verbose_name=_("category"), max_length=1000)
|
||||
html_link = models.TextField(
|
||||
default="",
|
||||
verbose_name=_("html link to comments, used for non-comments"),
|
||||
max_length=1000,
|
||||
)
|
||||
author = models.ForeignKey(
|
||||
Profile,
|
||||
null=True,
|
||||
verbose_name=_("who trigger, used for non-comment"),
|
||||
on_delete=CASCADE,
|
||||
)
|
||||
comment = models.ForeignKey(
|
||||
Comment, null=True, verbose_name=_("comment"), on_delete=CASCADE
|
||||
) # deprecated
|
||||
read = models.BooleanField(verbose_name=_("read"), default=False) # deprecated
|
||||
|
||||
|
||||
class NotificationProfile(models.Model):
|
||||
unread_count = models.IntegerField(default=0)
|
||||
user = models.OneToOneField(Profile, on_delete=CASCADE)
|
||||
|
||||
|
||||
def make_notification(to_users, category, html_link, author):
|
||||
for user in to_users:
|
||||
if user == author:
|
||||
continue
|
||||
notif = Notification(
|
||||
owner=user, category=category, html_link=html_link, author=author
|
||||
)
|
||||
notif.save()
|
||||
NotificationProfile.objects.get_or_create(user=user)
|
||||
NotificationProfile.objects.filter(user=user).update(
|
||||
unread_count=F("unread_count") + 1
|
||||
)
|
||||
unseen_notifications_count.dirty(user)
|
||||
|
||||
|
||||
@cache_wrapper(prefix="unc")
|
||||
def unseen_notifications_count(profile):
|
||||
try:
|
||||
return NotificationProfile.objects.get(user=profile).unread_count
|
||||
except ObjectDoesNotExist:
|
||||
return 0
|
|
@ -5,6 +5,7 @@ from django.contrib.contenttypes.fields import GenericForeignKey
|
|||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from judge.models.profile import Profile
|
||||
from judge.caching import cache_wrapper
|
||||
|
||||
__all__ = ["PageVote", "PageVoteVoter"]
|
||||
|
||||
|
@ -28,6 +29,7 @@ class PageVote(models.Model):
|
|||
]
|
||||
unique_together = ("content_type", "object_id")
|
||||
|
||||
@cache_wrapper(prefix="PVvs")
|
||||
def vote_score(self, user):
|
||||
page_vote = PageVoteVoter.objects.filter(pagevote=self, voter=user)
|
||||
if page_vote.exists():
|
||||
|
|
|
@ -16,6 +16,7 @@ from sortedm2m.fields import SortedManyToManyField
|
|||
from judge.models.choices import ACE_THEMES, MATH_ENGINES_CHOICES, TIMEZONE
|
||||
from judge.models.runtime import Language
|
||||
from judge.ratings import rating_class
|
||||
from judge.caching import cache_wrapper
|
||||
|
||||
|
||||
__all__ = ["Organization", "Profile", "OrganizationRequest", "Friend"]
|
||||
|
@ -142,6 +143,7 @@ class Organization(models.Model):
|
|||
)
|
||||
verbose_name = _("organization")
|
||||
verbose_name_plural = _("organizations")
|
||||
app_label = "judge"
|
||||
|
||||
|
||||
class Profile(models.Model):
|
||||
|
@ -266,10 +268,9 @@ class Profile(models.Model):
|
|||
|
||||
@cached_property
|
||||
def count_unseen_notifications(self):
|
||||
query = {
|
||||
"read": False,
|
||||
}
|
||||
return self.notifications.filter(**query).count()
|
||||
from judge.models.notification import unseen_notifications_count
|
||||
|
||||
return unseen_notifications_count(self)
|
||||
|
||||
@cached_property
|
||||
def count_unread_chat_boxes(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue