Skip to content

Commit

Permalink
Nitpicking spaces!!!1!
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Jan 17, 2012
1 parent 39c55c7 commit 4c31494
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 94 deletions.
2 changes: 1 addition & 1 deletion threaded_messages/admin.py
Expand Up @@ -8,7 +8,7 @@

class MessageAdmin(admin.ModelAdmin):
list_display = ('sender', 'sent_at','body')

admin.site.register(Message, MessageAdmin)
admin.site.register(Thread)
admin.site.register(Participant)
16 changes: 8 additions & 8 deletions threaded_messages/fields.py
Expand Up @@ -11,7 +11,7 @@

class CommaSeparatedUserInput(widgets.Input):
input_type = 'text'

def render(self, name, value, attrs=None):
if value is None:
value = ''
Expand All @@ -22,37 +22,37 @@ def render(self, name, value, attrs=None):

class CommaSeparatedUserField(forms.Field):
widget = CommaSeparatedUserInput

def __init__(self, *args, **kwargs):
recipient_filter = kwargs.pop('recipient_filter', None)
self._recipient_filter = recipient_filter
super(CommaSeparatedUserField, self).__init__(*args, **kwargs)

def clean(self, value):
super(CommaSeparatedUserField, self).clean(value)
if not value:
return ''
if isinstance(value, (list, tuple)):
return value

#test if last char is a seperator, some tokenizers are not that smart
if value[-1] == ',':
value = value[0:-1]

names = set(value.split(','))
names_set = set([name.strip() for name in names])
users = list(User.objects.filter(username__in=names_set))
unknown_names = names_set ^ set([user.username for user in users])

recipient_filter = self._recipient_filter
invalid_users = []
if recipient_filter is not None:
for r in users:
if recipient_filter(r) is False:
users.remove(r)
invalid_users.append(r.username)

if unknown_names or invalid_users:
raise forms.ValidationError(_(u"The following usernames are incorrect: %(users)s") % {'users': ', '.join(list(unknown_names)+invalid_users)})

return users
24 changes: 12 additions & 12 deletions threaded_messages/forms.py
Expand Up @@ -15,7 +15,7 @@
notification = None
if "notification" in settings.INSTALLED_APPS:
from notification import models as notification

class ComposeForm(forms.Form):
"""
A simple default form for private messages.
Expand All @@ -24,49 +24,49 @@ class ComposeForm(forms.Form):
subject = forms.CharField(label=_(u"Subject"))
body = forms.CharField(label=_(u"Body"),
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))

def __init__(self, *args, **kwargs):
recipient_filter = kwargs.pop('recipient_filter', None)
super(ComposeForm, self).__init__(*args, **kwargs)
if recipient_filter is not None:
self.fields['recipient']._recipient_filter = recipient_filter

def save(self, sender, send=True):
recipients = self.cleaned_data['recipient']
subject = self.cleaned_data['subject']
body = self.cleaned_data['body']

new_message = Message.objects.create(body=body, sender=sender)

thread = Thread.objects.create(subject=subject,
latest_msg=new_message,
creator=sender)
thread.all_msgs.add(new_message)

for recipient in recipients:
Participant.objects.create(thread=thread, user=recipient)

(sender_part, created) = Participant.objects.get_or_create(thread=thread, user=sender)
sender_part.replied_at = sender_part.read_at = datetime.datetime.now()
sender_part.save()

thread.save() #save this last, since this updates the search index

#send notifications
if send and notification:
if sendgrid_settings.THREADED_MESSAGES_USE_SENDGRID:
for r in recipients:
reply_email = create_reply_email(sendgrid_settings.THREADED_MESSAGES_ID, r, thread)
notification.send(recipients, "received_email",
notification.send(recipients, "received_email",
{"thread": thread,
"message": new_message}, sender=sender,
from_email= reply_email.get_from_email(),
headers = {'Reply-To': reply_email.get_reply_to_email()})
else:
notification.send(recipients, "received_email",
notification.send(recipients, "received_email",
{"thread": thread,
"message": new_message}, sender=sender)

return (thread, new_message)


Expand All @@ -76,7 +76,7 @@ class ReplyForm(forms.Form):
"""
body = forms.CharField(label=_(u"Reply"),
widget=forms.Textarea(attrs={'rows': '4', 'cols':'55'}))

def save(self, sender, thread):
body = self.cleaned_data['body']
return reply_to_thread(thread, sender, body)
36 changes: 18 additions & 18 deletions threaded_messages/models.py
Expand Up @@ -12,7 +12,7 @@
start_listening()

class MessageManager(models.Manager):

def inbox_for(self, user, read=None, only_unreplied=None):
"""
Returns all messages that were received by the given user and are not
Expand All @@ -36,9 +36,9 @@ def inbox_for(self, user, read=None, only_unreplied=None):
if only_unreplied == True:
inbox = inbox.filter(Q(replied_at__isnull=True)
|Q(replied_at__lt=F("thread__latest_msg__sent_at")))

return inbox

def outbox_for(self, user):
"""
Returns all messages that were sent by the given user and are not
Expand All @@ -49,7 +49,7 @@ def outbox_for(self, user):
replied_at__isnull=False,
deleted_at__isnull=True,
)

def trash_for(self, user):
"""
Returns all messages that were either received or sent by the given
Expand All @@ -69,15 +69,15 @@ class Message(models.Model):
sender = models.ForeignKey(User, related_name='sent_messages', blank=True, null=True, verbose_name=_("sender"))
parent_msg = models.ForeignKey('self', related_name='next_messages', blank=True, null=True, verbose_name=_("parent message"))
sent_at = models.DateTimeField(_("sent at"), auto_now_add=True)

def __unicode__(self):
return "%s - %s" % (str(self.sender), self.sent_at)

def save(self, **kwargs):
if not self.id:
self.sent_at = datetime.datetime.now()
super(Message, self).save(**kwargs)

class Meta:
ordering = ['-sent_at']
verbose_name = _("Message")
Expand All @@ -94,20 +94,20 @@ class Thread(models.Model):
# the following fields are used to filter out messages that have not been replied to in the inbox
creator = models.ForeignKey(User, related_name='created_threads', verbose_name=_("creator"))
replied = models.BooleanField(editable=False, default=False)

def __unicode__(self):
return self.subject

def get_absolute_url(self):
return ('messages_detail', [self.id])
get_absolute_url = models.permalink(get_absolute_url)

class Meta:
ordering = ['latest_msg']
verbose_name = _("Thread")
verbose_name_plural = _("Threads")


class Participant(models.Model):
"""
Thread manager for each participant
Expand All @@ -117,9 +117,9 @@ class Participant(models.Model):
read_at = models.DateTimeField(_("read at"), null=True, blank=True)
replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
deleted_at = models.DateTimeField(_("deleted at"), null=True, blank=True)

objects = MessageManager()

def new(self):
"""returns whether the recipient has read the message or not"""
if self.read_at is None or self.read_at < self.thread.latest_msg.sent_at:
Expand All @@ -132,7 +132,7 @@ def replied(self):
or self.replied_at < self.thread.latest_msg.sent_at:
return True
return False

def last_other_sender(self):
"""returns the last sender thats not the viewing user. if nobody
besides you sent a message to the thread we take a random one
Expand All @@ -145,7 +145,7 @@ def last_other_sender(self):
if others:
return others[0].user
return None

def others(self):
"""returns the other participants of the thread"""
return self.thread.participants.exclude(user=self.user)
Expand All @@ -166,10 +166,10 @@ def get_previous(self):
return participation
except:
return None

def __unicode__(self):
return "%s - %s" % (str(self.user), self.thread.subject)

class Meta:
ordering = ['thread']
verbose_name = _("participant")
Expand Down
2 changes: 0 additions & 2 deletions threaded_messages/search_indexes.py
Expand Up @@ -14,5 +14,3 @@ def prepare_participants(self, object):

def get_model(self):
return Thread


14 changes: 7 additions & 7 deletions threaded_messages/templatetags/inbox.py
Expand Up @@ -4,7 +4,7 @@
class InboxOutput(Node):
def __init__(self, varname=None):
self.varname = varname

def render(self, context):
try:
user = context['user']
Expand All @@ -16,21 +16,21 @@ def render(self, context):
return ""
else:
return "%s" % (count)

def do_print_inbox_count(parser, token):
"""
A templatetag to show the unread-count for a logged in user.
Returns the number of unread messages in the user's inbox.
Usage::
{% load inbox %}
{% inbox_count %}
{# or assign the value to a variable: #}
{% inbox_count as my_var %}
{{ my_var }}
"""
bits = token.contents.split()
if len(bits) > 1:
Expand All @@ -42,5 +42,5 @@ def do_print_inbox_count(parser, token):
else:
return InboxOutput()

register = Library()
register = Library()
register.tag('inbox_count', do_print_inbox_count)
8 changes: 4 additions & 4 deletions threaded_messages/urls.py
Expand Up @@ -15,16 +15,16 @@
url(r'^undelete/(?P<thread_id>[\d]+)/$', undelete, name='messages_undelete'),
url(r'^batch-update/$', batch_update, name='messages_batch_update'),
url(r'^trash/$', trash, name='messages_trash'),

url(r"^recipient-search/$", recipient_search, name="recipient_search"),
url(r'^message-reply/(?P<thread_id>[\d]+)/$', message_ajax_reply, name="message_reply"),
# modal composing

# modal composing
url(r'^modal-compose/(?P<recipient>[\w.+-_]+)/$', compose, {
"template_name":"django_messages/modal_compose.html",
"form_class": ComposeForm
}, name='modal_messages_compose_to'),

url(r'^modal-compose/$', compose, {
"template_name":"django_messages/modal_compose.html",
"form_class": ComposeForm
Expand Down

0 comments on commit 4c31494

Please sign in to comment.