django-machina icon indicating copy to clipboard operation
django-machina copied to clipboard

reply to post and tree-like organization of posts

Open BoPeng opened this issue 4 years ago • 2 comments

Just checking if I have missed something obvious, is is true that reply in machina always replies to the topic, not particular post? In another word,

  1. how can I reply to a particular post (and notify the author of the post, in addition to the topic poster)?
  2. how can I organize the post in a tree-like structure, like what quora or disqus do?

BoPeng avatar Mar 24 '21 05:03 BoPeng

That would appear to be the case. There isn't a 'reply to post' feature like on most forums.

I too was looking for django forum software that did both the things you're asking about too ...

aes256lamp avatar Nov 20 '21 06:11 aes256lamp

I can confirm that django-machina does not support this feature. It is however possible to extend Post to support it. It is a lot of work but the following are the essential steps:

  1. Extend the entire forum_conversation app as described in the documentation.

  2. extend Post and add a reply-to field.

from machina.apps.forum_conversation.abstract_models import AbstractPost
class Post(AbstractPost):

    reply_to = models.ForeignKey(
        'Post', related_name='replies', on_delete=models.CASCADE
    )
  1. Extend the PostCreateView (optional, but here is how to included the quoted text in the new reply)
from machina.apps.forum_conversation.views import PostCreateView as BasePostCreateView

def quote_text(text):
    lines = ['> ' + x for x in text.raw.splitlines()]
    return '\n'.join(lines) + '\n\n'

class PostCreateView(BasePostCreateView):

    def get_post_form_kwargs(self):
        kwargs = super().get_post_form_kwargs()
        
        if 'reply_to' in self.request.GET:
            try:
                post = Post.objects.get(pk=self.request.GET['reply_to'])
                kwargs['reply_to'] = post
                kwargs['initial'] = {'content': quote_text(post.content)}
            except Exception as e:
                logger.warning(e)
        else:
            kwargs['reply_to'] = None
        return kwargs
  1. Similarly, the the PostForm, you will need to save reply_to to Post.
  2. Then, in the template, you will have to carefully display posts depending on reply_to of post. You can display everything sequencitially but marking reply_to, or display in a tree structure, basically running a for loop that lists all replies....

BoPeng avatar Apr 16 '22 14:04 BoPeng