core, com: make permanent notifications for news to moderate

This needs to be made for other stuff, like the SAS or the files...

Signed-off-by: Skia <skia@libskia.so>
This commit is contained in:
Skia 2017-09-06 13:16:28 +02:00
parent dd5926b404
commit 0184b9c29b
5 changed files with 66 additions and 5 deletions

View File

@ -24,13 +24,15 @@
from django.shortcuts import render from django.shortcuts import render
from django.db import models, transaction from django.db import models, transaction
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.conf import settings from django.conf import settings
from django.contrib.staticfiles.templatetags.staticfiles import static from django.contrib.staticfiles.templatetags.staticfiles import static
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from core.models import User, Preferences from core.models import User, Preferences, RealGroup, Notification
from club.models import Club from club.models import Club
@ -82,6 +84,23 @@ class News(models.Model):
def __str__(self): def __str__(self):
return "%s: %s" % (self.type, self.title) return "%s: %s" % (self.type, self.title)
def save(self, *args, **kwargs):
super(News, self).save(*args, **kwargs)
for u in RealGroup.objects.filter(id=settings.SITH_GROUP_COM_ADMIN_ID).first().users.all():
Notification(user=u, url=reverse("com:news_admin_list"),
type="NEWS_MODERATION", param="1").save()
def news_notification_callback(notif):
count = News.objects.filter(
Q(dates__start_date__gt=timezone.now(), is_moderated=False) |
Q(type="NOTICE", is_moderated=False)
).distinct().count()
if count:
notif.viewed = False
notif.param = "%s" % count
notif.date = timezone.now()
else:
notif.viewed = True
class NewsDate(models.Model): class NewsDate(models.Model):
""" """

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0023_auto_20170902_1226'),
]
operations = [
migrations.AlterField(
model_name='notification',
name='type',
field=models.CharField(choices=[('MAILING_MODERATION', 'A new mailing list needs to be moderated'), ('NEWS_MODERATION', 'There are %s fresh news to be moderated'), ('FILE_MODERATION', 'New files to be moderated'), ('SAS_MODERATION', 'New pictures/album to be moderated in the SAS'), ('NEW_PICTURES', "You've been identified on some pictures"), ('REFILLING', 'You just refilled of %s'), ('SELLING', 'You just bought %s'), ('GENERIC', 'You have a notification')], verbose_name='type', default='GENERIC', max_length=32),
),
]

View File

@ -21,6 +21,7 @@
# Place - Suite 330, Boston, MA 02111-1307, USA. # Place - Suite 330, Boston, MA 02111-1307, USA.
# #
# #
import importlib
from django.db import models from django.db import models
from django.core.mail import send_mail from django.core.mail import send_mail
@ -1063,3 +1064,14 @@ class Notification(models.Model):
if self.param: if self.param:
return self.get_type_display() % self.param return self.get_type_display() % self.param
return self.get_type_display() return self.get_type_display()
def save(self, *args, **kwargs):
if not self.id and self.type in settings.SITH_PERMANENT_NOTIFICATIONS:
old_notif = self.user.notifications.filter(type=self.type).last()
if old_notif:
mod_name, func_name = settings.SITH_PERMANENT_NOTIFICATIONS[self.type].rsplit('.',1)
mod = importlib.import_module(mod_name)
getattr(mod, func_name)(old_notif)
old_notif.save()
return
super(Notification, self).save(*args, **kwargs)

View File

@ -27,6 +27,7 @@ from django.http import JsonResponse
from django.core import serializers from django.core import serializers
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.views.generic import ListView, TemplateView from django.views.generic import ListView, TemplateView
from django.conf import settings
import json import json
@ -50,13 +51,16 @@ class NotificationList(ListView):
def get_queryset(self): def get_queryset(self):
if 'see_all' in self.request.GET.keys(): if 'see_all' in self.request.GET.keys():
self.request.user.notifications.update(viewed=True) for n in self.request.user.notifications.all():
n.viewed = True
n.save()
return self.request.user.notifications.order_by('-date')[:20] return self.request.user.notifications.order_by('-date')[:20]
def notification(request, notif_id): def notification(request, notif_id):
notif = Notification.objects.filter(id=notif_id).first() notif = Notification.objects.filter(id=notif_id).first()
if notif: if notif:
if notif.type not in settings.SITH_PERMANENT_NOTIFICATIONS:
notif.viewed = True notif.viewed = True
notif.save() notif.save()
return redirect(notif.url) return redirect(notif.url)

View File

@ -551,7 +551,7 @@ SITH_LAUNDERETTE_PRICES = {
SITH_NOTIFICATIONS = [ SITH_NOTIFICATIONS = [
('MAILING_MODERATION', _("A new mailing list needs to be moderated")), ('MAILING_MODERATION', _("A new mailing list needs to be moderated")),
('NEWS_MODERATION', _("A fresh new to be moderated")), ('NEWS_MODERATION', _("There are %s fresh news to be moderated")),
('FILE_MODERATION', _("New files to be moderated")), ('FILE_MODERATION', _("New files to be moderated")),
('SAS_MODERATION', _("New pictures/album to be moderated in the SAS")), ('SAS_MODERATION', _("New pictures/album to be moderated in the SAS")),
('NEW_PICTURES', _("You've been identified on some pictures")), ('NEW_PICTURES', _("You've been identified on some pictures")),
@ -560,6 +560,13 @@ SITH_NOTIFICATIONS = [
('GENERIC', _("You have a notification")), ('GENERIC', _("You have a notification")),
] ]
# The keys are the notification names as found in SITH_NOTIFICATIONS, and the
# values are the callback function to update the notifs.
# The callback must take the notif object as first and single argument.
SITH_PERMANENT_NOTIFICATIONS = {
'NEWS_MODERATION': 'com.models.news_notification_callback',
}
SITH_QUICK_NOTIF = { SITH_QUICK_NOTIF = {
'qn_success': _("Success!"), 'qn_success': _("Success!"),
'qn_fail': _("Fail!"), 'qn_fail': _("Fail!"),