Skip to content

Commit b74ab2e

Browse files
committed
refactor commentbox_props() into classmethod to allow better overriding
1 parent c2669b4 commit b74ab2e

File tree

1 file changed

+122
-99
lines changed

1 file changed

+122
-99
lines changed

django_comments_xtd/api/frontend.py

Lines changed: 122 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -12,113 +12,136 @@
1212
)
1313

1414

15+
COMMENTS_FOR_CONCRETE_MODEL = getattr(settings, 'COMMENTS_FOR_CONCRETE_MODEL', True)
16+
17+
1518
XtdComment = get_comment_model()
1619

1720

18-
def commentbox_props(obj, user, request=None):
19-
"""
20-
Returns a JSON object with the initial props for the CommentBox component.
21+
class CommentBoxDriver(object):
22+
""" Class that allows overriding methods like get_queryset for CommentBox props function. """
23+
@classmethod
24+
def get_queryset(cls, ctype, obj, request):
25+
return XtdComment.objects.filter(
26+
content_type=ctype,
27+
object_pk=obj._get_pk_val(),
28+
site__pk=get_current_site_id(request),
29+
is_public=True,
30+
)
31+
32+
@classmethod
33+
def _reverse(cls, *args, **kwargs):
34+
"""do not inject request to avoid http:// urls on https:// only sites"""
35+
return reverse(*args, **kwargs)
36+
37+
@classmethod
38+
def get_props(cls, obj, user, request=None):
39+
"""
40+
Returns a JSON object with the initial props for the CommentBox component.
41+
42+
The returned JSON object contains the following attributes::
43+
{
44+
comment_count: <int>, // Count of comments posted to the object.
45+
allow_comments: <bool>, // Whether to allow comments to this post.
46+
current_user: <str as "user_id:user_name">,
47+
is_authenticated: <bool>, // Whether current_user is authenticated.
48+
request_name: <bool>, // True when auth user has no actual name.
49+
request_email_address: <bool>, // True when auth user has no email.
50+
allow_flagging: false,
51+
allow_feedback: false,
52+
show_feedback: false,
53+
can_moderate: <bool>, // Whether current_user can moderate.
54+
polling_interval: 2000, // Check for new comments every 2 seconds.
55+
feedback_url: <api-url-to-send-like/dislike-feedback>,
56+
delete_url: <api-url-for-moderators-to-remove-comment>,
57+
login_url: settings.LOGIN_URL,
58+
reply_url: <api-url-to-reply-comments>,
59+
flag_url: <api-url-to-suggest-comment-removal>,
60+
list_url: <api-url-to-list-comments>,
61+
count_url: <api-url-to-count-comments>,
62+
send_url: <api-url-to-send-a-comment>,
63+
preview_url: <api-url-to-preview-users-avatar>,
64+
form: {
65+
content_type: <value>,
66+
object_pk: <value>,
67+
timestamp: <value>,
68+
security_hash: <value>
69+
},
70+
login_url: <only_when_user_is_not_authenticated>,
71+
like_url: <only_when_user_is_not_authenticated>,
72+
dislike_url: <only_when_user_is_not_authenticated>,
73+
html_id_suffix: <html_element_id_suffix>
74+
}
75+
"""
2176

22-
The returned JSON object contains the following attributes::
23-
{
24-
comment_count: <int>, // Count of comments posted to the object.
25-
allow_comments: <bool>, // Whether to allow comments to this post.
26-
current_user: <str as "user_id:user_name">,
27-
is_authenticated: <bool>, // Whether current_user is authenticated.
28-
request_name: <bool>, // True when auth user has no actual name.
29-
request_email_address: <bool>, // True when auth user has no email.
30-
allow_flagging: false,
31-
allow_feedback: false,
32-
show_feedback: false,
33-
can_moderate: <bool>, // Whether current_user can moderate.
34-
polling_interval: 2000, // Check for new comments every 2 seconds.
35-
feedback_url: <api-url-to-send-like/dislike-feedback>,
36-
delete_url: <api-url-for-moderators-to-remove-comment>,
37-
login_url: settings.LOGIN_URL,
38-
reply_url: <api-url-to-reply-comments>,
39-
flag_url: <api-url-to-suggest-comment-removal>,
40-
list_url: <api-url-to-list-comments>,
41-
count_url: <api-url-to-count-comments>,
42-
send_url: <api-url-to-send-a-comment>,
43-
preview_url: <api-url-to-preview-users-avatar>,
44-
form: {
45-
content_type: <value>,
46-
object_pk: <value>,
47-
timestamp: <value>,
48-
security_hash: <value>
77+
form = CommentSecurityForm(obj)
78+
ctype = ContentType.objects.get_for_model(obj)
79+
queryset = cls.get_queryset(ctype, obj, request)
80+
ctype_slug = "%s-%s" % (ctype.app_label, ctype.model)
81+
ctype_key = "%s.%s" % (ctype.app_label, ctype.model)
82+
options = get_app_model_options(content_type=ctype_key)
83+
d = {
84+
"comment_count": queryset.count(),
85+
"allow_comments": True,
86+
"current_user": "0:Anonymous",
87+
"request_name": False,
88+
"request_email_address": False,
89+
"is_authenticated": False,
90+
"who_can_post": options['who_can_post'],
91+
"allow_flagging": False,
92+
"allow_feedback": False,
93+
"show_feedback": False,
94+
"can_moderate": False,
95+
"polling_interval": 2000,
96+
"feedback_url": cls._reverse("comments-xtd-api-feedback"),
97+
"delete_url": cls._reverse("comments-delete", args=(0,)),
98+
"reply_url": cls._reverse("comments-xtd-reply", kwargs={'cid': 0}),
99+
"flag_url": cls._reverse("comments-flag", args=(0,)),
100+
"list_url": cls._reverse('comments-xtd-api-list',
101+
kwargs={'content_type': ctype_slug,
102+
'object_pk': obj.id}),
103+
"count_url": cls._reverse('comments-xtd-api-count',
104+
kwargs={'content_type': ctype_slug,
105+
'object_pk': obj.id}),
106+
"send_url": cls._reverse("comments-xtd-api-create"),
107+
"preview_url": cls._reverse("comments-xtd-api-preview"),
108+
"form": {
109+
"content_type": form['content_type'].value(),
110+
"object_pk": form['object_pk'].value(),
111+
"timestamp": form['timestamp'].value(),
112+
"security_hash": form['security_hash'].value()
49113
},
50-
login_url: <only_when_user_is_not_authenticated>,
51-
like_url: <only_when_user_is_not_authenticated>,
52-
dislike_url: <only_when_user_is_not_authenticated>,
53-
html_id_suffix: <html_element_id_suffix>
114+
"default_followup": settings.COMMENTS_XTD_DEFAULT_FOLLOWUP,
115+
"html_id_suffix": get_html_id_suffix(obj),
116+
"max_thread_level": max_thread_level_for_content_type(ctype),
54117
}
55-
"""
118+
try:
119+
user_is_authenticated = user.is_authenticated()
120+
except TypeError: # Django >= 1.11
121+
user_is_authenticated = user.is_authenticated
122+
if user and user_is_authenticated:
123+
d['current_user'] = "%d:%s" % (
124+
user.pk, settings.COMMENTS_XTD_API_USER_REPR(user))
125+
d['is_authenticated'] = True
126+
d['can_moderate'] = user.has_perm("django_comments.can_moderate")
127+
d['request_name'] = True if not len(user.get_full_name()) else False
128+
d['request_email_address'] = True if not user.email else False
129+
else:
130+
d['login_url'] = settings.LOGIN_URL
131+
d['like_url'] = reverse("comments-xtd-like", args=(0,))
132+
d['dislike_url'] = reverse("comments-xtd-dislike", args=(0,))
56133

57-
def _reverse(*args, **kwargs):
58-
"""do not inject request to avoid http:// urls on https:// only sites"""
59-
return reverse(*args, **kwargs)
134+
return d
60135

61-
form = CommentSecurityForm(obj)
62-
ctype = ContentType.objects.get_for_model(obj)
63-
queryset = XtdComment.objects.filter(content_type=ctype,
64-
object_pk=obj.pk,
65-
site__pk=get_current_site_id(request),
66-
is_public=True)
67-
ctype_slug = "%s-%s" % (ctype.app_label, ctype.model)
68-
ctype_key = "%s.%s" % (ctype.app_label, ctype.model)
69-
options = get_app_model_options(content_type=ctype_key)
70-
d = {
71-
"comment_count": queryset.count(),
72-
"allow_comments": True,
73-
"current_user": "0:Anonymous",
74-
"request_name": False,
75-
"request_email_address": False,
76-
"is_authenticated": False,
77-
"who_can_post": options['who_can_post'],
78-
"allow_flagging": False,
79-
"allow_feedback": False,
80-
"show_feedback": False,
81-
"can_moderate": False,
82-
"polling_interval": 2000,
83-
"feedback_url": _reverse("comments-xtd-api-feedback"),
84-
"delete_url": _reverse("comments-delete", args=(0,)),
85-
"reply_url": _reverse("comments-xtd-reply", kwargs={'cid': 0}),
86-
"flag_url": _reverse("comments-flag", args=(0,)),
87-
"list_url": _reverse('comments-xtd-api-list',
88-
kwargs={'content_type': ctype_slug,
89-
'object_pk': obj.id}),
90-
"count_url": _reverse('comments-xtd-api-count',
91-
kwargs={'content_type': ctype_slug,
92-
'object_pk': obj.id}),
93-
"send_url": _reverse("comments-xtd-api-create"),
94-
"preview_url": _reverse("comments-xtd-api-preview"),
95-
"form": {
96-
"content_type": form['content_type'].value(),
97-
"object_pk": form['object_pk'].value(),
98-
"timestamp": form['timestamp'].value(),
99-
"security_hash": form['security_hash'].value()
100-
},
101-
"default_followup": settings.COMMENTS_XTD_DEFAULT_FOLLOWUP,
102-
"html_id_suffix": get_html_id_suffix(obj),
103-
"max_thread_level": max_thread_level_for_content_type(ctype),
104-
}
105-
try:
106-
user_is_authenticated = user.is_authenticated()
107-
except TypeError: # Django >= 1.11
108-
user_is_authenticated = user.is_authenticated
109-
if user and user_is_authenticated:
110-
d['current_user'] = "%d:%s" % (
111-
user.pk, settings.COMMENTS_XTD_API_USER_REPR(user))
112-
d['is_authenticated'] = True
113-
d['can_moderate'] = user.has_perm("django_comments.can_moderate")
114-
d['request_name'] = True if not len(user.get_full_name()) else False
115-
d['request_email_address'] = True if not user.email else False
116-
else:
117-
d['login_url'] = settings.LOGIN_URL
118-
d['like_url'] = reverse("comments-xtd-like", args=(0,))
119-
d['dislike_url'] = reverse("comments-xtd-dislike", args=(0,))
120-
121-
return d
136+
137+
def commentbox_props(obj, user, request=None):
138+
"""
139+
Returns a JSON object with the initial props for the CommentBox component.
140+
141+
This is transitional function left for backword compatibility with applications
142+
that are using commentbox_props() function directly.
143+
"""
144+
return CommentBoxDriver.get_props(obj, user, request)
122145

123146

124147
def commentbox_props_response(obj, user, request):

0 commit comments

Comments
 (0)