Skip to content

Commit

Permalink
fix: proper comment editing form for battles and replies
Browse files Browse the repository at this point in the history
  • Loading branch information
vas3k committed Sep 26, 2024
1 parent 899583f commit f4b3a7f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
10 changes: 10 additions & 0 deletions comments/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.core.exceptions import ValidationError

from comments.models import Comment
from posts.models.post import Post


class CommentForm(forms.ModelForm):
Expand Down Expand Up @@ -111,3 +112,12 @@ def clean(self):
}
}
return cleaned_data


def edit_form_class_for_comment(comment):
if comment.reply_to:
return ReplyForm
elif comment.post.type == Post.TYPE_BATTLE:
return BattleCommentForm
else:
return CommentForm
12 changes: 5 additions & 7 deletions comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from authn.decorators.auth import require_auth
from club.exceptions import AccessDenied, RateLimitException
from comments.forms import CommentForm, ReplyForm, BattleCommentForm
from comments.forms import CommentForm, ReplyForm, BattleCommentForm, edit_form_class_for_comment
from comments.models import Comment, CommentVote
from common.request import parse_ip_address, parse_useragent
from authn.decorators.api import api
Expand Down Expand Up @@ -130,11 +130,11 @@ def edit_comment(request, comment_id):
raise AccessDenied(title="Комментарии к этому посту закрыты")

post = comment.post
FormClass = edit_form_class_for_comment(comment)

if request.method == "POST":
form = CommentForm(request.POST, instance=comment)
if post.type == Post.TYPE_BATTLE:
form = BattleCommentForm(request.POST, instance=comment)
form = FormClass(request.POST, instance=comment)

if form.is_valid():
comment = form.save(commit=False)
comment.is_deleted = False
Expand All @@ -147,9 +147,7 @@ def edit_comment(request, comment_id):

return redirect("show_comment", post.slug, comment.id)
else:
form = CommentForm(instance=comment)
if post.type == Post.TYPE_BATTLE:
form = BattleCommentForm(instance=comment)
form = FormClass(instance=comment)

return render(request, "comments/edit.html", {
"comment": comment,
Expand Down
4 changes: 4 additions & 0 deletions frontend/html/comments/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{% block content %}
<div class="content comment">
<form action="{% url "edit_comment" comment.id %}" method="post" class="form comment-form-form">
{% if comment.reply_to_id %}
<input type="hidden" name="reply_to_id" value="{{ comment.reply_to_id }}">
{% endif %}

<div class="comment-form">
<div class="comment-form-body">
{% include form|edit_form with post=post form=form comment=comment save_message="Сохранить" %}
Expand Down
14 changes: 10 additions & 4 deletions frontend/html/comments/forms/battle.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<input type="hidden" name="post_comment_order" value="{{ comment_order }}">
<div class="comment-form">
<div class="comment-form-avatar">
<div class="avatar"><img src="{{ me.get_avatar }}" alt="Аватар {{ me.full_name }}" loading="lazy" /></div>
{% if comment %}
<div class="avatar"><img src="{{ comment.author.get_avatar }}" alt="Аватар" loading="lazy" /></div>
{% else %}
<div class="avatar"><img src="{{ me.get_avatar }}" alt="Аватар" loading="lazy" /></div>
{% endif %}
</div>
<div class="comment-form-body comment-form-battle">
<div class="comment-form-body-battle-side">Я за
Expand All @@ -28,12 +32,14 @@
<i class="fa fa-image"></i>
</label>
<div class="comment-form-subscribe">
{{ form.subscribe_to_post }} {{ form.subscribe_to_post.label_tag }}
{% if not comment %}
{{ form.subscribe_to_post }} {{ form.subscribe_to_post.label_tag }}
{% endif %}
</div>
<button type="submit" class="button comment-form-button"> {{save_message}} </button>
<button type="submit" class="button comment-form-button"> {{ save_message }} </button>
</div>
</div>
{% if form.text.errors %}<span class="form-errors">{{ form.full_name.errors }}</span>{% endif %}
{% if form.errors %}<span class="form-errors">{{ form.errors }}</span>{% endif %}
</div>
</div>
</form>
12 changes: 9 additions & 3 deletions frontend/html/comments/forms/comment.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<input type="hidden" name="post_comment_order" value="{{ comment_order }}">
<div class="comment-form">
<div class="comment-form-avatar">
<div class="avatar"><img src="{{ me.get_avatar }}" alt="Аватар {{ me.full_name }}" loading="lazy" /></div>
{% if comment %}
<div class="avatar"><img src="{{ comment.author.get_avatar }}" alt="Аватар" loading="lazy" /></div>
{% else %}
<div class="avatar"><img src="{{ me.get_avatar }}" alt="Аватар" loading="lazy" /></div>
{% endif %}
</div>
<div class="comment-form-body comment-form-editor-container">
<comment-markdown-editor class="comment-form-body-text" post-slug="{{ post.slug }}"
Expand All @@ -17,11 +21,13 @@
<i class="fa fa-image"></i>
</label>
<div class="comment-form-subscribe">
{{ form.subscribe_to_post }} {{ form.subscribe_to_post.label_tag }}
{% if not comment %}
{{ form.subscribe_to_post }} {{ form.subscribe_to_post.label_tag }}
{% endif %}
</div>
<button type="submit" class="button comment-form-button">{{ save_message }}</button>
</div>
{% if form.text.errors %}<span class="form-errors">{{ form.full_name.errors }}</span>{% endif %}
{% if form.errors %}<span class="form-errors">{{ form.errors }}</span>{% endif %}
</div>
</div>
</form>

0 comments on commit f4b3a7f

Please sign in to comment.