-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tuned urls.py to work with nicks with spaces
- Loading branch information
Showing
83 changed files
with
8,323 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
VERSION = (0, 6, 0,) | ||
__version__ = '.'.join(map(str, VERSION)) | ||
default_app_config = 'django_messages.apps.DjangoMessagesConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from django import forms | ||
from django.conf import settings | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.contrib import admin | ||
from django.contrib.auth.models import Group | ||
|
||
from django_messages.utils import get_user_model | ||
User = get_user_model() | ||
|
||
if "pinax.notifications" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True): | ||
from pinax.notifications import models as notification | ||
else: | ||
notification = None | ||
|
||
from django_messages.models import Message | ||
|
||
class MessageAdminForm(forms.ModelForm): | ||
""" | ||
Custom AdminForm to enable messages to groups and all users. | ||
""" | ||
group = forms.ChoiceField(label=_('group'), required=False, | ||
help_text=_('Creates the message optionally for all users or a group of users.')) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(MessageAdminForm, self).__init__(*args, **kwargs) | ||
self.fields['group'].choices = self._get_group_choices() | ||
self.fields['recipient'].required = True | ||
|
||
def _get_group_choices(self): | ||
return [('', u'---------'), ('all', _('All users'))] + \ | ||
[(group.pk, group.name) for group in Group.objects.all()] | ||
|
||
class Meta: | ||
model = Message | ||
fields = ('sender', 'recipient', 'group', 'parent_msg', 'subject', | ||
'body', 'sent_at', 'read_at', 'replied_at', 'sender_deleted_at', | ||
'recipient_deleted_at') | ||
|
||
class MessageAdmin(admin.ModelAdmin): | ||
form = MessageAdminForm | ||
fieldsets = ( | ||
(None, { | ||
'fields': ( | ||
'sender', | ||
('recipient', 'group'), | ||
), | ||
}), | ||
(_('Message'), { | ||
'fields': ( | ||
'parent_msg', | ||
'subject', 'body', | ||
), | ||
'classes': ('monospace' ), | ||
}), | ||
(_('Date/time'), { | ||
'fields': ( | ||
'sent_at', 'read_at', 'replied_at', | ||
'sender_deleted_at', 'recipient_deleted_at', | ||
), | ||
'classes': ('collapse', 'wide'), | ||
}), | ||
) | ||
list_display = ('subject', 'sender', 'recipient', 'sent_at', 'read_at') | ||
list_filter = ('sent_at', 'sender', 'recipient') | ||
search_fields = ('subject', 'body') | ||
raw_id_fields = ('sender', 'recipient', 'parent_msg') | ||
|
||
def save_model(self, request, obj, form, change): | ||
""" | ||
Saves the message for the recipient and looks in the form instance | ||
for other possible recipients. Prevents duplication by excludin the | ||
original recipient from the list of optional recipients. | ||
When changing an existing message and choosing optional recipients, | ||
the message is effectively resent to those users. | ||
""" | ||
obj.save() | ||
|
||
if notification: | ||
# Getting the appropriate notice labels for the sender and recipients. | ||
if obj.parent_msg is None: | ||
sender_label = 'messages_sent' | ||
recipients_label = 'messages_received' | ||
else: | ||
sender_label = 'messages_replied' | ||
recipients_label = 'messages_reply_received' | ||
|
||
# Notification for the sender. | ||
notification.send([obj.sender], sender_label, {'message': obj,}) | ||
|
||
if form.cleaned_data['group'] == 'all': | ||
# send to all users | ||
recipients = User.objects.exclude(pk=obj.recipient.pk) | ||
else: | ||
# send to a group of users | ||
recipients = [] | ||
group = form.cleaned_data['group'] | ||
if group: | ||
group = Group.objects.get(pk=group) | ||
recipients.extend( | ||
list(group.user_set.exclude(pk=obj.recipient.pk))) | ||
# create messages for all found recipients | ||
for user in recipients: | ||
obj.pk = None | ||
obj.recipient = user | ||
obj.save() | ||
|
||
if notification: | ||
# Notification for the recipient. | ||
notification.send([user], recipients_label, {'message' : obj,}) | ||
|
||
admin.site.register(Message, MessageAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
class DjangoMessagesConfig(AppConfig): | ||
name = 'django_messages' | ||
verbose_name = _('Messages') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django_messages.models import inbox_count_for | ||
|
||
def _user_is_authenticated(user): | ||
# django < 2.0 | ||
try: | ||
return user.is_authenticated() | ||
except TypeError: | ||
# django >= 2.0 | ||
return user.is_authenticated | ||
|
||
def inbox(request): | ||
if _user_is_authenticated(request.user): | ||
return {'messages_inbox_count': inbox_count_for(request.user)} | ||
else: | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Based on http://www.djangosnippets.org/snippets/595/ | ||
by sopelkin | ||
""" | ||
|
||
from django import forms | ||
from django.forms import widgets | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from django_messages.utils import get_user_model, get_username_field | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CommaSeparatedUserInput(widgets.Input): | ||
input_type = 'text' | ||
|
||
def render(self, name, value, **kwargs): | ||
if value is None: | ||
value = '' | ||
elif isinstance(value, (list, tuple)): | ||
value = (', '.join([getattr(user, get_username_field()) for user in value])) | ||
return super(CommaSeparatedUserInput, self).render(name, value, **kwargs) | ||
|
||
|
||
|
||
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 | ||
|
||
names = set(value.split(',')) | ||
names_set = set([name.strip() for name in names if name.strip()]) | ||
users = list(User.objects.filter(**{'%s__in' % get_username_field(): names_set})) | ||
unknown_names = names_set ^ set([getattr(user, get_username_field()) 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(getattr(r, get_username_field())) | ||
|
||
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 | ||
|
||
def prepare_value(self, value): | ||
if value is None: | ||
value = '' | ||
elif isinstance(value, (list, tuple)): | ||
value = (', '.join([getattr(user, get_username_field()) for user in value])) | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django import forms | ||
from django.conf import settings | ||
from django.utils.translation import ugettext_lazy as _ | ||
from django.utils import timezone | ||
|
||
if "pinax.notifications" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True): | ||
from pinax.notifications import models as notification | ||
else: | ||
notification = None | ||
|
||
from django_messages.models import Message | ||
from django_messages.fields import CommaSeparatedUserField | ||
|
||
class ComposeForm(forms.Form): | ||
""" | ||
A simple default form for private messages. | ||
""" | ||
recipient = CommaSeparatedUserField(label=_(u"Recipient")) | ||
subject = forms.CharField(label=_(u"Subject"), max_length=140) | ||
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, parent_msg=None): | ||
recipients = self.cleaned_data['recipient'] | ||
subject = self.cleaned_data['subject'] | ||
body = self.cleaned_data['body'] | ||
message_list = [] | ||
for r in recipients: | ||
msg = Message( | ||
sender = sender, | ||
recipient = r, | ||
subject = subject, | ||
body = body, | ||
) | ||
if parent_msg is not None: | ||
msg.parent_msg = parent_msg | ||
parent_msg.replied_at = timezone.now() | ||
parent_msg.save() | ||
msg.save() | ||
message_list.append(msg) | ||
if notification: | ||
if parent_msg is not None: | ||
notification.send([sender], "messages_replied", {'message': msg,}) | ||
notification.send([r], "messages_reply_received", {'message': msg,}) | ||
else: | ||
notification.send([sender], "messages_sent", {'message': msg,}) | ||
notification.send([r], "messages_received", {'message': msg,}) | ||
return message_list |
Binary file not shown.
Oops, something went wrong.