Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update models.py #147

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions django_messages/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _

class DjangoMessagesConfig(AppConfig):
name = 'django_messages'
verbose_name = _('Messages')
verbose_name = 'Messages'
3 changes: 1 addition & 2 deletions django_messages/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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

Expand Down Expand Up @@ -53,7 +52,7 @@ def clean(self, value):
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)})
raise forms.ValidationError("The following usernames are incorrect: %(users)s" % {'users': ', '.join(list(unknown_names)+invalid_users)})

return users

Expand Down
7 changes: 3 additions & 4 deletions django_messages/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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):
Expand All @@ -15,9 +14,9 @@ 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"),
recipient = CommaSeparatedUserField(label="Recipient")
subject = forms.CharField(label="Subject", max_length=140)
body = forms.CharField(label="Body",
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))


Expand Down
13 changes: 6 additions & 7 deletions django_messages/management.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from django.db.models import signals
from django.conf import settings
from django.utils.translation import ugettext_noop as _

if "pinax.notifications" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
from pinax.notifications import models as notification

def create_notice_types(app, created_models, verbosity, **kwargs):
notification.create_notice_type("messages_received", _("Message Received"), _("you have received a message"), default=2)
notification.create_notice_type("messages_sent", _("Message Sent"), _("you have sent a message"), default=1)
notification.create_notice_type("messages_replied", _("Message Replied"), _("you have replied to a message"), default=1)
notification.create_notice_type("messages_reply_received", _("Reply Received"), _("you have received a reply to a message"), default=2)
notification.create_notice_type("messages_deleted", _("Message Deleted"), _("you have deleted a message"), default=1)
notification.create_notice_type("messages_recovered", _("Message Recovered"), _("you have undeleted a message"), default=1)
notification.create_notice_type("messages_received", "Message Received", "you have received a message", default=2)
notification.create_notice_type("messages_sent", "Message Sent", "you have sent a message", default=1)
notification.create_notice_type("messages_replied", "Message Replied", "you have replied to a message", default=1)
notification.create_notice_type("messages_reply_received", "Reply Received", "you have received a reply to a message", default=2)
notification.create_notice_type("messages_deleted", "Message Deleted", "you have deleted a message", default=1)
notification.create_notice_type("messages_recovered", "Message Recovered", "you have undeleted a message", default=1)

signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
Expand Down
27 changes: 12 additions & 15 deletions django_messages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from django.db import models
from django.db.models import signals
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')

Expand Down Expand Up @@ -48,21 +46,20 @@ def trash_for(self, user):
)


@python_2_unicode_compatible
class Message(models.Model):
"""
A private message from user to user
"""
subject = models.CharField(_("Subject"), max_length=140)
body = models.TextField(_("Body"))
sender = models.ForeignKey(AUTH_USER_MODEL, related_name='sent_messages', verbose_name=_("Sender"), on_delete=models.PROTECT)
recipient = models.ForeignKey(AUTH_USER_MODEL, related_name='received_messages', null=True, blank=True, verbose_name=_("Recipient"), on_delete=models.SET_NULL)
parent_msg = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbose_name=_("Parent message"), on_delete=models.SET_NULL)
sent_at = models.DateTimeField(_("sent at"), null=True, blank=True)
read_at = models.DateTimeField(_("read at"), null=True, blank=True)
replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
sender_deleted_at = models.DateTimeField(_("Sender deleted at"), null=True, blank=True)
recipient_deleted_at = models.DateTimeField(_("Recipient deleted at"), null=True, blank=True)
subject = models.CharField("Subject", max_length=140)
body = models.TextField("Body")
sender = models.ForeignKey(AUTH_USER_MODEL, related_name='sent_messages', verbose_name="Sender", on_delete=models.PROTECT)
recipient = models.ForeignKey(AUTH_USER_MODEL, related_name='received_messages', null=True, blank=True, verbose_name="Recipient", on_delete=models.SET_NULL)
parent_msg = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbose_name="Parent message", on_delete=models.SET_NULL)
sent_at = models.DateTimeField("sent at", null=True, blank=True)
read_at = models.DateTimeField("read at", null=True, blank=True)
replied_at = models.DateTimeField("replied at", null=True, blank=True)
sender_deleted_at = models.DateTimeField("Sender deleted at", null=True, blank=True)
recipient_deleted_at = models.DateTimeField("Recipient deleted at", null=True, blank=True)

objects = MessageManager()

Expand Down Expand Up @@ -91,8 +88,8 @@ def save(self, **kwargs):

class Meta:
ordering = ['-sent_at']
verbose_name = _("Message")
verbose_name_plural = _("Messages")
verbose_name = "Message"
verbose_name_plural = "Messages"


def inbox_count_for(user):
Expand Down
23 changes: 11 additions & 12 deletions django_messages/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from django.conf.urls import url
from django.views.generic import RedirectView

from django.urls import include, path
from django_messages.views import *

urlpatterns = [
url(r'^$', RedirectView.as_view(permanent=True, url='inbox/'), name='messages_redirect'),
url(r'^inbox/$', inbox, name='messages_inbox'),
url(r'^outbox/$', outbox, name='messages_outbox'),
url(r'^compose/$', compose, name='messages_compose'),
url(r'^compose/(?P<recipient>[\w.@+-]+)/$', compose, name='messages_compose_to'),
url(r'^reply/(?P<message_id>[\d]+)/$', reply, name='messages_reply'),
url(r'^view/(?P<message_id>[\d]+)/$', view, name='messages_detail'),
url(r'^delete/(?P<message_id>[\d]+)/$', delete, name='messages_delete'),
url(r'^undelete/(?P<message_id>[\d]+)/$', undelete, name='messages_undelete'),
url(r'^trash/$', trash, name='messages_trash'),
path('', RedirectView.as_view(permanent=True, url='inbox/'), name='messages_redirect'),
path('inbox/', inbox, name='messages_inbox'),
path('outbox/', outbox, name='messages_outbox'),
path('compose/', compose, name='messages_compose'),
path('compose/(?P<recipient>[\w.@+-]+)/', compose, name='messages_compose_to'),
path('reply/(?P<message_id>[\d]+)/', reply, name='messages_reply'),
path('view/(?P<message_id>[\d]+)/', view, name='messages_detail'),
path('delete/(?P<message_id>[\d]+)/', delete, name='messages_delete'),
path('undelete/(?P<message_id>[\d]+)/', undelete, name='messages_undelete'),
path('trash/$', trash, name='messages_trash'),
]
3 changes: 1 addition & 2 deletions django_messages/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
import django
from django.utils.text import wrap
from django.utils.translation import ugettext, ugettext_lazy as _
from django.template.loader import render_to_string
from django.conf import settings

Expand Down Expand Up @@ -56,7 +55,7 @@ def format_subject(subject):
}

def new_message_email(sender, instance, signal,
subject_prefix=_(u'New Message: %(subject)s'),
subject_prefix=u'New Message: %(subject)s',
template_name="django_messages/new_message.html",
default_protocol=None,
*args, **kwargs):
Expand Down
13 changes: 6 additions & 7 deletions django_messages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.template import RequestContext
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext as _
from django.utils import timezone
try:
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -86,7 +85,7 @@ def compose(request, recipient=None, form_class=ComposeForm,
form = form_class(request.POST, recipient_filter=recipient_filter)
if form.is_valid():
form.save(sender=request.user)
messages.info(request, _(u"Message successfully sent."))
messages.info(request, "Message successfully sent.")
if success_url is None:
success_url = reverse('messages_inbox')
if 'next' in request.GET:
Expand All @@ -105,7 +104,7 @@ def compose(request, recipient=None, form_class=ComposeForm,
def reply(request, message_id, form_class=ComposeForm,
template_name='django_messages/compose.html', success_url=None,
recipient_filter=None, quote_helper=format_quote,
subject_template=_(u"Re: %(subject)s"),):
subject_template="Re: %(subject)s",):
"""
Prepares the ``form_class`` form for writing a reply to a given message
(specified via ``message_id``). Uses the ``format_quote`` helper from
Expand All @@ -123,7 +122,7 @@ def reply(request, message_id, form_class=ComposeForm,
form = form_class(request.POST, recipient_filter=recipient_filter)
if form.is_valid():
form.save(sender=request.user, parent_msg=parent)
messages.info(request, _(u"Message successfully sent."))
messages.info(request, "Message successfully sent.")
if success_url is None:
success_url = reverse('messages_inbox')
return HttpResponseRedirect(success_url)
Expand Down Expand Up @@ -166,7 +165,7 @@ def delete(request, message_id, success_url=None):
deleted = True
if deleted:
message.save()
messages.info(request, _(u"Message successfully deleted."))
messages.info(request, "Message successfully deleted.")
if notification:
notification.send([user], "messages_deleted", {'message': message,})
return HttpResponseRedirect(success_url)
Expand All @@ -193,15 +192,15 @@ def undelete(request, message_id, success_url=None):
undeleted = True
if undeleted:
message.save()
messages.info(request, _(u"Message successfully recovered."))
messages.info(request, "Message successfully recovered.")
if notification:
notification.send([user], "messages_recovered", {'message': message,})
return HttpResponseRedirect(success_url)
raise Http404

@login_required
def view(request, message_id, form_class=ComposeForm, quote_helper=format_quote,
subject_template=_(u"Re: %(subject)s"),
subject_template="Re: %(subject)s",
template_name='django_messages/view.html'):
"""
Shows a single message.``message_id`` argument is required.
Expand Down