0

From my research online there are many questions related to what i am about to ask. Solutions i saw do not solve my problem because mine is related with django channels and signals .

Here is a post save signal code in my consumer(channels).

private chat consummer

class ChatConsummer(AsyncConsumer):
    async def websocket_connect(self,event):
    await  self.channel_layer.group_add(
         "private_chat",
        self.channel_name,
    )
    data={
        'chats':data_array
    }


def Create_Notify(sender,instance,**kwargs):
    data={'notificat':'notication'}
    notification_data={
        'notify':data,
    }

    async_to_sync(get_channel_layer().group_send)("private_chat",    {"type": "Send_Message",'text':json.dumps(notification_data)})    
post_save.connect(Create_Notify,sender=notifyall)
def credit_gotten(instance):
    receiver_credit=0.0
return receiver_credit
#create a notification object when ever a rating model is saved
def save_rating(sender,instance,**kwargs):
    obj_notification=notifyall.objects.create()
post_save.connect(save_rating,sender=RatingModel)

So in my code, i am accessing the channel layer outside the consumer function meaning i can not use self.scope['user'] to get the current login user .

I am using post_save signal function making it impossible to use request to access the current login in user . So please help me, i need to access the current login user in my post_save signal which calls channel layer out outside a channel consumer function .

john
  • 29
  • 8
  • A post_save signal is just a convenient way to have something happen when your `notifyall` object is being saved, but it's not asynchronous and just performed in sequence where you call `notifyall.save()`. It's not clear where your `notifyall` object is actually being saved, but you should consider not using signals and just perform the notification at the place where you actually save your object (your view code), where you also have access to the logged in user. – dirkgroten Jul 24 '19 at 13:40
  • i understand your point, using signals is one of the best way to perform an action when ever notification object is saved . My call is i want to access the user object in the post _save signal , use a better approach to communicate with the signal object when ever is saved. – john Jul 24 '19 at 13:53
  • 1
    You can’t. There isn’t even a guarantee that there is a user or a request when the object is saved (eg if saved by admin it might be the wrong user or if saved via a management command there’s no user). Again, move the code to where you save the notification and know the user. – dirkgroten Jul 24 '19 at 13:56
  • A dirty hack is to set the `request` object as a global variable that you can access anywhere. – dirkgroten Jul 24 '19 at 13:57
  • Please give me an idea on how to create a dirty hack to access the current user objects anywhere in my code . I am using a post_save to create and save my notification signals. Which means signals are created only when an object is being saved or created – john Jul 24 '19 at 14:04
  • https://stackoverflow.com/q/3227180/1252711 – dirkgroten Jul 24 '19 at 14:06
  • that is out dated – john Jul 24 '19 at 17:27
  • No the answer at the bottom isn’t (> Django 1.10). Or search for threadlocal + Django request middleware you’ll find other posts. The principle remains to use threadlocal to set a global variable inside a middleware class. – dirkgroten Jul 24 '19 at 17:28

0 Answers0