Commit d92a7069 authored by Skia's avatar Skia

Refactor notifications

parent b53531c3
Pipeline #487 passed with stage
in 2 minutes and 26 seconds
# -*- 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),
),
]
......@@ -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()
......@@ -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;
......
......@@ -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">
......
{% 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 %}
......@@ -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
......
......@@ -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):
......
......@@ -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("/")
......
......@@ -270,11 +270,10 @@ class Refilling(models.Model):
self.customer.amount += self.amount
self.customer.save()
self.is_validated = True
Notification(
user=self.customer.user,
url=reverse('core:user_account_detail',
Notification(user=self.customer.user, url=reverse('core:user_account_detail',
kwargs={'user_id': self.customer.user.id, 'year': self.date.year, 'month': self.date.month}),
text=_("You just refilled of %(amount)s €") % {'amount': self.amount}
param=str(self.amount),
type="REFILLING",
).save()
super(Refilling, self).save(*args, **kwargs)
......@@ -387,9 +386,9 @@ class Selling(models.Model):
user=self.customer.user,
url=reverse('core:user_account_detail',
kwargs={'user_id': self.customer.user.id, 'year': self.date.year, 'month': self.date.month}),
text=_("You just bought %(quantity)d %(product_name)s") % {'quantity': self.quantity, 'product_name': self.label}
param="%d x %s" % (self.quantity, self.label),
type="SELLING",
).save()
super(Selling, self).save(*args, **kwargs)
class Permanency(models.Model):
......
This diff is collapsed.
......@@ -95,7 +95,7 @@
{% if picture.is_moderated %}
<div id="pict">
{% else %}
<div id="pict" style="border: solid #f00 2px;">
<div id="pict" style="border: solid #f00 2px; box-shadow: red 0px 0px 5px">
{% set next = picture.get_next() %}
{% if not next %}
{% set next = url('sas:moderation') %}
......
......@@ -51,8 +51,7 @@ class SASForm(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="SAS_MODERATION").exists():
Notification(user=u, text=_("New pictures/album to be moderated in the SAS"),
url=reverse("sas:moderation"), type="SAS_MODERATION").save()
Notification(user=u, url=reverse("sas:moderation"), type="SAS_MODERATION").save()
class RelationForm(forms.ModelForm):
class Meta:
......@@ -124,8 +123,7 @@ class PictureView(CanViewMixin, DetailView, FormMixin):
PeoplePictureRelation(user=u,
picture=self.form.cleaned_data['picture']).save()
if not u.notifications.filter(type="NEW_PICTURES").exists():
Notification(user=u, text=_("You've been identified on some pictures"),
url=reverse("core:user_pictures", kwargs={'user_id': u.id}), type="NEW_PICTURES").save()
Notification(user=u, url=reverse("core:user_pictures", kwargs={'user_id': u.id}), type="NEW_PICTURES").save()
return super(PictureView, self).form_valid(self.form)
else:
self.form.add_error(None, _("You do not have the permission to do that"))
......
......@@ -453,9 +453,12 @@ SITH_SAS_ROOT_DIR_ID = 4
SITH_SAS_ADMIN_GROUP_ID = 9
SITH_NOTIFICATIONS = [
('FILE_MODERATION', _("File moderation")),
('SAS_MODERATION', _("SAS moderation")),
('NEW_PICTURES', _("New pictures")),
('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")),
]
try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment