Django: send message to all users (sessions) - django

I have a python+django project and want to implement following functionality: by certain trigger (at certain time or by manual admin action) show message to all active users (i.e. to all sessions).
Webpush seems unnecessary here as far as django has nice built-in messages subframework.
I guess I may develop such functionality manually: make sql table session_messages, each time take snapshot of all sessions and show message, perform some checks to assure that message is shown only once and only to active users, etc. etc.
Question: maybe there is some nice little library that already does it? Or even maybe django messages are able to do it "from the box"?
I've googled a bit but found only packages for webpush integrations.
Thanks :)

You must implement a software architecture based on django channels, redis or rabbitmq and signals
Basically you must open a socket at the moment the user logs in, add the authenticated user to a group, and when you trigger the event with the signals send a message to the group

Related

Intercept all exceptions from a certain app in Django/DRF

For example a have a main app, let’s say for users handling and secondary app where I would create logs, models change history, statistics, ets.
Generally most of the CRUD activity in main app tigers CREATE operations in secondary app via signals to create logs and stuff.
What I want to achieve is to avoid exceptions being raised by secondary app from being propagated and shown to user via DRF response or make them ‘fail silently’ in a way, as , for instance, is user would update his account and history log subsequently created in secondary model would raise Integrity error – better just continue and do nothing rather then notify user about it.
There are to main types of exceptions - IntegrityError and ValidationError.
I could try to try/except all validation ones and maybe use custom exception handler to intercept Integrity errors if I know constraints names but
a) I still can’t intercept them all as some of them are originated from Django source code
2) A lot of hardcode.
Question is – is it possible somehow to intercept all exceptions from a certain app and suppress them all?
Thank you.
I think you’re very aware that suppressing all exceptions is bad practice in it’s own right, however, considering your situation perhaps you can try something like this serializer.is_valid(raise_exception=False) in your APIs

Detect Presence in AWS AppSync

I am working on an app that relies heavily on detecting when users go offline and go back online. I wanted to do this with AWS AppSync, but I can't seem to find a way to do this in the documentation. Is there a way to do it in AppSync?
Thanks for the question. Detecting presence is not currently support out of the box but you can likely build similar features yourself depending on the use case.
For example, a resolver on a subscription field is invoked every time a new device tries to open a subscription. You can use this resolver field to update some data source to tell the rest of your system that some user is currently subscribed. If using something like DynamoDB, you can use a TTL field to have records automatically removed after a certain amount of time and then require a user to "ping" every N minutes to specify that they are still online.
You could also have your application call a mutation when it first starts to register the user as online, then have the application call another mutation when the app closes to register it as offline. You could combine this with TTLs to prevent stale records in situations where the app crashes or something prevents the call to register as offline.
Thanks for the suggestion and hope this helps in the meantime.

How do I update a web API resource by request while also reacting with backend?

How do you update (RESTful) resources in a web API from the client, when you also need the backend to take actions regarding these changes?
Let's say I have a RESTful web API with the following resources:
Worker - id, first_name, last_name, ...
Project - id, title, due_date, ..., worker [ref to Worker]. A project can exist only if it belongs to a worker.
In the client (which is typically a mobile app), users can retrieve a list of some worker's projects. They can then modify each project in that list (update), delete, or create new ones.
Changes must take place locally on the client side, until a "send" command is dispatched, only then the server should receive the updates. Kind of like a classic form use case.
The tricky part:
I need the backend to take actions according to each change, both individually and also as a whole. For example:
A user retrieved some worker's projects list, deleted a project, and also updated the due_date of another.
According to this change, the backend needs to both send push notifications to all of that project's members, and also recalculate the relevant worker's priorities according the total change in their projects (one was deleted, another got postponed...).
How do I achieve this in the best way?
If I update/delete/create each project by itself (with seperate POSTs, PUTs and DELETEs), when will the backend do the overall recalculation task?
If I update them all together as a bulk (with PUT), the backend will then need to understand what exactly changed (which got deleted, which modified...), which is a hard chore.
Another option I heard is to create a third utility resource, that's something like "WorkerProjectUpdater" that holds the changes that need to be made, like transactions, and then have a "daemon" going through it and actually committing the changes. This is also hard to achieve as in the real story there are many many types of modifications, and it'll be quite complex to create a resource (with a model and DB records) for every type of change.
I'm using Django with Django Rest Framework for that web service.
Appreciate your help!

How do I programmatically access items in the Workbox?

How does Sitecore find these items? I want to set up a schedule task to email my admins when there are items pending in the Workbox. Maybe there is already a feature like this? The only piece of the puzzle I am missing is how to easily identify when/if Workbox items exist.
Instead of sending emails when an item is in a workflow state why don't you try using the RSS feeds that Sitecore generates for each state. Details are in the Client Configuration Cookbook.
The majority of email clients have built in RSS readers which typically will show the feed as a separate "inbox". IMHO this is much better than email alerts which often get ignored because of how spammy they can get.
The item is shown in the Workbox as long as it is in the workflow and not in the final state. Take a look at this shared source component - it seems to be just your requirement. This one is also quite similar.

Asynchronous database update in Django?

I have a big form on my site. When the users fill it out and submit it, most of the data just gets dumped to the database, and then they get redirected to a new page. However, I'd also like to use the data to query another site, and then parse the results. That might take a bit longer. It's not essential that the user sees these results right away, so I was wondering if it's possible to asynchronously call a function that will handle this, and then return an HttpResponse from my view like usual without making them wait?
If so... how? Any particular libraries I should look at?
User RabbitMQ and Celery with django. If you are deployed on EC2, also look at SQS
You create a message from the request-response cycle and an alternative process or a cron keeps checking off the messages.