Permalink
Browse files

Added message_composed signal and hooked up the invalidate count cach…

…e function with it.
  • Loading branch information...
1 parent bb3938c commit 9efe5354c5d71e03716db3db496d370e07f040f7 @jezdez jezdez committed Nov 20, 2012
@@ -2,13 +2,17 @@
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
+
from .models import *
from .fields import CommaSeparatedUserField
from .utils import reply_to_thread, now
+from .signals import message_composed
+
if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID:
from sendgrid_parse_api.utils import create_reply_email
+
notification = None
if "notification" in settings.INSTALLED_APPS:
from notification import models as notification
@@ -48,9 +52,11 @@ def save(self, sender, send=True):
sender_part.replied_at = sender_part.read_at = now()
sender_part.save()
- thread.save() # save this last, since this updates the search index
+ thread.save() # save this last, since this updates the search index
- invalidate_count_cache(new_message)
+ message_composed.send(sender=Message,
+ message=new_message,
+ recipients=recipients)
#send notifications
if send and notification:
@@ -3,6 +3,7 @@
from django.utils.html import strip_tags
from . import settings as sendgrid_settings
+from .signals import message_composed
logger = logging.getLogger('threaded_messages')
@@ -37,4 +38,5 @@ def start_listening():
logger.debug("Sendgrid start listening")
email_received.connect(signal_received_email, dispatch_uid="thm_reply")
-
+ from .utils import invalidate_count_cache
+ message_composed.connect(invalidate_count_cache, dispatch_uid="thm_composed")
@@ -5,6 +5,7 @@
from django.db.models import F, Q
from .listeners import start_listening
+
from .settings import INBOX_COUNT_CACHE, INBOX_COUNT_CACHE_TIME
start_listening()
@@ -207,10 +208,3 @@ def cached_inbox_count_for(user):
def inbox_count_for(user):
return Participant.objects.inbox_for(user, read=False).count()
-
-
-def invalidate_count_cache(message):
- from .utils import fill_count_cache
- for thread in message.thread.select_related().all():
- for participant in thread.participants.exclude(user=message.sender):
- fill_count_cache(participant.user)
@@ -0,0 +1,3 @@
+import django.dispatch
+
+message_composed = django.dispatch.Signal(providing_args=["message", "recipients"])
@@ -1,5 +1,4 @@
# -*- coding:utf-8 -*-
-import HTMLParser
import datetime
import re
@@ -9,7 +8,6 @@
from django.template.loader import get_template
from . import settings as tm_settings
-from .models import Message, Participant, inbox_count_for, invalidate_count_cache
if "notification" in settings.INSTALLED_APPS:
@@ -31,8 +29,9 @@
def fill_count_cache(user):
+ from .models import inbox_count_for
cache.set(tm_settings.INBOX_COUNT_CACHE % user.pk,
- inbox_count_for(user), tm_settings.INBOX_COUNT_CACHE_TIME)
+ inbox_count_for(user), tm_settings.INBOX_COUNT_CACHE_TIME)
def open_message_thread(recipients, subject, template,
@@ -44,7 +43,7 @@ def open_message_thread(recipients, subject, template,
else:
body = message
- from forms import ComposeForm # temporary here to remove circular dependence
+ from forms import ComposeForm # temporary here to remove circular dependence
compose_form = ComposeForm(data={
"recipient": recipients,
"subject": subject,
@@ -56,7 +55,8 @@ def open_message_thread(recipients, subject, template,
return (thread, new_message)
-def reply_to_thread(thread,sender, body):
+def reply_to_thread(thread, sender, body):
+ from .models import Message, Participant
new_message = Message.objects.create(body=body, sender=sender)
new_message.parent_msg = thread.latest_msg
thread.latest_msg = new_message
@@ -69,14 +69,14 @@ def reply_to_thread(thread,sender, body):
for participant in thread.participants.all():
participant.deleted_at = None
participant.save()
- if sender != participant.user: # dont send emails to the sender!
+ if sender != participant.user: # dont send emails to the sender!
recipients.append(participant.user)
sender_part = Participant.objects.get(thread=thread, user=sender)
sender_part.replied_at = sender_part.read_at = now()
sender_part.save()
- invalidate_count_cache(new_message)
+ invalidate_count_cache(Message, new_message)
if notification:
for r in recipients:
@@ -120,22 +120,29 @@ def strip_mail(body):
break
# strip quotes
- for i,l in enumerate(lines):
+ for i, l in enumerate(lines):
if l.lstrip().startswith('>'):
if not custom_line_no:
- custom_line_no = i-1
+ custom_line_no = i - 1
popme = custom_line_no
while not lines[popme]:
lines.pop(popme)
- popme -=1
+ popme -= 1
if re.search(r'^.*?([0-1][0-9]|[2][0-3]):([0-5][0-9]).*?$', lines[popme]) or re.search(r'[\w-]+@([\w-]+\.)+[\w-]+', lines[popme]):
lines.pop(popme)
break
stripped_lines = [s for s in lines if not s.lstrip().startswith('>')]
# strip last empty string in the list if it exists
- if not stripped_lines[-1]: stripped_lines.pop()
+ if not stripped_lines[-1]:
+ stripped_lines.pop()
# stripped message
return ('\n').join(stripped_lines)
+
+
+def invalidate_count_cache(sender, message, recipients=None, **kwargs):
+ for thread in message.thread.select_related().all():
+ for participant in thread.participants.exclude(user=message.sender):
+ fill_count_cache(participant.user)

0 comments on commit 9efe535

Please sign in to comment.