Mehmet Kagan Kayaalp
Mehmet Kagan Kayaalp

Reputation: 565

Lock the system

I am writing a mini-CRM system that two users can login at the same time and they can answer received messages. However, the problem is that they might response the same message because messages can only disappear when they click "Response" button. Is there any suggestion to me to lock the system?

Upvotes: 0

Views: 59

Answers (2)

Curtis Olson
Curtis Olson

Reputation: 967

This sounds like a great case for an 'optimistic locking' approach. Here are two methods I've used with much success. Often, I combine the two methods to ensure no data is lost by mis-matched object instances on POSTs.

  1. The easy way: Add a version field to your model. On POST, check the POSTed version number vs. the object's version number. If they don't match, raise a validation error. If they do match, increment the version by 1.

  2. More elegant approach: Django's Generic Relations (part of the content types framework). A table which stores the content_type and object_id of the object that's locked, along with the user who 'owns' that lock. Check this lock on GET requests, and disable POSTing if it's 'locked' by another user. 'Release' the lock on a page unload, session end, or browser exit. You can get very creative with this approach.

Upvotes: 1

KravAn
KravAn

Reputation: 306

Add some boolean field (answered, is_answered.. etc) and check on every "Response" click if it answered. Hope it will help.

Upvotes: 1

Related Questions