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

New templatetags, migrations, signals and purging emails #42

Open
wants to merge 15 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: 2 additions & 1 deletion django_messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
VERSION = (0, 5, 1,)
__version__ = '.'.join(map(str, VERSION))
default_app_config = 'django_messages.apps.DjangoMessagesConfig'
default_app_config = 'django_messages.apps.DjangoMessagesConfig'
from . import signals
11 changes: 6 additions & 5 deletions django_messages/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from notification import models as notification
else:
notification = None

from django_messages.models import Message

class MessageAdminForm(forms.ModelForm):
Expand Down Expand Up @@ -75,7 +75,7 @@ def save_model(self, request, obj, form, change):
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:
Expand All @@ -84,7 +84,7 @@ def save_model(self, request, obj, form, change):
else:
sender_label = 'messages_replied'
recipients_label = 'messages_reply_received'

# Notification for the sender.
notification.send([obj.sender], sender_label, {'message': obj,})

Expand All @@ -108,5 +108,6 @@ def save_model(self, request, obj, form, change):
if notification:
# Notification for the recipient.
notification.send([user], recipients_label, {'message' : obj,})

admin.site.register(Message, MessageAdmin)

if getattr(settings, 'DJANGO_MESSAGES_ADMIN_PANEL', True):
admin.site.register(Message, MessageAdmin)
40 changes: 16 additions & 24 deletions django_messages/forms.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone

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

from django_messages.models import Message
from django_messages.fields import CommaSeparatedUserField
from django_messages import signals


class ComposeForm(forms.Form):

"""
A simple default form for private messages.
"""
recipient = CommaSeparatedUserField(label=_(u"Recipient"))
subject = forms.CharField(label=_(u"Subject"), max_length=120)
body = forms.CharField(label=_(u"Body"),
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))


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,
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()
msg.save()
signals.message_repled.send(sender=ComposeForm, message=msg, user=sender)
else:
msg.save()
signals.message_sent.send(sender=ComposeForm, message=msg, user=sender)

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
2 changes: 2 additions & 0 deletions django_messages/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def create_notice_types(app, created_models, verbosity, **kwargs):
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_marked_unread", _("Message Marked As Unread"), _("you have marked a message as unread"), default=1)
notification.create_notice_type("messages_purged", _("Message Purged"), _("you have purged a message"), default=1)

signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
Expand Down
37 changes: 37 additions & 0 deletions django_messages/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Message',
fields=[
('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)),
('subject', models.CharField(max_length=120, verbose_name='Subject')),
('body', models.TextField(verbose_name='Body')),
('sent_at', models.DateTimeField(null=True, verbose_name='sent at', blank=True)),
('read_at', models.DateTimeField(null=True, verbose_name='read at', blank=True)),
('replied_at', models.DateTimeField(null=True, verbose_name='replied at', blank=True)),
('sender_deleted_at', models.DateTimeField(null=True, verbose_name='Sender deleted at', blank=True)),
('recipient_deleted_at', models.DateTimeField(null=True, verbose_name='Recipient deleted at', blank=True)),
('parent_msg', models.ForeignKey(null=True, to='django_messages.Message', verbose_name='Parent message', blank=True, related_name='next_messages')),
('recipient', models.ForeignKey(null=True, to=settings.AUTH_USER_MODEL, verbose_name='Recipient', blank=True, related_name='received_messages')),
('sender', models.ForeignKey(to=settings.AUTH_USER_MODEL, verbose_name='Sender', related_name='sent_messages')),
],
options={
'ordering': ['-sent_at'],
'verbose_name_plural': 'Messages',
'verbose_name': 'Message',
},
bases=(models.Model,),
),
]
26 changes: 26 additions & 0 deletions django_messages/migrations/0002_auto_20140926_1746.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('django_messages', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='message',
name='purged_for_recipient',
field=models.BooleanField(db_index=True, verbose_name='Purged for recipient', default=False),
preserve_default=True,
),
migrations.AddField(
model_name='message',
name='purged_for_sender',
field=models.BooleanField(db_index=True, verbose_name='Purged for sender', default=False),
preserve_default=True,
),
]
Empty file.
11 changes: 5 additions & 6 deletions django_messages/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.conf import settings
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 _
Expand Down Expand Up @@ -38,14 +37,17 @@ def trash_for(self, user):
return self.filter(
recipient=user,
recipient_deleted_at__isnull=False,
purged_for_recipient=False,
) | self.filter(
sender=user,
sender_deleted_at__isnull=False,
purged_for_sender=False,
)


@python_2_unicode_compatible
class Message(models.Model):

"""
A private message from user to user
"""
Expand All @@ -59,6 +61,8 @@ class Message(models.Model):
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)
purged_for_sender = models.BooleanField(_("Purged for sender"), default=False, db_index=True)
purged_for_recipient = models.BooleanField(_("Purged for recipient"), default=False, db_index=True)

objects = MessageManager()

Expand Down Expand Up @@ -98,8 +102,3 @@ def inbox_count_for(user):
mark them seen
"""
return Message.objects.filter(recipient=user, read_at__isnull=True, recipient_deleted_at__isnull=True).count()

# fallback for email notification if django-notification could not be found
if "notification" not in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
from django_messages.utils import new_message_email
signals.post_save.connect(new_message_email, sender=Message)
64 changes: 64 additions & 0 deletions django_messages/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from django.conf import settings
from django.dispatch import Signal

message_deleted = Signal(providing_args=["message", "user"])
message_sent = Signal(providing_args=["message", "user"])
message_repled = Signal(providing_args=["message", "user"])
mesage_recovered = Signal(providing_args=["message", "user"])
message_marked_as_unread = Signal(providing_args=["message", "user"])
message_purge = Signal(providing_args=["message", "user"])

try:
#If it's during installation, we should configure the settings otherwise it fails
settings.configure()
except RuntimeError:
# Already configured (installation is complete)
pass

if "notification" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
from notification import models as notification
from django_messages.forms import ComposeForm
from django_messages.views import delete, undelete, unread, purge

def sent_notification(sender, **kwargs):
msg = kwargs['message']
notification.send([msg.sender], "messages_sent", {'message': msg})
notification.send([msg.recipient], "messages_received", {'message': msg})

def replied_notification(sender, **kwargs):
msg = kwargs['message']
notification.send([msg.sender], "messages_replied", {'message': msg})
notification.send([msg.recipient], "messages_reply_received", {'message': msg})

def deleted_notification(sender, **kwargs):
msg = kwargs['message']
user = kwargs['user']
notification.send([user], "messages_deleted", {'message': msg})

def recovered_notification(sender, **kwargs):
msg = kwargs['message']
user = kwargs['user']
notification.send([user], "messages_recovered", {'message': msg})

def unread_notification(sender, **kwargs):
msg = kwargs['message']
user = kwargs['user']
notification.send([user], "messages_marked_unread", {'message': msg})

def purge_notification(sender, **kwargs):
msg = kwargs['message']
user = kwargs['user']
notification.send([user], "messages_purged", {'message': msg})

message_deleted.connect(deleted_notification, sender=delete)
message_sent.connect(sent_notification, sender=ComposeForm)
message_repled.connect(replied_notification, sender=ComposeForm)
mesage_recovered.connect(recovered_notification, sender=undelete)
message_marked_as_unread.connect(unread_notification, sender=unread)
message_purge.connect(purge_notification, sender=purge)

# fallback for email notification if django-notification could not be found
from django_messages.utils import new_message_email
from django.db.models import signals
from django_messages.models import Message
signals.post_save.connect(new_message_email, sender=Message)
17 changes: 9 additions & 8 deletions django_messages/templates/django_messages/trash.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
{% extends "django_messages/base.html" %}
{% load i18n %}
{% extends "django_messages/base.html" %}
{% load i18n %}
{% load url from future %}

{% block content %}
{% block content %}
<h1>{% trans "Deleted Messages" %}</h1>
{% if message_list %}
{% if message_list %}
<table class="messages">
<thead>
<tr><th>{% trans "Sender" %}</th><th>{% trans "Subject" %}</th><th>{% trans "Date" %}</th><th>{% trans "Action" %}</th></tr>
</thead>
<tbody>
{% for message in message_list %}
{% for message in message_list %}
<tr>
<td>{{ message.sender }}</td>
<td>
<td>
{{ message.subject }}
</td>
<td>{{ message.sent_at|date:_("DATETIME_FORMAT") }}</td>
<td><a href="{% url 'messages_undelete' message.id %}">{% trans "undelete" %}</a></td>
<td><a href="{% url 'messages_purge' message.id %}">{% trans "purge" %}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>{% trans "No messages." %}</p>
{% endif %}
{% endif %}
<br />
<p>{% trans "Deleted Messages are removed from the trash at unregular intervals, don't rely on this feature for long-time storage." %}</p>
{% endblock %}
{% endblock %}
3 changes: 2 additions & 1 deletion django_messages/templates/django_messages/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h1>{% trans "View Message" %}</h1>
<a href="{% url 'messages_reply' message.id %}">{% trans "Reply" %}</a>
{% endifequal %}
<a href="{% url 'messages_delete' message.id %}">{% trans "Delete" %}</a>
<a href="{% url 'messages_mark_unread' message.id %}">{% trans "Mark Unread" %}</a>

{% comment %}Example reply_form integration
{% if reply_form %}
Expand All @@ -33,4 +34,4 @@ <h1>{% trans "Compose reply"%}</h1>
</form>
{% endif %}
{% endcomment %}
{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% load i18n %}{% blocktrans %}You have marked the message as unread '{{ message }}'.{% endblocktrans %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% load i18n %}{% blocktrans with message.get_absolute_url as message_url %}You have marked the message as unread <a href="{{ message_url }}">{{ message }}</a>.{% endblocktrans %}
Loading