These are chat archives for django/django

24th
Jun 2018
Roel
@roelzkie15
Jun 24 2018 07:33
I have a question in how asynchronous celery works
Curtis Maloney
@funkybob
Jun 24 2018 07:35
you push tasks into a queue... worker tasks pull off the jobs and complete them
Roel
@roelzkie15
Jun 24 2018 07:38

In js an example of asynchronous call behavior is to use Promise.

You can get the data if successful through .then() otherwise the .catch() method is executed.

Now in celery:

@celery.task()
def add(a, b):
    return a + b

def some_view():
     add.delay(1, 2)
    return HttpResponse("Here's the text of the Web page.")

My question is how to know that the add.delay() is successfully finished before the request cycle ends in HTTPResponse?

I would like to make a defensive catch if the add task fails then send error notification to the client and if the task completed it will also notify client positively.
Curtis Maloney
@funkybob
Jun 24 2018 07:42
no... that's not how celery works
celery is a task queue system... for doing work completely out of band of the http request
it's not that sort of "async"... it's just run in another task
Roel
@roelzkie15
Jun 24 2018 07:43
I get it now.
So how can you determine to notify the client if things went wrong or is completed?
Curtis Maloney
@funkybob
Jun 24 2018 07:50
they'd have to come back later
Roel
@roelzkie15
Jun 24 2018 07:50
Other use case is sending email in the background using celery. I can't still figure out how to handle the celery state this way if all email was sent successfully or not. Hope you can shed some light.
Curtis Maloney
@funkybob
Jun 24 2018 07:52
well, you have a business decision to make... what to do if the email sending "fails"
you can retry, or pass a message back for the user to see, or...
Roel
@roelzkie15
Jun 24 2018 08:05
Well let me try
Sanjeev Sharma
@thesanjeevsharma
Jun 24 2018 14:02
Hello everyone! I am developing my blog site. The first page is set to display only first 12 blogs. using 'ListView...[:12]. Page will have 'next page' button at the end. How do I set it up so everytime 'next page' gets clicked django picks next 12 blogs to display, and same applies to 'prev page', it should display 12 previous blogs. Any idea?
Himanshu Shankar
@iamhssingh
Jun 24 2018 14:57
On every click make a request to Django? πŸ™„πŸ™„
I use DjangoRestFramework so my FE part is a separate app. But the function remains same. You are looking for something called as pagination.
Sanjeev Sharma
@thesanjeevsharma
Jun 24 2018 14:58
Yes! but for that I'll have to store the number(post loaded until now) and render next 12 posts from that. I thought of that. But is there any other way?
Here it is. xD Thanks for the term. :)
Himanshu Shankar
@iamhssingh
Jun 24 2018 15:00
Got it? @thesanjeevsharma
Sanjeev Sharma
@thesanjeevsharma
Jun 24 2018 15:00
Yeah!
Salar Rahmanian
@softinio
Jun 24 2018 16:03
@funkybob got it ! So im my app I have a file called receivers.py that I register all my receivers. I use the @receiver decorator to do it
@receiver(user_logged_in, dispatch_uid='authentication.handle_user_logged_in')
def handle_user_logged_in(sender, user=None, request=None, **kwargs):
    if hasattr(user, 'username'):
        details = USER_LOGGED_IN.format(user.username)
        log_user_login_event(event_details=details, user=user, request=request)
    else:
        logging.debug("No username found to audit log user_logged_in")
for the test I created one as a nested function ie:
def  test_my_receiver(self,…..):
         …….
         self.signal_was_called = False
        @receiver(user_logged_in, dispatch_uid='authentication.handle_user_logged_in’)
        def handle(sender, user=None, request=None, **kwargs):
             self.signal_was_called = True

        self.assertEquals(self.signal_was_called, True)
Salar Rahmanian
@softinio
Jun 24 2018 16:09
when running test if I put print statement or breakpoint in main app handler it works, but in test itself self.signal_was_called = True never gets called
Sanjeev Sharma
@thesanjeevsharma
Jun 24 2018 18:09
Hello everyone! I am creating a blog website. Whenever I post something through admin panel, it gets displayed as one paragraph. I want it to have a few paragraphs and a lil bit of text formatting like bold, etc. Any idea?
Salar Rahmanian
@softinio
Jun 24 2018 21:33
@funkybob got it working thanks to your hint ! Changed where I was registering it inside the test and it worked :-) you are a genie-i :smile:
thanks dude
Erick Agrazal
@ErickAgrazal
Jun 24 2018 23:14

Hello everyone! I am creating a blog website. Whenever I post something through admin panel, it gets displayed as one paragraph. I want it to have a few paragraphs and a lil bit of text formatting like bold, etc. Any idea?

@thesanjeevsharma https://github.com/aljosa/django-tinymce β€”> This is one I use.