diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-10-10 11:07:31 -0600 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-10-10 11:07:31 -0600 |
commit | 1f90a80a86690aa70b18e8770f55afb88838a8ae (patch) | |
tree | 4eec574b744f9156a10371b3ff79f5ae43d12568 | |
parent | 44c3e32981a1af6d507a9bb3648354f8e33e3b22 (diff) | |
download | askbot-1f90a80a86690aa70b18e8770f55afb88838a8ae.tar.gz askbot-1f90a80a86690aa70b18e8770f55afb88838a8ae.tar.bz2 askbot-1f90a80a86690aa70b18e8770f55afb88838a8ae.zip |
added fix for Message model
-rw-r--r-- | askbot/models/__init__.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index ee8bbc47..b1b26bd2 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -65,6 +65,8 @@ from askbot.utils.diff import textDiff as htmldiff from askbot.utils.url_utils import strip_path from askbot import mail +from django import get_version as django_version + def get_model(model_name): """a shortcut for getting model for an askbot app""" return models.get_model('askbot', model_name) @@ -116,6 +118,42 @@ def get_users_by_text_query(search_query, users_query_set = None): # models.Q(about__search = search_query) # ) +class CacheMessage(object): + '''An object to replace the django.contrib.auth.models.Message model + that stores the messages in the cache''' + + def __init__(self, user): + self.user = user + self.cache_key = 'user-message-%d' % self.user.id + self.messages = cache.cache.get(self.cache_key) or [] + + def _set_message(self, message): + self.messages.append(message) + cache.cache.set(self.cache_key, self.messages) + + def create(self, message=''): + if message: + self._set_message(message) + + def get_and_delete_messages(self): + cache.cache.delete(self.cache_key) + return_value = self.messages + self.messages = [] + return return_value + +@property +def user_message_set(self): + if not hasattr(self, '_message_set_cache'): + self._message_set_cache = CacheMessage(self) + return self._message_set_cache + +def user_get_and_delete_messages(self): + return self.message_set.get_and_delete_messages() + +if django_version() > '1.3.1': + User.add_to_class('message_set', user_message_set) + User.add_to_class('get_and_delete_messages', user_get_and_delete_messages) + User.add_to_class( 'status', models.CharField( @@ -3586,7 +3624,6 @@ def moderate_group_joining(sender, instance=None, created=False, **kwargs): content_object = group ) - #signal for User model save changes django_signals.pre_save.connect(make_admin_if_first_user, sender=User) django_signals.pre_save.connect(calculate_gravatar_hash, sender=User) |