Notification Django cannot be display
I am making a notification in Django but it seems to be not working. How can I make a notification whenever a user add a incident report (model: Incident General), it will notify the roles: admin and super admin that a new report for this user is added and when the admin or super admin change the status it will notify the user of the change in status. Can someone help me with this? Thank you
Views
def ShowNotifications(request):
profile = get_object_or_404(UserProfile, user=request.user)
notifications = Notification.objects.filter(user=request.user).order_by('-date')
context = {
'profile': profile,
'notifications': notifications,
}
return render(request, 'notifications.html', context)
Models
class Notification(models.Model):
TYPES = ((1, 'Reports'), (2, 'User Accounts'), (3, 'Inbox'), (4, 'Attributes Builder'))
incident_report = models.ForeignKey('incidentreport.IncidentGeneral', on_delete=models.CASCADE, related_name="noti_report", blank=True, null=True)
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_to_user")
notification_type = models.IntegerField(choices=TYPES)
text_preview = models.CharField(max_length=90, blank=True)
date = models.DateTimeField(auto_now_add=True)
is_seen = models.BooleanField(default=False)
class IncidentGeneral(SoftDeleteModel):
PENDING = 1
APPROVED = 2
REJECTED = 3
STATUS = (
(PENDING, 'Pending'),
(APPROVED, 'Approved'),
(REJECTED, 'Rejected')
)
IF_DUPLICATE = (
('Duplicate', 'Duplicate'),
('Possible Duplicate', 'Possible Duplicate'),
('Not Duplicate', 'Not Duplicate')
)
WEATHER = (
('Clear Night', 'Clear Night'),
('Cloudy', 'Cloudy'),
('Day', 'Day'),
('Fog', 'Fog'),
('Hail', 'Hail'),
('Partially cloudy day', 'Partially cloudy day'),
('Partially cloudy night', 'Partially cloudy night'),
('Rain', 'Rain'),
('Rain', 'Rain'),
('Wind', 'Wind'),
)
LIGHT = (
('Dawn', 'Dawn'),
('Day', 'Day'),
('Dusk', 'Dusk'),
('Night', 'Night'),
)
SEVERITY = (
('Damage to Property', 'Damage to Property'),
('Fatal', 'Fatal'),
('Non-Fatal', 'Non-Fatal'),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, null=True, blank=True)
generalid = models.CharField(max_length=250, unique=True, null=True, blank=True)
description = models.TextField(max_length=250, blank=True)
address = models.CharField(max_length=250)
country = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=250, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
pin_code = models.CharField(max_length=6, blank=True, null=True)
latitude = models.FloatField(max_length=20, blank=True, null=True)
longitude = models.FloatField(max_length=20, blank=True, null=True)
geo_location = gismodels.PointField(blank=True, null=True, srid=4326) # New field
upload_photovideo = models.FileField(upload_to=path_and_rename, blank=True, null=True)
date = models.DateField(auto_now_add=False, auto_now=False, blank=True, null=True)
time = models.TimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
status = models.PositiveSmallIntegerField(choices=STATUS, blank=True, null=True)
duplicate = models.CharField(choices=IF_DUPLICATE,max_length=250, blank=True, null=True)
accident_factor = models.ForeignKey(AccidentCausation, on_delete=models.SET_NULL, blank=True, null=True)
collision_type = models.ForeignKey(CollisionType, on_delete=models.SET_NULL, blank=True, null=True)
crash_type = models.ForeignKey(CrashType, on_delete=models.SET_NULL, blank=True, null=True)
weather = models.CharField(choices=WEATHER, max_length=250,blank=True, null=True)
light = models.CharField(choices=LIGHT,max_length=250, blank=True, null=True)
severity = models.CharField(choices=SEVERITY, max_length=250, blank=True, null=True)
movement_code = models.CharField(max_length=250, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def get_status(self):
if self.status == 1:
incident_status = 'Pending'
elif self.status == 2:
incident_status = 'Approved'
elif self.status == 3:
incident_status = 'Rejected'
return incident_status
class IncidentNotif(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_receiver')
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_sender')
incident_general_report = models.ForeignKey(IncidentGeneral, on_delete=models.CASCADE, related_name='report_general')
def user_report_incident (sender, instance, *args, **kwargs):
incidentnotif = instance
incident_report = incidentnotif.incident_general_report
sender = incidentnotif.sender
notify = Notification(incident_report=incident_report, sender=sender, user=incident_report.user, notification_type=1)
notify.save()
post_save.connect(IncidentNotif.user_report_incident, sender=IncidentNotif)