Skip to content
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

Add parent field to Post #274

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion machina/apps/forum_conversation/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ class AbstractPost(DatedModel):
anonymous_key = models.CharField(
max_length=100, blank=True, null=True, verbose_name=_('Anonymous user forum key'),
)

parent = models.ForeignKey(
'self', related_name='children', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='Reply to post',
)
# Each post can have its own subject. The subject of the thread corresponds to the
# one associated with the first post
subject = models.CharField(verbose_name=_('Subject'), max_length=255)
Expand Down
5 changes: 4 additions & 1 deletion machina/apps/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class PostForm(forms.ModelForm):

class Meta:
model = Post
fields = ['subject', 'content', 'username', 'update_reason', 'enable_signature', ]
fields = ['subject', 'content', 'username', 'update_reason', 'enable_signature', 'parent']

def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
self.forum = kwargs.pop('forum', None)
self.topic = kwargs.pop('topic', None)
self.parent = kwargs.pop('parent', None)

self.perm_handler = PermissionHandler()

Expand All @@ -47,6 +48,7 @@ def __init__(self, *args, **kwargs):
self.fields['subject'].widget.attrs['placeholder'] = _('Enter your subject')
self.fields['content'].label = _('Message')
self.fields['content'].widget.attrs['placeholder'] = _('Enter your message')
self.fields['parent'].initial = self.parent

# Handles anonymous users
if self.user and self.user.is_anonymous:
Expand Down Expand Up @@ -99,6 +101,7 @@ def save(self, commit=True):
subject=self.cleaned_data['subject'],
approved=self.perm_handler.can_post_without_approval(self.forum, self.user),
content=self.cleaned_data['content'],
parent=self.cleaned_data['parent'],
enable_signature=self.cleaned_data['enable_signature'])
if not self.user.is_anonymous:
post.poster = self.user
Expand Down
19 changes: 19 additions & 0 deletions machina/apps/forum_conversation/migrations/0014_add_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.11 on 2022-04-23 15:13

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0013_auto_20201220_1745'),
]

operations = [
migrations.AddField(
model_name='post',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='forum_conversatin.Post', verbose_name='Parent post'),
),
]
10 changes: 10 additions & 0 deletions machina/apps/forum_conversation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def get_post_form_kwargs(self):
'user': self.request.user,
'forum': self.get_forum(),
'topic': self.get_topic(),
'parent': self.get_parent_post(),
}

post = self.get_post()
Expand Down Expand Up @@ -311,6 +312,15 @@ def get_topic(self):
self._topic = get_object_or_404(Topic, pk=pk)
return self._topic

def get_parent_post(self):
if 'parent' in self.request.GET:
try:
return Post.objects.get(
pk=self.request.GET['parent'])
except Post.DoesNotExist:
return None
return None

def get_post(self):
""" Returns the considered post if applicable. """
pk = self.kwargs.get(self.post_pk_url_kwarg, None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{% if post_form.username %}
{% include "partials/form_field.html" with field=post_form.username %}
{% endif %}
{{ post_form.parent.as_hidden }}
{% if post_form.update_reason %}
{% include "partials/form_field.html" with field=post_form.update_reason %}
{% endif %}
Expand Down