1đź‘Ť
If I understand your question correctly, you want the originator of a message to be able to delete a message, but for a recipient to still be able to view that message if it hasn’t yet been read?
So, instead of deleting the message when the originator hits the delete button, just have the delete mark the message has having been deleted. eg Add another field to your Message
.
is_deleted = models.BooleanField(default=False)
The originator’s read function would then filter on is_deleted
and not display those messages. The recipient wouldn’t filter on is_deleted
and so continue to see the messages. You would then have to decide on a policy to purge messages from the data store that are marked is_deleted
. eg. Perhaps once the message has been read by the recipient it can be automatically deleted.
You already have quite a few variables keeping track of a messages state. Perhaps it makes sense to collapse the message state to fewer enumerated type values.
1đź‘Ť
Django messages is not suitable for what you are doing because it is based on cookies or sessions, not on users. So, messages are not stored between sessions.
From what I understood, now if someone deletes the message, it is deleted from the database and thus becomes unreadable to anyone else, which you don’t want.
I will put your problem in more simple terms:
You have three models, user thread and message. Each user has a set of threads, and each thread has a set of messages. Each message has a sender and a recipient.
Now, each message has a set of own attributes (e.g. draft, body) and a set of attributes of the relationship between itself and the sender or recipient (e.g. “read” is not an attribute of the message itself, it is an attribute of the relationship between recipient and message (“the recipient has read the message”)).
What you want now is also an attribute on the relationship between the sender and the message (“the sender deleted the message”).
Like you did with “read”, you should have two attributes:
is_deleted_by_sender = models.BooleanField(default=False)
is_deleted_by_recipient = models.BooleanField(default=False)
In sender/recipient messages’ view, you check whether that user deleted the message or not. If he deleted the message, it no longer appears on its inbox. However, because the message still exists, the recipient/sender (notice the order) can still read it.
Messages that are deleted by both sender and recipient can be deleted from the database. Thus, either a scheduled job or after a deletion of one of the sides (sender and recipient) can trigger the message deletion from the database.
This approach is actually a simplified version of a more complex approach on which you want to define a set of attributes of the relationship of two models (in this case user and message), which can be made in Django using through.
- [Answered ]-Test that fails if exception is rised?
- [Answered ]-Basic celery task function call from inside a django function
- [Answered ]-How to use nested lookup in django?
- [Answered ]-Django json serialize doesn't return Object