forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
97 lines (79 loc) · 3.92 KB
/
Copy pathmodels.py
File metadata and controls
97 lines (79 loc) · 3.92 KB
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
# ------------------------------------------------------------------------
# coding=utf-8
# ------------------------------------------------------------------------
#
# Created by Martin J. Laubach on 08.01.10.
# skyl wuz here (11.05.10)
#
# ------------------------------------------------------------------------
"""
Embed a comment list and comment form anywhere. Uses the standard
``django.contrib.comments`` application.
"""
from django.contrib import comments
from django.db import models
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
# ------------------------------------------------------------------------
class CommentsContent(models.Model):
comments_enabled = models.BooleanField(_('enabled'), default=True, help_text=_('New comments may be added'))
class Meta:
abstract = True
verbose_name = _('comments')
verbose_name_plural = _('comments')
@classmethod
def initialize_type(cls):
from feincms.admin.item_editor import ItemEditorForm
class CommentContentAdminForm(ItemEditorForm):
def __init__(self, *args, **kwargs):
super(CommentContentAdminForm, self).__init__(*args, **kwargs)
parent = kwargs.get('instance', None)
if parent is not None:
f = self.fields['comments_enabled']
r = f.help_text
r += u'<hr />'
comments_model = comments.get_model()
for c in comments_model.objects.for_model(parent.parent).order_by('-submit_date'):
r += '<div class="form-row" style="margin-left: 60px"># %(pk)d <a href="/admin/%(app)s/%(model)s/%(pk)d/">%(comment)s</a> - %(is_public)s</div>' % \
{
'pk': c.id,
'comment': c.comment[:80],
'is_public': c.is_public and _('public') or _('not public'),
'app': comments_model._meta.app_label,
'model': comments_model._meta.module_name
}
f.help_text = r
cls.feincms_item_editor_form = CommentContentAdminForm
def process(self, request, **kwargs):
parent_type = self.parent.__class__.__name__.lower()
comment_page = self.parent
if hasattr(comment_page, 'original_translation') and comment_page.original_translation:
comment_page = comment_page.original_translation
f = None
if self.comments_enabled and request.POST:
# I guess the drawback is that this page can't handle any other types of posts
# just the comments for right now, but if we just post to the current path
# and handle it this way .. at least it works for now.
#extra = request._feincms_extra_context.get('page_extra_path', ())
#if len(extra) > 0 and extra[0] == u"post-comment":
from django.contrib.comments.views.comments import post_comment
r = post_comment(request, next=comment_page.get_absolute_url())
if isinstance(r, HttpResponseRedirect):
return r
f = comments.get_form()(comment_page, data=request.POST)
if f is None:
f = comments.get_form()(comment_page)
self.rendered_output = render_to_string([
'content/comments/%s.html' % parent_type,
'content/comments/default-site.html',
'content/comments/default.html',
], RequestContext(request, {
'content': self,
'feincms_page': self.parent,
'parent': comment_page,
'form': f,
}))
def render(self, **kwargs):
return getattr(self, 'rendered_output', u'')