-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmodels.py
104 lines (84 loc) · 3.48 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from django.conf import settings
from django.contrib import comments
from django.contrib.comments import Comment
from django.contrib.comments.managers import CommentManager
from django.contrib.contenttypes.generic import GenericRelation
from django.contrib.sites.models import get_current_site
from django.core.mail import send_mail
from django.dispatch import receiver
from django.contrib.comments import signals
from django.template import RequestContext
from django.template.loader import render_to_string
from fluent_comments import appsettings
class FluentCommentManager(CommentManager):
"""
Manager to optimize SQL queries for comments.
"""
def get_query_set(self):
return super(CommentManager, self).get_query_set().select_related('user')
class FluentComment(Comment):
"""
Proxy model to make sure that a ``select_related()`` is performed on the ``user`` field.
"""
objects = FluentCommentManager()
class Meta:
proxy = True
app_label = 'comments'
@receiver(signals.comment_was_posted)
def on_comment_posted(sender, comment, request, **kwargs):
"""
Send email notification of a new comment to site staff when email notifications have been requested.
"""
# This code is copied from django.contrib.comments.moderation.
# That code doesn't offer a RequestContext, which makes it really
# hard to generate proper URL's with FQDN in the email
#
# Instead of implementing this feature in the moderator class, the signal is used instead
# so the notification feature works regardless of a manual moderator.register() call in the project.
if not appsettings.FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION:
return
recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
site = get_current_site(request)
content_object = comment.content_object
subject = '[{0}] New comment posted on "{1}"'.format(site.name, content_object)
context = {
'site': site,
'comment': comment,
'content_object': content_object
}
message = render_to_string("comments/comment_notification_email.txt", context, RequestContext(request))
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
def get_comments_for_model(content_object, include_moderated=False):
"""
Return the QuerySet with all comments for a given model.
"""
qs = comments.get_model().objects.for_model(content_object)
if not include_moderated:
qs = qs.filter(is_public=True, is_removed=False)
return qs
class CommentsRelation(GenericRelation):
"""
A :class:`~django.contrib.contenttypes.generic.GenericRelation` which can be applied to a parent model that
is expected to have comments. For example:
.. code-block:: python
class Article(models.Model):
comments_set = CommentsRelation()
"""
def __init__(self, *args, **kwargs):
super(CommentsRelation, self).__init__(
to=comments.get_model(),
content_type_field='content_type',
object_id_field='object_pk',
**kwargs
)
try:
from south.modelsinspector import add_ignored_fields
except ImportError:
pass
else:
# South 0.7.x ignores GenericRelation fields but doesn't ignore subclasses.
# Taking the same fix as applied in http://south.aeracode.org/ticket/414
_name_re = "^" + __name__.replace(".", "\.")
add_ignored_fields((
_name_re + "\.CommentsRelation",
))