diff --git a/models.py b/models.py index 3cb6e1d..14fa5be 100644 --- a/models.py +++ b/models.py @@ -27,7 +27,8 @@ from re import sub from datetime import datetime from django.db import models from django.contrib.auth.models import User -from django.db.models.signals import post_init, pre_delete, post_delete, post_save +from django.db.models.signals import pre_init, post_init, pre_delete, post_delete +from django.db.models.signals import pre_save, post_save from django.dispatch import receiver from dynastie.generators import * @@ -362,6 +363,12 @@ class Comment(models.Model): the_comment = models.TextField(max_length=255) ip = models.GenericIPAddressField() + def _update_line_returns(self): + self.the_comment = self.the_comment.replace('\n', '
') + + def _remove_br(self): + self.the_comment = self.the_comment.replace('
', '\n') + @receiver(post_init, sender=Blog) def init_blog_signal(sender, **kwargs): kwargs['instance'].create_paths() @@ -386,3 +393,8 @@ def delete_post_signal(sender, **kwargs): def pre_delete_post_signal(sender, **kwargs): post = kwargs['instance'] comments = Comment.objects.filter(post=post.id).delete() + +# Replace line returns by
for generation +@receiver(pre_save, sender=Comment) +def pre_save_comment_signal(sender, **kwargs): + kwargs['instance']._update_line_returns() diff --git a/templates/edit_post.html b/templates/edit_post.html index 561c56c..983c814 100644 --- a/templates/edit_post.html +++ b/templates/edit_post.html @@ -18,10 +18,12 @@ {% for comment in comments %}
- #{{ comment.id }} {{ comment.author }} {{ comment.email|default:"no mail" }} {{ comment.date|date:"D d M Y H:m" }} delete + #{{ comment.id }} {{ comment.author }} {{ comment.email|default:"no mail" }} {{ comment.date|date:"D d M Y H:m" }} delete
+ {% autoescape off %} {{ comment.the_comment }} + {% endautoescape %}
{% endfor %}
diff --git a/views.py b/views.py index c10516e..e6bb1ba 100644 --- a/views.py +++ b/views.py @@ -782,6 +782,7 @@ def edit_comment(request, comment_id): else: return HttpResponseRedirect('/post/edit/' + str(post_id)) else: + comment._remove_br() form = CommentForm(instance=comment) return render(request, 'edit_comment.html', {'form': form, 'comment':comment})