Help with django signals and superuser

Hi guys!

I have this application in which users can request an absence and superuser approves it. I am trying to make a signal to trigger a notification. Righ now user request an absence and that same user receives the notification, but I want the admin - superuser to receive it. Can you help me?

The request absence model:

class Request(models.Model):
name = models.ForeignKey(User, on_delete=models.CASCADE)
CHOICES = (
(‘VACATION’, ‘Vacation Days’),
(‘MEDICAL’, ‘Medical Leave’),
(‘JURY’, ‘Jury Duty’),
(
‘REMOTE’,
‘Remote Work’,
),
)
absence_type = models.CharField(max_length=20,
choices=CHOICES,
default=“Remote Work”)
absence_comments = models.CharField(max_length=100)
start_date = models.DateTimeField(default=timezone.now)
end_date = models.DateTimeField(default=timezone.now)
begin_hour = models.TimeField(auto_now=False, null=True)
end_hour = models.TimeField(auto_now=False, null=True)

The notification model:

NOTIFICATION_TYPES = ((1, ‘Requested’), (2, ‘Replied’))

request = models.ForeignKey('absence.Request', on_delete=models.CASCADE, 
                                                                               related_name="noti_request",
                                                                               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=NOTIFICATION_TYPES)
text_preview = models.CharField(max_length=90, blank=True)
date = models.DateTimeField(auto_now_add=True)
is_seen = models.BooleanField(default=False)

Here is the code to the signal/notification:

def employee_requested_absence(sender, instance, *args, **kwargs):
request = instance
sender = request.name
notify = Notification(request=request,
sender=sender,
user=request.name,
notification_type=1)
notify.save()

post_save.connect(Request.employee_requested_absence, sender=Request)

Can you help me please? I am really stuck. Thank you so much for your attention!

First, when posting code in this forum, please enclose it between lines of three backtick - ` characters to preserve formatting and to prevent the forum software from interpreting special characters. This means you’ll have a line of ```, then your code, then another line of ```. I’ve done this with your code below:

def employee_requested_absence(sender, instance, *args, **kwargs):
    request = instance
    sender = request.name
    notify = Notification(request=request,
        sender=sender,
        user=request.name,
        notification_type=1)
    notify.save()

To answer your question, you can repeat these two lines as many times as needed to create these notifications for as many people as you wish:

    notify = Notification(request=request,
        sender=sender,
        user=request.name,
        notification_type=1)
    notify.save()

Thank you for the ``` tip.

My question is, how can I send it only to a superuser?

How do you define a “superuser” in the context of your application?

Then, how would you select superusers from your database?

For example, I have this model which is called ‘AbsenceManager’:

class AbsenceManager(models.Model):
    absence_manager = models.ForeignKey(User, on_delete=models.CASCADE)
    is_superuser = models.BooleanField(default=True)

And select these like this:

def absence_manager(request):
    absence_manager = AbsenceManager.objects.all()

So then that identifies which people it needs to be sent to.

What field in your Notification model identifies the recipient? That’s what you set to the superusers.

I created another notification model to test some things, and to answer you, in this case the recipient would be the ‘receiver’.

class NotificationToAdmin(models.Model):
    NOTIFICATION_TYPES = ((1, 'Requested'), (2, 'Updated'))
    request = models.ForeignKey('absence.Request',
                                on_delete=models.CASCADE,
                                related_name="noti_to_admin",
                                blank=True,
                                null=True)
    sender = models.ForeignKey(User,
                               on_delete=models.CASCADE,
                               related_name="noti_from_employee")
    receiver = models.ForeignKey('absence.AbsenceManager',
                                 on_delete=models.CASCADE,
                                 related_name="noti_to_admin")
    notification_type = models.IntegerField(choices=NOTIFICATION_TYPES)
    text_preview = models.CharField(max_length=90, blank=True)
    date = models.DateTimeField(auto_now_add=True)
    is_seen = models.BooleanField(default=False)

And the code to send notification:

    def employee_requested_absence(sender, instance, *args, **kwargs):
        request = instance
        sender = request.name
        notify = NotificationToAdmin(request=request,
                                     receiver=absence_manager.receiver,
                                     sender=sender,
                                     notification_type=1)
        notify.save()

But then, when I create the absence request it says >>> ‘name ‘absence_manager’ is not defined’.

I’m sorry if these are stupid questions, but I’m having some difficulties as I am a beginner. I appreciate your effort.

Assuming you have multiple superusers (or the possibility of multiple superusers), you want to get a list of those superusers, and then for each one (using a loop), create the NotificationToAdmin object.

(No need to apologize for the questions - we’re here to help you along. Everyone was a beginner at some point in time.)

Thank you. I will post here the solution after I figure it out!

Ok, the answer was this:

def employee_requested_absence(sender, instance, *args, **kwargs):
    obj = get_object_or_404(AbsenceManager, id=1)
    test = obj.absence_manager
    request = instance
    sender = request.name
    notify = Notification(request=request,
        sender=sender,
        user=request.name,
        receiver=test,
        notification_type=1)
    notify.save()

Now it sends to the AbsenceManager of id=1.

Thank you for your help!