Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New notifications #16

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion helpdesk/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

from helpdesk import settings
from helpdesk.lib import safe_template_context, process_attachments
from helpdesk.models import Ticket, TicketCC, FollowUp, IgnoreEmail, FormType, CustomField, is_extra_data
from helpdesk.models import Ticket, TicketCC, FollowUp, IgnoreEmail, FormType, CustomField, is_extra_data, Notification
from seed.models import EmailImporter, GOOGLE, MICROSOFT, EXCHANGE_OAUTH, EXCHANGE_PASS
from helpdesk.decorators import is_helpdesk_staff
from post_office.models import Email
Expand Down Expand Up @@ -854,6 +854,30 @@ def create_ticket_from_processed_message(message, ticket_id, payload, files, log
email_logger=logger,
source="import"
)
for ticketcc in ticket.ticketcc_set.all():
if is_helpdesk_staff(ticketcc.user, ticket.ticket_form.organization_id) and ticketcc.user.usersettings_helpdesk.enable_notifications:
new_notification = Notification(
user=ticketcc.user,
ticket=ticket,
organization=ticket.ticket_form.organization,
message= "A ticket you have been CC'd on has been updated",
is_read=False,
delete_by = timezone.now() + timedelta(days=60)
)

new_notification.save()

if ticket.assigned_to and ticket.assigned_to.user.usersettings_helpdesk.enable_notifications:
new_notification = Notification(
user=ticket.assigned_to,
ticket=ticket,
organization=ticket.ticket_form.organization,
message=message,
is_read=False,
delete_by = timezone.now() + timedelta(days=60)
)

new_notification.save()
return ticket

def process_message(message, importer, queues, logger, options=None):
Expand Down
16 changes: 14 additions & 2 deletions helpdesk/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.shortcuts import get_object_or_404

from helpdesk.lib import safe_template_context, process_attachments
from helpdesk.models import (Ticket, Queue, FollowUp, IgnoreEmail, TicketCC,
from helpdesk.models import (Ticket, Queue, FollowUp, IgnoreEmail, TicketCC, Notification,
CustomField, TicketDependency, UserSettings, KBItem, Tag,
FormType, KBCategory, KBIAttachment, is_extra_data, PreSetReply, EmailTemplate, clean_html)
from helpdesk import settings as helpdesk_settings
Expand Down Expand Up @@ -163,7 +163,6 @@ def customfield_to_field(self, field, instanceargs, kwargs={}):
class PreviewWidget(forms.widgets.Textarea):
template_name = "helpdesk/include/edit_md_preview.html"


class EditTicketForm(CustomFieldMixin, forms.ModelForm):

class Meta:
Expand Down Expand Up @@ -1388,3 +1387,16 @@ def clean_tickets(self):
if len(queues) != 1:
raise ValidationError(_('All selected tickets must share the same queue in order to be merged.'))
return tickets

class AnnouncementForm(forms.ModelForm):
class Meta:
model = Notification
exclude = ['user', 'ticket', 'organization', 'is_read', 'created', 'announcement']
widgets = {
'message': forms.Textarea(attrs={'class': 'form-control'}),
'delete_by': forms.DateInput(attrs={'id': 'datefield', 'class': 'form-control', 'type': 'datetime-local'})
}
help_texts = {
'message': 'Message to be displayed'
}

27 changes: 27 additions & 0 deletions helpdesk/management/commands/delete_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/python


from django.core.management.base import BaseCommand, CommandError

import datetime

from helpdesk.models import Notification

class Command(BaseCommand):
help = "Deletes read notifications over a day old and any expired announcements"

def handle(self, *args, **options):
notifications = Notification.objects.filter(announcement=False, is_read=True, delete_by__lt=datetime.datetime.now())
announcements = Notification.objects.filter(announcement=True, is_read=True, delete_by__lt=datetime.datetime.now())

if notifications.exists():
count_notifs = notifications.delete()[0]
self.stdout.write(self.style.SUCCESS(f'Successfully deleted {count_notifs} old read notifications'))
else:
self.stdout.write(self.style.WARNING('No old read notifications to delete'))

if announcements.exists():
count_announcements = announcements.delete()[0]
self.stdout.write(self.style.SUCCESS(f'Successfully deleted {count_announcements} expired announcements'))
else:
self.stdout.write(self.style.WARNING('No old read announcements to delete'))
36 changes: 36 additions & 0 deletions helpdesk/migrations/0103_add_notification_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 3.2.18 on 2024-06-05 16:22

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),
('orgs', '0056_alter_organization_map_help_text'),
('helpdesk', '0102_add_kbitem_unlisted'),
]

operations = [
migrations.AlterField(
model_name='kbitem',
name='unlisted',
field=models.BooleanField(default=False, help_text='Should this form be hidden from the public article list? (If the "Is this article publicly visible?" option is checked, this form will still be accessible by everyone through the link.)', verbose_name='Unlisted'),
),
migrations.CreateModel(
name='Notification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('message', models.TextField()),
('is_read', models.BooleanField()),
('created', models.DateTimeField(auto_now_add=True)),
('delete_by', models.DateTimeField()),
('announcement', models.BooleanField(default=False)),
('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='orgs.organization')),
('ticket', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='helpdesk.ticket', verbose_name='Ticket')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, verbose_name='User')),
],
),
]
18 changes: 18 additions & 0 deletions helpdesk/migrations/0104_alter_usersettings_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.18 on 2024-06-07 15:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('helpdesk', '0103_add_notification_model'),
]

operations = [
migrations.AddField(
model_name='usersettings',
name='enable_notifications',
field=models.BooleanField(default=True, help_text="Get notifications when tickets you've been assigned to or CC'd on are updated?", verbose_name='Enable notification when tickets are created or updated?'),
),
]
62 changes: 61 additions & 1 deletion helpdesk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import mimetypes
import datetime
from datetime import timedelta
import logging

from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -679,7 +680,7 @@ def send(role, recipient):

for field in email_fields:
if field in self.extra_data and self.extra_data[field] is not None and self.extra_data[field] != '' and is_extra_data(field):
send('extra', self.extra_data[field])
send('extra', self.extra_data[field])

return recipients

Expand Down Expand Up @@ -1846,6 +1847,12 @@ class UserSettings(models.Model):
default=use_email_as_submitter_default,
)

enable_notifications = models.BooleanField(
verbose_name=('Enable notification when tickets are created or updated?'),
help_text='Get notifications when tickets you\'ve been assigned to or CC\'d on are updated?',
default=True,
)

def __str__(self):
return 'Preferences for %s' % self.user

Expand Down Expand Up @@ -2248,3 +2255,56 @@ class Meta:

def __str__(self):
return '%s / %s' % (self.ticket, self.depends_on)

class Notification(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
blank=True,
null=True,
verbose_name=_('User'),
)

ticket = models.ForeignKey(
Ticket,
on_delete=models.CASCADE,
verbose_name=_('Ticket'),
blank=True,
null=True
)

organization = models.ForeignKey(
Organization,
on_delete=models.CASCADE
)

message = models.TextField()

is_read = models.BooleanField()

created = models.DateTimeField(
auto_now_add=True
)

delete_by = models.DateTimeField()

announcement = models.BooleanField(default=False)

def get_time_since_created(self):

time_diff = timezone.now() - self.created

days = time_diff.days
seconds = time_diff.seconds
hours = seconds // 3600
minutes = (seconds % 3600) // 60

if days > 0:
return self.created
elif hours > 0:
return f"{hours} hour(s) ago"
elif minutes > 0:
return f"{minutes} minute(s) ago"
else:
return "just now"

24 changes: 18 additions & 6 deletions helpdesk/static/helpdesk/css/sb-admin.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions helpdesk/static/helpdesk/css/sb-admin.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading