first push
This commit is contained in:
Executable
+1
@@ -0,0 +1 @@
|
||||
default_app_config = 'notifications.apps.NotificationsConfig'
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class NotificationsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'notifications'
|
||||
|
||||
def ready(self):
|
||||
import notifications.signals
|
||||
@@ -0,0 +1,49 @@
|
||||
# Generated by Django 3.2 on 2022-11-02 06:34
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('roles', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Notification',
|
||||
fields=[
|
||||
('status', models.IntegerField(default=1)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('created_by', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('updated_by', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('instance_id', models.UUIDField(default=uuid.uuid4, editable=False)),
|
||||
('notification_title', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('notification_text', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('icon', models.CharField(blank=True, max_length=225, null=True)),
|
||||
('is_view', models.BooleanField(default=False)),
|
||||
('is_superuser', models.BooleanField(default=False)),
|
||||
('role', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='roles.role')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='UserNotification',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('is_seen', models.BooleanField(default=False)),
|
||||
('notification', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='notifications.notification')),
|
||||
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
# Generated by Django 3.2 on 2022-11-02 06:42
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('notifications', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='usernotification',
|
||||
name='notification',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='usernotification',
|
||||
name='user',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='Notification',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='UserNotification',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,50 @@
|
||||
# Generated by Django 3.2 on 2022-11-02 06:42
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('roles', '0001_initial'),
|
||||
('notifications', '0002_auto_20221102_1212'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Notification',
|
||||
fields=[
|
||||
('status', models.IntegerField(default=1)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('created_by', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('updated_by', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('instance_id', models.UUIDField(default=uuid.uuid4, editable=False)),
|
||||
('notification_title', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('notification_text', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('icon', models.CharField(blank=True, max_length=225, null=True)),
|
||||
('is_view', models.BooleanField(default=False)),
|
||||
('is_superuser', models.BooleanField(default=False)),
|
||||
('role', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='roles.role')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='UserNotification',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('is_seen', models.BooleanField(default=False)),
|
||||
('notification', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='notifications.notification')),
|
||||
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,22 @@
|
||||
# Generated by Django 3.2 on 2022-11-02 06:48
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('notifications', '0003_notification_usernotification'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='notification',
|
||||
name='is_view',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='notification',
|
||||
name='link',
|
||||
field=models.CharField(blank=True, max_length=225, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 3.2 on 2022-11-08 09:43
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('notifications', '0004_auto_20221102_1218'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='notification',
|
||||
name='role',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
# Generated by Django 3.2 on 2022-12-27 10:04
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('notifications', '0005_remove_notification_role'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='usernotification',
|
||||
name='user',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.2 on 2023-01-04 09:25
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('notifications', '0006_alter_usernotification_user'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='notification',
|
||||
name='status',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
||||
Executable
Executable
+22
@@ -0,0 +1,22 @@
|
||||
from email.policy import default
|
||||
from django.db import models
|
||||
from roles.models import Role
|
||||
from dashboard.models import TimeStampModel
|
||||
import uuid
|
||||
from authentication.models import User
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class Notification(TimeStampModel):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
instance_id = models.UUIDField(default=uuid.uuid4, editable=False)
|
||||
notification_title = models.CharField(max_length = 255, blank = True, null = True)
|
||||
notification_text = models.CharField(max_length = 255, blank = True, null = True)
|
||||
icon = models.CharField(max_length = 225, blank = True, null = True)
|
||||
link = models.CharField(max_length = 225, blank = True, null = True)
|
||||
is_superuser = models.BooleanField(default=False)
|
||||
|
||||
class UserNotification(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank = True, null = True)
|
||||
notification = models.ForeignKey(Notification, models.DO_NOTHING, blank = True, null = True)
|
||||
is_seen = models.BooleanField(default=False)
|
||||
@@ -0,0 +1,147 @@
|
||||
from django.db.models.signals import post_save, post_init, post_delete
|
||||
from django.dispatch import receiver
|
||||
from django.conf import settings
|
||||
from notifications.models import Notification, UserNotification
|
||||
from authentication.models import User
|
||||
from datetime import datetime, timedelta
|
||||
from django.db.backends.signals import connection_created
|
||||
from assets.models import AssignAsset
|
||||
from django.db import connection
|
||||
|
||||
# User Notification
|
||||
|
||||
|
||||
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
|
||||
def userNotification(sender, instance, created, **kwargs):
|
||||
|
||||
if instance.previous_role != instance.role:
|
||||
notification = Notification.objects.create(
|
||||
instance_id=instance.id,
|
||||
notification_title='New User' if created else 'Updated User',
|
||||
notification_text=f'{instance.full_name} has been added to your team ({instance.role})',
|
||||
icon='bi-person-fill',
|
||||
)
|
||||
|
||||
users = User.objects.filter(
|
||||
role=instance.role, organization=instance.organization).exclude(pk=instance.id)
|
||||
for user in users:
|
||||
UserNotification.objects.create(
|
||||
user=user,
|
||||
notification=notification
|
||||
)
|
||||
|
||||
if instance.previous_role is not None and instance.previous_role != instance.role and not created:
|
||||
notification = Notification.objects.create(
|
||||
instance_id=instance.id,
|
||||
notification_title='Updated User',
|
||||
notification_text=f'{instance.full_name} has been removed from your team ({instance.previous_role})',
|
||||
icon='bi-person-fill',
|
||||
)
|
||||
|
||||
users = User.objects.filter(
|
||||
role=instance.previous_role, organization=instance.organization)
|
||||
for user in users:
|
||||
UserNotification.objects.create(
|
||||
user=user,
|
||||
notification=notification
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_init, sender=settings.AUTH_USER_MODEL)
|
||||
def remember_state_user(sender, instance, **kwargs):
|
||||
|
||||
instance.previous_role = instance.role
|
||||
|
||||
|
||||
# Asset Notification
|
||||
|
||||
@receiver(post_save, sender=AssignAsset)
|
||||
def asset_notification(sender, instance, created, **kwargs):
|
||||
|
||||
if instance.previous_user != instance.user:
|
||||
|
||||
notification = Notification.objects.create(
|
||||
instance_id=instance.id,
|
||||
notification_title='Assigned asset',
|
||||
notification_text=f'{instance.asset.name} is assigned to you.',
|
||||
icon='bi-person-workspace',
|
||||
)
|
||||
|
||||
UserNotification.objects.create(
|
||||
user=instance.user,
|
||||
notification=notification
|
||||
)
|
||||
|
||||
if instance.previous_user != instance.user and not created:
|
||||
notification = Notification.objects.create(
|
||||
instance_id=instance.id,
|
||||
notification_title='Assigned asset',
|
||||
notification_text=f'{instance.asset.name} is unassigned from you.',
|
||||
icon='bi-person-workspace',
|
||||
)
|
||||
|
||||
UserNotification.objects.create(
|
||||
user=instance.previous_user,
|
||||
notification=notification
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_delete, sender=AssignAsset)
|
||||
def asset_delete_notification(sender, instance, *args, **kwargs):
|
||||
notification = Notification.objects.create(
|
||||
instance_id=instance.id,
|
||||
notification_title='Assigned asset',
|
||||
notification_text=f'{instance.asset.name} is unassigned from you.',
|
||||
icon='bi-person-workspace',
|
||||
)
|
||||
|
||||
UserNotification.objects.create(
|
||||
user=instance.previous_user,
|
||||
notification=notification
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_init, sender=AssignAsset)
|
||||
def remember_state_asset(sender, instance, **kwargs):
|
||||
instance.previous_user = instance.user
|
||||
|
||||
|
||||
def expiring_asset(days):
|
||||
# checking if table exists
|
||||
all_tables = connection.introspection.table_names()
|
||||
|
||||
# Asset Expires Notification
|
||||
if 'assets_assignasset' in all_tables:
|
||||
|
||||
time_threshold = datetime.now() + timedelta(days=days)
|
||||
expiring_assets = AssignAsset.objects.filter(
|
||||
asset__warranty_expiry_date=time_threshold)
|
||||
|
||||
|
||||
|
||||
|
||||
for expiring_asset in expiring_assets:
|
||||
|
||||
super_user = User.objects.filter(
|
||||
is_superuser=True, organization=expiring_asset.user.organization).first()
|
||||
|
||||
if not UserNotification.objects.filter(user=super_user, notification__notification_text=f'{expiring_asset.asset.name} warranty expires in {days} days.').exists():
|
||||
|
||||
notification = Notification.objects.create(
|
||||
instance_id=expiring_asset.id,
|
||||
notification_title='Warranty Expires',
|
||||
notification_text=f'{expiring_asset.asset.name} warranty expires in {days} days.',
|
||||
icon='bi-person-workspace',
|
||||
link=f'/assets/details/{expiring_asset.asset.id}'
|
||||
)
|
||||
|
||||
UserNotification.objects.create(
|
||||
user=super_user,
|
||||
notification=notification
|
||||
)
|
||||
|
||||
@receiver(connection_created)
|
||||
def conn_db(sender, connection, **kwargs):
|
||||
expiring_asset(15)
|
||||
expiring_asset(7)
|
||||
expiring_asset(0)
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,14 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
app_name = 'notifications'
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
path('list', views.list, name='list'),
|
||||
path('count', views.count, name='count'),
|
||||
path('data', views.data, name='data'),
|
||||
path('clear', views.clear, name='clear'),
|
||||
path('seen/<int:id>', views.seen, name='seen'),
|
||||
path('search/<str:page>', views.search, name='search'),
|
||||
]
|
||||
Executable
+98
@@ -0,0 +1,98 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from .models import UserNotification
|
||||
from django.core.paginator import Paginator
|
||||
from django.http import HttpResponse
|
||||
import json
|
||||
from django.views.decorators.http import require_POST
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
PAGE_SIZE = 10
|
||||
ORPHANS = 1
|
||||
|
||||
|
||||
@login_required
|
||||
def list(request):
|
||||
notifications_list = UserNotification.objects.filter(
|
||||
user=request.user).order_by('-notification__created_at')
|
||||
paginator = Paginator(notifications_list, PAGE_SIZE, orphans=ORPHANS)
|
||||
page_number = request.GET.get('page')
|
||||
page_object = paginator.get_page(page_number)
|
||||
|
||||
context = {
|
||||
'sidebar': 'notifications',
|
||||
'page_object': page_object,
|
||||
'title': 'Notifications'
|
||||
}
|
||||
UserNotification.objects.filter(user=request.user, is_seen=False).exclude(
|
||||
notification__instance_id=request.user.id).update(is_seen=True)
|
||||
return render(request, 'notifications/details.html', context=context)
|
||||
|
||||
|
||||
def count(request):
|
||||
if not request.user.is_authenticated:
|
||||
context = {'not_authenticated': True}
|
||||
else:
|
||||
context = {'notification_count': UserNotification.objects.filter(user=request.user, is_seen=False).exclude(
|
||||
notification__instance_id=request.user.id).order_by('-notification__created_at').count()}
|
||||
|
||||
return render(request, 'notifications/notification_count.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
def data(request):
|
||||
return render(request, 'notifications/notification_list.html', {
|
||||
'notifications': UserNotification.objects.filter(user=request.user, is_seen=False).exclude(notification__instance_id=request.user.id).order_by('-notification__created_at')[0:5]
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
@ require_POST
|
||||
def clear(request):
|
||||
if request.method == 'POST':
|
||||
UserNotification.objects.filter(user=request.user, is_seen=False).exclude(
|
||||
notification__instance_id=request.user.id).update(is_seen=True)
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={
|
||||
'HX-Trigger': json.dumps({
|
||||
"notificationCountChanged": None,
|
||||
"notificationListChanged": None,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@ require_POST
|
||||
def seen(request, id):
|
||||
if request.method == 'POST':
|
||||
UserNotification.objects.filter(pk=id).update(is_seen=True)
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={
|
||||
'HX-Trigger': json.dumps({
|
||||
"notificationCountChanged": None,
|
||||
"notificationListChanged": None,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def search(request, page):
|
||||
search_text = request.GET.get('search_text').strip()
|
||||
if search_text:
|
||||
return render(request, 'notifications/notifications-data.html', {
|
||||
'page_object': UserNotification.objects.filter(Q(user=request.user) & (Q(
|
||||
notification__notification_title__icontains=search_text) | Q(notification__notification_text__icontains=search_text)
|
||||
)).order_by('-notification__created_at')[:10]
|
||||
})
|
||||
|
||||
notification_list = UserNotification.objects.filter(
|
||||
user=request.user).order_by('-notification__created_at')
|
||||
paginator = Paginator(notification_list, PAGE_SIZE, orphans=ORPHANS)
|
||||
page_number = page
|
||||
page_object = paginator.get_page(page_number)
|
||||
return render(request, 'notifications/notifications-data.html', {'page_object': page_object})
|
||||
Reference in New Issue
Block a user