Do the same for comment edition

This commit is contained in:
Grégory Soutadé 2013-02-09 09:48:30 +01:00
parent fd65e84b62
commit 61d2d63bf4
3 changed files with 17 additions and 2 deletions

View File

@ -27,7 +27,8 @@ from re import sub
from datetime import datetime from datetime import datetime
from django.db import models from django.db import models
from django.contrib.auth.models import User 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 django.dispatch import receiver
from dynastie.generators import * from dynastie.generators import *
@ -362,6 +363,12 @@ class Comment(models.Model):
the_comment = models.TextField(max_length=255) the_comment = models.TextField(max_length=255)
ip = models.GenericIPAddressField() ip = models.GenericIPAddressField()
def _update_line_returns(self):
self.the_comment = self.the_comment.replace('\n', '<br />')
def _remove_br(self):
self.the_comment = self.the_comment.replace('<br />', '\n')
@receiver(post_init, sender=Blog) @receiver(post_init, sender=Blog)
def init_blog_signal(sender, **kwargs): def init_blog_signal(sender, **kwargs):
kwargs['instance'].create_paths() kwargs['instance'].create_paths()
@ -386,3 +393,8 @@ def delete_post_signal(sender, **kwargs):
def pre_delete_post_signal(sender, **kwargs): def pre_delete_post_signal(sender, **kwargs):
post = kwargs['instance'] post = kwargs['instance']
comments = Comment.objects.filter(post=post.id).delete() comments = Comment.objects.filter(post=post.id).delete()
# Replace line returns by <br /> for generation
@receiver(pre_save, sender=Comment)
def pre_save_comment_signal(sender, **kwargs):
kwargs['instance']._update_line_returns()

View File

@ -18,10 +18,12 @@
{% for comment in comments %} {% for comment in comments %}
<div class="comment"> <div class="comment">
<div class="infos"> <div class="infos">
<a href="/comment/edit/{{ comment.id }}">#{{ comment.id }}</a> <span class="author">{{ comment.author }}</span> <span class="mail">{{ comment.email|default:"no mail" }}</span> <span class="date">{{ comment.date|date:"D d M Y H:m" }}<span> <a href="/comment/delete/{{ comment.id }}">delete</a> <a href="/comment/edit/{{ comment.id }}">#{{ comment.id }}</a> <span class="author">{{ comment.author }}</span> <span class="mail">{{ comment.email|default:"no mail" }}</span> <span class="date">{{ comment.date|date:"D d M Y H:m" }}<span> <a href="/comment/delete/{{ comment.id }}" onclick="return confirm('Do you really want to delete this item ?')">delete</a>
</div> </div>
<div class="content"> <div class="content">
{% autoescape off %}
{{ comment.the_comment }} {{ comment.the_comment }}
{% endautoescape %}
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -782,6 +782,7 @@ def edit_comment(request, comment_id):
else: else:
return HttpResponseRedirect('/post/edit/' + str(post_id)) return HttpResponseRedirect('/post/edit/' + str(post_id))
else: else:
comment._remove_br()
form = CommentForm(instance=comment) form = CommentForm(instance=comment)
return render(request, 'edit_comment.html', {'form': form, 'comment':comment}) return render(request, 'edit_comment.html', {'form': form, 'comment':comment})