Refactor notifications

This commit is contained in:
Skia
2016-12-10 00:06:17 +01:00
parent b53531c391
commit d92a706920
13 changed files with 203 additions and 114 deletions

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0012_notification'),
]
operations = [
migrations.RemoveField(
model_name='notification',
name='text',
),
migrations.AddField(
model_name='notification',
name='param',
field=models.CharField(verbose_name='param', default='', max_length=128),
),
migrations.AddField(
model_name='notification',
name='viewed',
field=models.BooleanField(verbose_name='viewed', default=False),
),
migrations.AlterField(
model_name='notification',
name='type',
field=models.CharField(verbose_name='type', default='GENERIC', choices=[('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 %(amount)s'), ('SELLING', 'You just bought %(selling)s'), ('GENERIC', 'You have a notification')], max_length=32),
),
]

View File

@ -853,8 +853,13 @@ class PageRev(models.Model):
class Notification(models.Model):
user = models.ForeignKey(User, related_name='notifications')
url = models.CharField(_("url"), max_length=255)
text = models.CharField(_("text"), max_length=512)
type = models.CharField(_("text"), max_length=16, choices=settings.SITH_NOTIFICATIONS, blank=True, null=True)
param = models.CharField(_("param"), max_length=128, default="")
type = models.CharField(_("type"), max_length=32, choices=settings.SITH_NOTIFICATIONS, default="GENERIC")
date = models.DateTimeField(_('date'), default=timezone.now)
viewed = models.BooleanField(_('viewed'), default=False)
def __str__(self):
if self.param:
return self.get_type_display() % self.param
return self.get_type_display()

View File

@ -67,6 +67,7 @@ header form {
display: none;
position: fixed;
background: lightgrey;
text-align: center;
}
#notif li:hover {
background: #bcc;
@ -289,6 +290,11 @@ tbody>tr:hover {
margin: 2px auto;
display: block;
}
#notifications li {
padding: 5px;
margin: 2px;
list-style: none;
}
#moderation div {
margin: 2px;
padding: 2px;

View File

@ -39,17 +39,16 @@
{% endfor %}
</ul>
<a href="{{ url('core:user_profile', user_id=user.id) }}">{{ user.get_display_name() }}</a>
{% if user.notifications.exists() %}
<a href="#" onclick="display_notif()">&#x1f514; ({{ user.notifications.count() }})</a>
<a href="#" onclick="display_notif()">&#x1f514; ({{ user.notifications.filter(viewed=False).count() }})</a>
<ul id="notif">
{% for n in user.notifications.order_by('-id') %}
{% for n in user.notifications.filter(viewed=False).order_by('-id') %}
<li><a href="{{ url("core:notification", notif_id=n.id) }}">
<span style="font-size: small; ">{{ n.date|date(DATE_FORMAT) }} {{
n.date|time(DATETIME_FORMAT) }}</span><br>
{{ n.text }}</a></li>
{{ n }}</a></li>
{% endfor %}
<li><a href="{{ url('core:notification_list') }}">{% trans %}View more{% endtrans %}</a></li>
</ul>
{% endif %}
<a href="{{ url('core:user_tools') }}">{% trans %}Tools{% endtrans %}</a>
<a href="{{ url('core:logout') }}">{% trans %}Logout{% endtrans %}</a>
<form action="{{ url('core:search') }}" method="GET">

View File

@ -0,0 +1,24 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Notification list{% endtrans %}
{% endblock %}
{% block content %}
<h3>{% trans %}Notification list{% endtrans %}</h3>
<ul id="notifications">
{% for n in notification_list %}
{% if n.viewed %}
<li>
{% else %}
<li style="background: lightgrey;">
{% endif %}
<a href="{{ url("core:notification", notif_id=n.id) }}">
<span style="font-size: small; ">{{ n.date|date(DATE_FORMAT) }} {{
n.date|time(DATETIME_FORMAT) }}</span><br>
{{ n }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -4,6 +4,7 @@ from core.views import *
urlpatterns = [
url(r'^$', index, name='index'),
url(r'^notifications$', NotificationList.as_view(), name='notification_list'),
url(r'^notification/(?P<notif_id>[0-9]+)$', notification, name='notification'),
# Search

View File

@ -71,8 +71,7 @@ class AddFilesForm(forms.Form):
if notif:
for u in RealGroup.objects.filter(id=settings.SITH_SAS_ADMIN_GROUP_ID).first().users.all():
if not u.notifications.filter(type="FILE_MODERATION").exists():
Notification(user=u, text=_("New files to be moderated"),
url=reverse("core:file_moderation"), type="FILE_MODERATION").save()
Notification(user=u, url=reverse("core:file_moderation"), type="FILE_MODERATION").save()
class FileListView(ListView):

View File

@ -4,6 +4,7 @@ from django.http import JsonResponse
from django.core import serializers
from django.db.models import Q
from django.contrib.auth.decorators import login_required
from django.views.generic import ListView
import os
import json
@ -15,10 +16,18 @@ from club.models import Club
def index(request, context=None):
return render(request, "core/index.jinja")
class NotificationList(ListView):
model = Notification
template_name = "core/notification_list.jinja"
def get_queryset(self):
return self.request.user.notifications.order_by('-id')[:20]
def notification(request, notif_id):
notif = Notification.objects.filter(id=notif_id).first()
if notif:
notif.delete()
notif.viewed = True
notif.save()
return redirect(notif.url)
return redirect("/")