Browse Source

Delete forums action pretty much done.

Rafał Pitoń 11 years ago
parent
commit
da79ff1029

+ 67 - 2
misago/forums/forms.py

@@ -1,5 +1,4 @@
 from django.db import models
 from django.db import models
-from django.core.validators import URLValidator
 from django.utils.html import conditional_escape, mark_safe
 from django.utils.html import conditional_escape, mark_safe
 from django.utils.translation import ugettext_lazy as _
 from django.utils.translation import ugettext_lazy as _
 from mptt.forms import TreeNodeChoiceField as TreeNodeChoiceField
 from mptt.forms import TreeNodeChoiceField as TreeNodeChoiceField
@@ -123,7 +122,7 @@ def ForumFormFactory(instance):
         not_siblings = not_siblings | models.Q(rght__gt=instance.rght)
         not_siblings = not_siblings | models.Q(rght__gt=instance.rght)
         parent_queryset = parent_queryset.filter(not_siblings)
         parent_queryset = parent_queryset.filter(not_siblings)
 
 
-    return type('TreeNodeChoiceField', (ForumFormBase,), {
+    return type('ForumFormFinal', (ForumFormBase,), {
         'new_parent': ForumChoiceField(
         'new_parent': ForumChoiceField(
             label=_("Parent forum"),
             label=_("Parent forum"),
             queryset=parent_queryset,
             queryset=parent_queryset,
@@ -147,3 +146,69 @@ def ForumFormFactory(instance):
             required=False),
             required=False),
         })
         })
 
 
+
+class DeleteForumFormBase(forms.ModelForm):
+    class Meta:
+        model = Forum
+        fields = []
+
+    def clean(self):
+        data = super(DeleteForumFormBase, self).clean()
+
+        if data.get('move_threads_to'):
+            if data['move_threads_to'].pk == self.instance.pk:
+                message = _("You are trying to move this forum threads to "
+                            "itself.")
+                raise forms.ValidationError(message)
+
+            if data['move_threads_to'].role == 'category':
+                message = _("Threads can't be moved to category.")
+                raise forms.ValidationError(message)
+
+            if data['move_threads_to'].role == 'redirect':
+                message = _("Threads can't be moved to redirect.")
+                raise forms.ValidationError(message)
+
+            moving_to_child = self.instance.has_child(data['move_threads_to'])
+            if moving_to_child and not data.get('move_children_to'):
+                message = _("You are trying to move this forum threads to a "
+                            "child forum that will be deleted together with "
+                            "this forum.")
+                raise forms.ValidationError(message)
+
+        if data.get('move_children_to'):
+            if data['move_children_to'].special_role == 'root_category':
+                for child in self.get_children().iterator():
+                    if child.role != 'category':
+                        message = _("One or more child forums in forum are not "
+                                    "categories and thus cannot be made root "
+                                    "categories.")
+                        raise forms.ValidationError(message)
+
+        return data
+
+
+def DeleteFormFactory(instance):
+    content_queryset = Forum.objects.all_forums().order_by('lft')
+    fields = {
+        'move_threads_to': ForumChoiceField(
+            label=_("Move forum threads to"),
+            queryset=content_queryset,
+            initial=instance.parent,
+            empty_label=_('Delete with forum'),
+            required=False),
+    }
+
+    not_siblings = models.Q(lft__lt=instance.lft)
+    not_siblings = not_siblings | models.Q(rght__gt=instance.rght)
+    children_queryset = Forum.objects.all_forums(True)
+    children_queryset = children_queryset.filter(not_siblings).order_by('lft')
+
+    if children_queryset.exists():
+        fields['move_children_to'] = ForumChoiceField(
+            label=_("Move child forums to"),
+            queryset=children_queryset,
+            empty_label=_('Delete with forum'),
+            required=False)
+
+    return type('DeleteForumFormFilan', (DeleteForumFormBase,), fields)

+ 5 - 2
misago/forums/models.py

@@ -17,7 +17,7 @@ class ForumManager(TreeManager):
         qs = self.filter(tree_id=1)
         qs = self.filter(tree_id=1)
         if not include_root:
         if not include_root:
             qs = self.filter(lft__gt=3)
             qs = self.filter(lft__gt=3)
-        return qs
+        return qs.order_by('lft')
 
 
 
 
 class Forum(MPTTModel):
 class Forum(MPTTModel):
@@ -50,7 +50,7 @@ class Forum(MPTTModel):
 
 
     def __unicode__(self):
     def __unicode__(self):
         if self.special_role == 'root_category':
         if self.special_role == 'root_category':
-            return unicode(_('No parent'))
+            return unicode(_('None (will become top level category)'))
         elif self.special_role == 'private_threads':
         elif self.special_role == 'private_threads':
             return unicode(_('Private Threads'))
             return unicode(_('Private Threads'))
         else:
         else:
@@ -64,6 +64,9 @@ class Forum(MPTTModel):
         self.description = description
         self.description = description
         self.description_as_html = subset_markdown(description)
         self.description_as_html = subset_markdown(description)
 
 
+    def has_child(self, child):
+        return child.lft > self.lft and child.rght < self.rght
+
 
 
 """register model in misago admin"""
 """register model in misago admin"""
 site.add_node(
 site.add_node(

+ 28 - 4
misago/forums/views.py

@@ -1,9 +1,11 @@
+import warnings
 from django.contrib import messages
 from django.contrib import messages
+from django.shortcuts import redirect
 from django.utils.translation import ugettext_lazy as _
 from django.utils.translation import ugettext_lazy as _
 from misago.acl import cachebuster
 from misago.acl import cachebuster
 from misago.admin.views import generic
 from misago.admin.views import generic
 from misago.forums.models import Forum
 from misago.forums.models import Forum
-from misago.forums.forms import ForumFormFactory
+from misago.forums.forms import ForumFormFactory, DeleteFormFactory
 
 
 
 
 class ForumAdmin(generic.AdminBaseMixin):
 class ForumAdmin(generic.AdminBaseMixin):
@@ -21,8 +23,6 @@ class ForumAdmin(generic.AdminBaseMixin):
 
 
 
 
 class ForumsList(ForumAdmin, generic.ListView):
 class ForumsList(ForumAdmin, generic.ListView):
-    ordering = (('lft', None),)
-
     def get_queryset(self):
     def get_queryset(self):
         return Forum.objects.all_forums()
         return Forum.objects.all_forums()
 
 
@@ -71,8 +71,32 @@ class EditForum(ForumFormMixin, ForumAdmin, generic.ModelFormView):
     message_submit = _('Forum "%s" has been edited.')
     message_submit = _('Forum "%s" has been edited.')
 
 
 
 
-class EditForum(ForumFormMixin, ForumAdmin, generic.ModelFormView):
+class DeleteForum(ForumAdmin, generic.ModelFormView):
     message_submit = _('Forum "%s" has been deleted.')
     message_submit = _('Forum "%s" has been deleted.')
+    template = 'delete.html'
+
+    def create_form_type(self, request, target):
+        return DeleteFormFactory(target)
+
+    def handle_form(self, form, request, target):
+        move_children_to = form.cleaned_data.get('move_children_to')
+        if move_children_to:
+            for child in target.get_children():
+                Forum.objects.move_node(child, move_children_to, 'last-child')
+        else:
+            for child in target.get_descendants().order_by('-lft'):
+                child.delete()
+
+        move_threads_to = form.cleaned_data.get('move_threads_to')
+        if move_threads_to:
+            warnings.warn("Not implemented yet! See #354 for details.",
+                          FutureWarning)
+
+        form.instance.delete()
+        cachebuster.invalidate()
+
+        messages.success(request, self.message_submit % target.name)
+        return redirect(self.root_link)
 
 
 
 
 class MoveUpForum(ForumAdmin, generic.ButtonView):
 class MoveUpForum(ForumAdmin, generic.ButtonView):

+ 1 - 2
misago/static/misago/admin/css/misago/forms.less

@@ -68,8 +68,7 @@
     }
     }
 
 
     ul {
     ul {
-      float: left;
-      margin-right: 30px;
+      margin-right: 56px;
       margin-bottom: 0px;
       margin-bottom: 0px;
       padding: 0px;
       padding: 0px;
 
 

+ 1 - 2
misago/static/misago/admin/css/style.css

@@ -6575,8 +6575,7 @@ body {
   font-size: 32px;
   font-size: 32px;
 }
 }
 .form-panel .form-errors-block ul {
 .form-panel .form-errors-block ul {
-  float: left;
-  margin-right: 30px;
+  margin-right: 56px;
   margin-bottom: 0px;
   margin-bottom: 0px;
   padding: 0px;
   padding: 0px;
 }
 }

+ 45 - 0
misago/templates/misago/admin/forums/delete.html

@@ -0,0 +1,45 @@
+{% extends "misago/admin/generic/form.html" %}
+{% load crispy_forms_filters i18n %}
+
+
+{% block title %}
+{% blocktrans with forum=target.name %}
+Delete forum: {{forum}}
+{% endblocktrans %} | {{ active_link.name }} | {{ block.super }}
+{% endblock title %}
+
+
+{% block page-target %}
+{% blocktrans with forum=target.name %}
+Delete forum: {{forum}}
+{% endblocktrans %}
+{% endblock page-target %}
+
+
+{% block form-header %}
+<h1>
+  {% blocktrans with forum=target.name %}
+  Delete forum: {{forum}}
+  {% endblocktrans %}
+</h1>
+{% endblock %}
+
+
+{% block form-body %}
+<div class="form-body">
+  <fieldset>
+    <legend>{% trans "Forum contents" %}</legend>
+
+    {% if not form.instance.is_leaf_node %}
+    {{ form.move_children_to|as_crispy_field }}
+    {% endif %}
+    {{ form.move_threads_to|as_crispy_field }}
+
+  </fieldset>
+</div>
+{% endblock form-body %}
+
+
+{% block form-footer %}
+<button class="btn btn-danger">{% trans "Delete forum" %}</button>
+{% endblock %}

+ 3 - 3
misago/templates/misago/admin/forums/form.html

@@ -4,7 +4,7 @@
 
 
 {% block title %}
 {% block title %}
 {% if target.pk %}
 {% if target.pk %}
-{% trans target.name %}
+{{ target.name }}
 {% else %}
 {% else %}
 {% trans "New forum" %}
 {% trans "New forum" %}
 {% endif %} | {{ active_link.name }} | {{ block.super }}
 {% endif %} | {{ active_link.name }} | {{ block.super }}
@@ -13,7 +13,7 @@
 
 
 {% block page-target %}
 {% block page-target %}
 {% if target.pk %}
 {% if target.pk %}
-{% trans target.name %}
+{{ target.name }}
 {% else %}
 {% else %}
 {% trans "New forum" %}
 {% trans "New forum" %}
 {% endif %}
 {% endif %}
@@ -23,7 +23,7 @@
 {% block form-header %}
 {% block form-header %}
 <h1>
 <h1>
   {% if target.pk %}
   {% if target.pk %}
-  {% trans target.name %}
+  {{ target.name }}
   {% else %}
   {% else %}
   {% trans "New forum" %}
   {% trans "New forum" %}
   {% endif %}
   {% endif %}

+ 6 - 0
misago/templates/misago/admin/forums/list.html

@@ -21,6 +21,7 @@
 <th>&nbsp;</th>
 <th>&nbsp;</th>
 <th>&nbsp;</th>
 <th>&nbsp;</th>
 <th>&nbsp;</th>
 <th>&nbsp;</th>
+<th>&nbsp;</th>
 {% endblock table-header %}
 {% endblock table-header %}
 
 
 
 
@@ -71,6 +72,11 @@
     <span class="fa fa-pencil"></span>
     <span class="fa fa-pencil"></span>
   </a>
   </a>
 </td>
 </td>
+<td class="row-action">
+  <a href="{% url 'misago:admin:forums:nodes:delete' forum_id=item.id %}" class="btn btn-danger tooltip-top" title="{% trans "Delete" %}">
+    <span class="fa fa-times"></span>
+  </a>
+</td>
 {% endblock %}
 {% endblock %}