automatic page update appengine - python

I have an application that uses Python appengine, there is a service that updates the status of users, if an admin person has a page open, I would need it to update in real time. I know that appengine has CRON and task queues, what would be the correct way to handle this? Should I set an update flag in the models that that triggers jscript?

The Channel API can be used to send real-time(ish) data to clients, without the need of clients polling the server.

Related

How to send data to running django management command?

I have a custom django management command that is constantly running through the supervisor (keep alive)
I need under certain circumstances (for example, a signal in the project, or a change in the database) the django management process reacted to these changes.
Tried looking for something similar but can't seem to find a simple implementation.
UPD: In the management command I start the stream process to the twitter API to track new tweets for the tags from django database. When adding a new tag to the database, I want to restart the stream connection.

How to transfer Django's messages to Angular app

I am building Django 2.2 +Node + Angular 8 app. Django is used to run a couple of simple scrapers when user clicks on Search btn. I want to make user notified that the scraping process started successfully (if it is the fact) and that the scraping is finished, or that some errors occurred. Some intermediate statuses are also desirable, but not mandatory. I thought about using django.contrib.messages, but not sure how to make my Angular app receive them.
Can anybody advise me with that problem?
P.S.: not sure if it is important - I want to use Angular's snackbar to make user notified about scraping statuses.
there is only one common way to push messages from Django to angular (from server -> to client), is to create a web-socket.
Check out this tutorial to create a web-socket that initiate a connexion between Angular and Django, and then pushing random notificatons from Django to Angular and finally closing the websocket.
In your case :
Open Web Socket when starting the task
Sending notifications about the task (DJANGO -> NG)
Sending Final Notification and Closing Web Socket
For other ideas checkout this medium: Do you really need websockets ?

Autoposting to Facebook page from django-rest-framework app

I have a simple Django Rest Framework app running. I want to auto post to app's Facebook page whenever new item is saved in the db. I was wondering if Django management commands is the best solution. The other option is google cloud functions to make a request at specified time and post to Facebook if there are new items (could be expensive and unnecessarily complex).
Any suggestions would be appreciated.
You can listen to your item model's post_save signal and connect it to a handler function which is responsible for posting your saved item data to a Facebook page. Refer to this Django Docs page on how to connect your handler to a signal.
For better performance, posting to a Facebook page can be done asynchronously by leveraging task queue like Celery so it won't affect your REST API response time.

Django how best to perform api request for large jobs

I need some direction as to how to achieve the following functionality using Django.
I want my application to enable multiple users to submit jobs to make calls to an API.
Each user job will require multiple API calls and will store the results in a db or a file.
Each user should be able to submit multiple jobs.
In case of some failure such as network blocked or API not returning results I want the application to pause for a while and then resume completing that job.
Basically want the application to pickup from where it was left off.
Any ideas as to how I could implement this solution or any technologies such as celery I should be looking at or even if you can suggest an opensource project where I can learn how to perform this would be a great help.
You can do this with rabbitmq and celery.
This post might be helpful.
https://medium.com/#ffreitasalves/executing-time-consuming-tasks-asynchronously-with-django-and-celery-8578eebab356

Get list of connected users with Django

I'm looking for a way to keep track of users that are online/offline. So if I present all users in a list i could have an icon or some kind of flag to show this. Is this built in in Django's default Auth system?
My first thought was to simply have a field in my profiles called last_logout in the models and update it with the date/time each time user logged out.
With this info and the built in last_login I should be able to make some kind of function to determine if the user is loggedin/online right?
Or should I just have a boolean field called "online" that I can change when user logs in and out?
With only django it will be hard to do. For such task async frameworks are more suitable.
For example, tornado.
Users won't do logout explicitly every time they go offline. They just close their browser and that's it. You can't know it with only django auth app. It is not designed for such tasks.
Even if you will check for not expired session, it not gives you all online users, because session can be non-expired for 30 days.
So to get real online users, possible solutions are:
Every user will send some data via javascript to your server, for example every 10 seconds. You can fetch that data on server and put user into cache and set cache key to be alive for 10 seconds. So when you need to know, who are online now, you'll check your cache. But it is not a good solution, because it will need a lot of server resources.
Use async framework (tornado) at server side (you can setup separate process for exact requests). And use websockets (SockJS is a good library for that at client side). It is a more complicated solution, but it is better.
You have to consider what exactly means for the users to be "online". Since any user can close the browser window any time and without the server knowing about that action, you'd end up having lots of false "online" users.
You have two basic options:
Keep track of the user's last activity time. Every time the user loads a page you'd update the value of the timer. To get a list of all online users you'd need to select the ones with an activity before X minutes. This is what is done by some web forums.
Open a websocket, long polling connection or some heartbeat to the server. This is what Facebook chat does. You'd need more than just django, since to keep a connection open another kind of server-side resources are needed.

Categories

Resources