Skip to content

Commit d2ece2a

Browse files
committed
tuned urls.py to work with nicks with spaces
1 parent 2b97592 commit d2ece2a

File tree

83 files changed

+8323
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+8323
-1
lines changed

build/lib/django_messages/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VERSION = (0, 6, 0,)
2+
__version__ = '.'.join(map(str, VERSION))
3+
default_app_config = 'django_messages.apps.DjangoMessagesConfig'

build/lib/django_messages/admin.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from django import forms
2+
from django.conf import settings
3+
from django.utils.translation import gettext_lazy as _
4+
from django.contrib import admin
5+
from django.contrib.auth.models import Group
6+
7+
from django_messages.utils import get_user_model
8+
User = get_user_model()
9+
10+
if "pinax.notifications" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
11+
from pinax.notifications import models as notification
12+
else:
13+
notification = None
14+
15+
from django_messages.models import Message
16+
17+
class MessageAdminForm(forms.ModelForm):
18+
"""
19+
Custom AdminForm to enable messages to groups and all users.
20+
"""
21+
group = forms.ChoiceField(label=_('group'), required=False,
22+
help_text=_('Creates the message optionally for all users or a group of users.'))
23+
24+
def __init__(self, *args, **kwargs):
25+
super(MessageAdminForm, self).__init__(*args, **kwargs)
26+
self.fields['group'].choices = self._get_group_choices()
27+
self.fields['recipient'].required = True
28+
29+
def _get_group_choices(self):
30+
return [('', u'---------'), ('all', _('All users'))] + \
31+
[(group.pk, group.name) for group in Group.objects.all()]
32+
33+
class Meta:
34+
model = Message
35+
fields = ('sender', 'recipient', 'group', 'parent_msg', 'subject',
36+
'body', 'sent_at', 'read_at', 'replied_at', 'sender_deleted_at',
37+
'recipient_deleted_at')
38+
39+
class MessageAdmin(admin.ModelAdmin):
40+
form = MessageAdminForm
41+
fieldsets = (
42+
(None, {
43+
'fields': (
44+
'sender',
45+
('recipient', 'group'),
46+
),
47+
}),
48+
(_('Message'), {
49+
'fields': (
50+
'parent_msg',
51+
'subject', 'body',
52+
),
53+
'classes': ('monospace' ),
54+
}),
55+
(_('Date/time'), {
56+
'fields': (
57+
'sent_at', 'read_at', 'replied_at',
58+
'sender_deleted_at', 'recipient_deleted_at',
59+
),
60+
'classes': ('collapse', 'wide'),
61+
}),
62+
)
63+
list_display = ('subject', 'sender', 'recipient', 'sent_at', 'read_at')
64+
list_filter = ('sent_at', 'sender', 'recipient')
65+
search_fields = ('subject', 'body')
66+
raw_id_fields = ('sender', 'recipient', 'parent_msg')
67+
68+
def save_model(self, request, obj, form, change):
69+
"""
70+
Saves the message for the recipient and looks in the form instance
71+
for other possible recipients. Prevents duplication by excludin the
72+
original recipient from the list of optional recipients.
73+
74+
When changing an existing message and choosing optional recipients,
75+
the message is effectively resent to those users.
76+
"""
77+
obj.save()
78+
79+
if notification:
80+
# Getting the appropriate notice labels for the sender and recipients.
81+
if obj.parent_msg is None:
82+
sender_label = 'messages_sent'
83+
recipients_label = 'messages_received'
84+
else:
85+
sender_label = 'messages_replied'
86+
recipients_label = 'messages_reply_received'
87+
88+
# Notification for the sender.
89+
notification.send([obj.sender], sender_label, {'message': obj,})
90+
91+
if form.cleaned_data['group'] == 'all':
92+
# send to all users
93+
recipients = User.objects.exclude(pk=obj.recipient.pk)
94+
else:
95+
# send to a group of users
96+
recipients = []
97+
group = form.cleaned_data['group']
98+
if group:
99+
group = Group.objects.get(pk=group)
100+
recipients.extend(
101+
list(group.user_set.exclude(pk=obj.recipient.pk)))
102+
# create messages for all found recipients
103+
for user in recipients:
104+
obj.pk = None
105+
obj.recipient = user
106+
obj.save()
107+
108+
if notification:
109+
# Notification for the recipient.
110+
notification.send([user], recipients_label, {'message' : obj,})
111+
112+
admin.site.register(Message, MessageAdmin)

build/lib/django_messages/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
from django.utils.translation import ugettext_lazy as _
3+
4+
class DjangoMessagesConfig(AppConfig):
5+
name = 'django_messages'
6+
verbose_name = _('Messages')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django_messages.models import inbox_count_for
2+
3+
def _user_is_authenticated(user):
4+
# django < 2.0
5+
try:
6+
return user.is_authenticated()
7+
except TypeError:
8+
# django >= 2.0
9+
return user.is_authenticated
10+
11+
def inbox(request):
12+
if _user_is_authenticated(request.user):
13+
return {'messages_inbox_count': inbox_count_for(request.user)}
14+
else:
15+
return {}

build/lib/django_messages/fields.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Based on http://www.djangosnippets.org/snippets/595/
3+
by sopelkin
4+
"""
5+
6+
from django import forms
7+
from django.forms import widgets
8+
from django.utils.translation import ugettext_lazy as _
9+
10+
from django_messages.utils import get_user_model, get_username_field
11+
12+
User = get_user_model()
13+
14+
15+
class CommaSeparatedUserInput(widgets.Input):
16+
input_type = 'text'
17+
18+
def render(self, name, value, **kwargs):
19+
if value is None:
20+
value = ''
21+
elif isinstance(value, (list, tuple)):
22+
value = (', '.join([getattr(user, get_username_field()) for user in value]))
23+
return super(CommaSeparatedUserInput, self).render(name, value, **kwargs)
24+
25+
26+
27+
class CommaSeparatedUserField(forms.Field):
28+
widget = CommaSeparatedUserInput
29+
30+
def __init__(self, *args, **kwargs):
31+
recipient_filter = kwargs.pop('recipient_filter', None)
32+
self._recipient_filter = recipient_filter
33+
super(CommaSeparatedUserField, self).__init__(*args, **kwargs)
34+
35+
def clean(self, value):
36+
super(CommaSeparatedUserField, self).clean(value)
37+
if not value:
38+
return ''
39+
if isinstance(value, (list, tuple)):
40+
return value
41+
42+
names = set(value.split(','))
43+
names_set = set([name.strip() for name in names if name.strip()])
44+
users = list(User.objects.filter(**{'%s__in' % get_username_field(): names_set}))
45+
unknown_names = names_set ^ set([getattr(user, get_username_field()) for user in users])
46+
47+
recipient_filter = self._recipient_filter
48+
invalid_users = []
49+
if recipient_filter is not None:
50+
for r in users:
51+
if recipient_filter(r) is False:
52+
users.remove(r)
53+
invalid_users.append(getattr(r, get_username_field()))
54+
55+
if unknown_names or invalid_users:
56+
raise forms.ValidationError(_(u"The following usernames are incorrect: %(users)s") % {'users': ', '.join(list(unknown_names)+invalid_users)})
57+
58+
return users
59+
60+
def prepare_value(self, value):
61+
if value is None:
62+
value = ''
63+
elif isinstance(value, (list, tuple)):
64+
value = (', '.join([getattr(user, get_username_field()) for user in value]))
65+
return value

build/lib/django_messages/forms.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from django import forms
2+
from django.conf import settings
3+
from django.utils.translation import ugettext_lazy as _
4+
from django.utils import timezone
5+
6+
if "pinax.notifications" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
7+
from pinax.notifications import models as notification
8+
else:
9+
notification = None
10+
11+
from django_messages.models import Message
12+
from django_messages.fields import CommaSeparatedUserField
13+
14+
class ComposeForm(forms.Form):
15+
"""
16+
A simple default form for private messages.
17+
"""
18+
recipient = CommaSeparatedUserField(label=_(u"Recipient"))
19+
subject = forms.CharField(label=_(u"Subject"), max_length=140)
20+
body = forms.CharField(label=_(u"Body"),
21+
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))
22+
23+
24+
def __init__(self, *args, **kwargs):
25+
recipient_filter = kwargs.pop('recipient_filter', None)
26+
super(ComposeForm, self).__init__(*args, **kwargs)
27+
if recipient_filter is not None:
28+
self.fields['recipient']._recipient_filter = recipient_filter
29+
30+
31+
def save(self, sender, parent_msg=None):
32+
recipients = self.cleaned_data['recipient']
33+
subject = self.cleaned_data['subject']
34+
body = self.cleaned_data['body']
35+
message_list = []
36+
for r in recipients:
37+
msg = Message(
38+
sender = sender,
39+
recipient = r,
40+
subject = subject,
41+
body = body,
42+
)
43+
if parent_msg is not None:
44+
msg.parent_msg = parent_msg
45+
parent_msg.replied_at = timezone.now()
46+
parent_msg.save()
47+
msg.save()
48+
message_list.append(msg)
49+
if notification:
50+
if parent_msg is not None:
51+
notification.send([sender], "messages_replied", {'message': msg,})
52+
notification.send([r], "messages_reply_received", {'message': msg,})
53+
else:
54+
notification.send([sender], "messages_sent", {'message': msg,})
55+
notification.send([r], "messages_received", {'message': msg,})
56+
return message_list
Binary file not shown.

0 commit comments

Comments
 (0)