Skip to content

Commit 7653462

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

File tree

1 file changed

+132
-98
lines changed

1 file changed

+132
-98
lines changed

django_comments_xtd/api/frontend.py

Lines changed: 132 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,110 +15,144 @@
1515
XtdComment = get_comment_model()
1616

1717

18-
def commentbox_props(obj, user, request=None):
18+
class CommentBoxDriver(object):
1919
"""
20-
Returns a JSON object with the initial props for the CommentBox component.
20+
Class that allows overriding methods like get_queryset()
21+
for CommentBox props function.
22+
"""
23+
@classmethod
24+
def get_queryset(cls, ctype, obj, request):
25+
return XtdComment.objects.filter(
26+
content_type=ctype,
27+
object_pk=obj.pk,
28+
site__pk=get_current_site_id(request),
29+
is_public=True,
30+
)
31+
32+
@classmethod
33+
def _reverse(cls, *args, **kwargs):
34+
"""
35+
do not inject request to avoid http:// urls on https:// only sites
36+
"""
37+
return reverse(*args, **kwargs)
38+
39+
@classmethod
40+
def get_props(cls, obj, user, request=None):
41+
"""
42+
Returns a JSON object with the initial props for
43+
the CommentBox component.
2144
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>
45+
The returned JSON object contains the following attributes::
46+
{
47+
comment_count: <int>, // Count of comments posted
48+
// to the object.
49+
allow_comments: <bool>, // Whether to allow comments
50+
// to this post.
51+
current_user: <str as "user_id:user_name">,
52+
is_authenticated: <bool>, // Whether current_user
53+
// is authenticated.
54+
request_name: <bool>, // True when auth user has
55+
// no actual name.
56+
request_email_address: <bool>, // True when auth user
57+
// has no email.
58+
allow_flagging: false,
59+
allow_feedback: false,
60+
show_feedback: false,
61+
can_moderate: <bool>, // Whether current_user can moderate.
62+
polling_interval: 2000, // Check for new comments
63+
// every 2 seconds.
64+
feedback_url: <api-url-to-send-like/dislike-feedback>,
65+
delete_url: <api-url-for-moderators-to-remove-comment>,
66+
login_url: settings.LOGIN_URL,
67+
reply_url: <api-url-to-reply-comments>,
68+
flag_url: <api-url-to-suggest-comment-removal>,
69+
list_url: <api-url-to-list-comments>,
70+
count_url: <api-url-to-count-comments>,
71+
send_url: <api-url-to-send-a-comment>,
72+
preview_url: <api-url-to-preview-users-avatar>,
73+
form: {
74+
content_type: <value>,
75+
object_pk: <value>,
76+
timestamp: <value>,
77+
security_hash: <value>
78+
},
79+
login_url: <only_when_user_is_not_authenticated>,
80+
like_url: <only_when_user_is_not_authenticated>,
81+
dislike_url: <only_when_user_is_not_authenticated>,
82+
html_id_suffix: <html_element_id_suffix>
83+
}
84+
"""
85+
86+
form = CommentSecurityForm(obj)
87+
ctype = ContentType.objects.get_for_model(obj)
88+
queryset = cls.get_queryset(ctype, obj, request)
89+
ctype_slug = "%s-%s" % (ctype.app_label, ctype.model)
90+
ctype_key = "%s.%s" % (ctype.app_label, ctype.model)
91+
options = get_app_model_options(content_type=ctype_key)
92+
d = {
93+
"comment_count": queryset.count(),
94+
"allow_comments": True,
95+
"current_user": "0:Anonymous",
96+
"request_name": False,
97+
"request_email_address": False,
98+
"is_authenticated": False,
99+
"who_can_post": options['who_can_post'],
100+
"allow_flagging": False,
101+
"allow_feedback": False,
102+
"show_feedback": False,
103+
"can_moderate": False,
104+
"polling_interval": 2000,
105+
"feedback_url": cls._reverse("comments-xtd-api-feedback"),
106+
"delete_url": cls._reverse("comments-delete", args=(0,)),
107+
"reply_url": cls._reverse("comments-xtd-reply", kwargs={'cid': 0}),
108+
"flag_url": cls._reverse("comments-flag", args=(0,)),
109+
"list_url": cls._reverse('comments-xtd-api-list',
110+
kwargs={'content_type': ctype_slug,
111+
'object_pk': obj.id}),
112+
"count_url": cls._reverse('comments-xtd-api-count',
113+
kwargs={'content_type': ctype_slug,
114+
'object_pk': obj.id}),
115+
"send_url": cls._reverse("comments-xtd-api-create"),
116+
"preview_url": cls._reverse("comments-xtd-api-preview"),
117+
"form": {
118+
"content_type": form['content_type'].value(),
119+
"object_pk": form['object_pk'].value(),
120+
"timestamp": form['timestamp'].value(),
121+
"security_hash": form['security_hash'].value()
49122
},
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>
123+
"default_followup": settings.COMMENTS_XTD_DEFAULT_FOLLOWUP,
124+
"html_id_suffix": get_html_id_suffix(obj),
125+
"max_thread_level": max_thread_level_for_content_type(ctype),
54126
}
55-
"""
127+
try:
128+
user_is_authenticated = user.is_authenticated()
129+
except TypeError: # Django >= 1.11
130+
user_is_authenticated = user.is_authenticated
131+
if user and user_is_authenticated:
132+
d['current_user'] = "%d:%s" % (
133+
user.pk, settings.COMMENTS_XTD_API_USER_REPR(user))
134+
d['is_authenticated'] = True
135+
d['can_moderate'] = user.has_perm("django_comments.can_moderate")
136+
d['request_name'] = (
137+
True if not len(user.get_full_name()) else False
138+
)
139+
d['request_email_address'] = True if not user.email else False
140+
else:
141+
d['login_url'] = settings.LOGIN_URL
142+
d['like_url'] = reverse("comments-xtd-like", args=(0,))
143+
d['dislike_url'] = reverse("comments-xtd-dislike", args=(0,))
144+
145+
return d
56146

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

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
148+
def commentbox_props(obj, user, request=None):
149+
"""
150+
Returns a JSON object with the initial props for the CommentBox component.
151+
152+
This is transitional function left for backward compatibility
153+
with applications that are using commentbox_props() function directly.
154+
"""
155+
return CommentBoxDriver.get_props(obj, user, request)
122156

123157

124158
def commentbox_props_response(obj, user, request):

0 commit comments

Comments
 (0)