-
Notifications
You must be signed in to change notification settings - Fork 42
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
top comments based on likes #83
Comments
For getting the top comments based on likes, you may use this function: from comment.models import Comment
from post.models import Post
# whatever model you associate comments with, just get an instance of the model here
post = Post.objects.first()
Comment.objects.filter_comments_by_object(post).order_by('-reaction__likes') Most likely, you would want to sort the parent comments based on likes, for that you may use this instead Comment.objects.filter_comments_by_parent(post).order_by('-reaction__likes') This feature is in the pipeline but hasn't been implemented yet. Most likely, we will try to incorporate it in one of the future releases. |
This is my views entry for top comments which I pass through as context.
|
Sorry i wrote the wrong functions, the correct ones are: # sort all comments
Comment.objects.all_comments_by_object(post).order_by('-reaction__likes')
# sort all parent comments
Comment.objects.filter_parents_by_object(post).order_by('-reaction__likes') |
Hey @Radi85, I was thinking of implementing this feature. It would be great to have the option to render comments in order of reaction(likes, dislikes etc) as well as the same in a reverse order. The reverse order thing can also be added with the present way in which we render comments. Any thoughts from your side would be welcome. |
Hey @abhiabhi94, This would be a nice feature. Would you create a setting variable to let the developers choose their desired ordering? |
This shouldn't require too much of work so 2.6 should be okay.
A setting variable is not a great option in my view. This might hinder the user if they want different order for comment on different objects. The raw idea that I have right now in my mind as of now, how the template tags have been written, I think we would require some sort of change or add some template tags, because ordering based upon reactions(like, dislike) would be a bit different from order based on replies or date. |
You may add an optional argument |
ordering by the number of replies will be a bit different from ordering by likes, also because we can't just directly add the argument to the order_by function from django. Ordering with likes Comment.objects.filter_comments_by_object(post).order_by('-reaction__likes') Ordering with replies # will have to add a property reply_count and see if this works or have to use annotation
Comment.objects.filter_comments_by_object(post).order_by('-reply_count') |
This can be simply documented, so I can pass order_by = ['-reaction__likes', '-reply_count', '-posted'] |
Hey @Radi85, I think we can add another setting attribute so that this can be applied globally, if required by the developer. We can probably pass their order to the Meta options inside the comment model. As of now, I haven't though of any pitfalls or issues that this may cause anything else in the codebase. Any thoughts from your side would be great. Also, may be at a later stage we can probably think of providing a dropdown inside the templates to sort the comment based upon any of the available parameters. |
Yes. This is how it should be.
The issue that can happen is passing a wrong field name. We might test the field names before passing them to meta class or just simply leave the app raising the default error.
This would be a nice feature. |
Yes, I think that would be necessary. In addition to these, I think we should add similar checks for other setting variables or would that fall into the YAGNI(you aren't going to need it) category? |
In fact I would prefer to not check any of settings values provided by the developer, let the app crach and notify the developer. Bcz if I fallback to the default value, that might be unnoticeable and give unexpected result. Moreover, most of the settings values are boolean and checking them is really YAGNI. |
Seems legit. My only point of concern now is every change in this setting would create a new migration. Would that be a good design strategy? We can probably bypass this issue by overriding the default queryset by this setting value. What do you think about this? |
I just realized that ordering is part of the migration, good you mentioned that 😅 . Of course this will be shitty design to change the migration when the user preference changes. DB (tables structure) should not be changed at all based on the user preference.
Yes we can extend the default queryset with the provided ordering. |
As far as ordering by An alternative to this would be something along the lines: import functools
def get_queryset(self):
@functools.lru_cache(maxsize=None)
def is_reverse(order):
if order[0] == '-':
return True
return False
order = settings.COMMENT_ORDER_BY
# ignore this method for now, it just handles the validation part
validate_order(order)
qs = super().get_queryset()
if order.find('replies') < -1:
return qs.order_by(order)
return sorted(qs, key=lambda a: a.replies().count(), reverse=is_reverse(order)) The above can't be used because it returns a list in case of In case, @Radi85 you have any suggestions to a way around this issue, it would be great to know them. |
Filtering by replies seems weird for me. We are currently using comment thread with one level of replies, how this can be controlled/calcualted if we want to go more deeper?! I would suggest to not implement ordering by replies for now and in case we recieve more request on it, we can then implement it and adjust the list views that uses the queryset. |
Just to be on the same page, replies here stands for reply count. Sorting by reply count can be one of the ways to present more "popular" or "discussed" conversations first. This would only be done for parent comment as of now. This assumption seems reasonable to me. |
Is there a way to list for example top 5 comments based on likes on a Post?
The text was updated successfully, but these errors were encountered: