Commits

Gregory Petukhov committed 1ccc7f9

Add topic delete feature

Comments (0)

Files changed (4)

 class TopicForm(forms.Form):
     name = forms.CharField(label=u'Название темы')
     content = forms.CharField(label=u'Сообщение', widget=forms.Textarea)
+
+
+class TopicDeleteForm(forms.Form):
+    pass
-from django.db.models.signals import post_save
+from django.db.models.signals import post_save, post_delete
 from django.dispatch import receiver
-from django.db.models import F
+from django.db.models import F, Count
 
 from pybb.models import Forum, Topic, Post
 
 @receiver(post_save, sender=Topic)
 def topic_post_save(instance, **kwargs):
     topic = instance
+    topic_count = topic.forum.topics.count()
     Forum.objects.filter(pk=topic.forum_id)\
-         .update(topic_count=F('topic_count') + 1)
+         .update(topic_count=topic_count)
 
 
 @receiver(post_save, sender=Post)
 def post_post_save(instance, **kwargs):
     post = instance
     topic = post.topic
+    topic_post_count = topic.posts.count()
+    forum_post_count = Post.objects.filter(topic__forum=topic.forum)\
+                           .aggregate(num=Count('pk'))['num']
     Topic.objects.filter(pk=post.topic_id)\
-         .update(post_count=F('post_count') + 1)
+         .update(post_count=topic_post_count)
     Forum.objects.filter(pk=topic.forum_id)\
-         .update(post_count=F('post_count') + 1)
+         .update(post_count=forum_post_count)
+
+
+@receiver(post_delete, sender=Topic)
+def topic_post_delete(instance, **kwargs):
+    topic = instance
+    topic_count = topic.forum.topics.count()
+    post_count = Post.objects.filter(topic__forum=topic.forum)\
+                     .aggregate(num=Count('pk'))['num']
+    Forum.objects.filter(pk=topic.forum_id)\
+         .update(topic_count=topic_count,
+                 post_count=post_count)
     url(r'^topic/(\d+)$', 'topic_page', name='topic_page'),
     url(r'^post/add$', 'post_add', name='post_add'),
     url(r'^topic/add$', 'topic_add', name='topic_add'),
+    url(r'^topic/(\d+)/delete$', 'topic_delete', name='topic_delete'),
 )
 from common.pagination import paginate
 
 from pybb.models import Category, Forum, Topic, Post
-from pybb.forms import PostForm, TopicForm
+from pybb.forms import PostForm, TopicForm, TopicDeleteForm
 
 
 def get_post_form(request, topic):
                'forum': forum,
             }
     return render(request, 'pybb/topic_add.html', context)
+
+
+@login_required
+def topic_delete(request, pk):
+    topic = get_object_or_404(Topic, pk=pk)
+    if not request.user.is_superuser:
+        messages.error(request, u'У вас нет права на удаление темы')
+        return reverse('pybb:home_page')
+    if request.method == 'POST':
+        form = TopicDeleteForm(request.POST)
+    else:
+        form = TopicDeleteForm()
+    if form.is_valid():
+        topic.delete()
+        messages.success(request, u'Тема удалена')
+        return redirect('pybb:home_page')
+    context = {'topic': topic, 'form': form,
+            }
+    return render(request, 'pybb/topic_delete.html', context)