Repository: liqd/a4-meinberlin

Issue:
Number appended to module name on project duplication



Discussion:
this was intended as the module name was required to be unique before. but we can remove it now

Resolving patch:
diff --git a/meinberlin/apps/dashboard2/mixins.py b/meinberlin/apps/dashboard2/mixins.py
--- a/meinberlin/apps/dashboard2/mixins.py
+++ b/meinberlin/apps/dashboard2/mixins.py
@@ -1,7 +1,12 @@
 from copy import deepcopy
+from datetime import datetime
 
+from django.contrib import messages
 from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import PermissionDenied
 from django.shortcuts import get_object_or_404
+from django.shortcuts import redirect
+from django.utils.translation import ugettext_lazy as _
 from django.views.generic import base
 
 from adhocracy4.modules import models as module_models
@@ -94,3 +99,53 @@ def get_context_data(self, **kwargs):
         }
 
         return context
+
+
+class DashboardProjectDuplicateMixin:
+    def post(self, request, *args, **kwargs):
+        if 'duplicate' in request.POST:
+            pk = int(request.POST['project_pk'])
+            project = get_object_or_404(project_models.Project, pk=pk)
+            can_add = request.user.has_perm('a4projects.add_project',
+                                            project)
+
+            if not can_add:
+                raise PermissionDenied
+
+            project_clone = deepcopy(project)
+            project_clone.pk = None
+            if project_clone.tile_image:
+                project_clone.tile_image.save(project.tile_image.name,
+                                              project.tile_image, False)
+            if project_clone.image:
+                project_clone.image.save(project.image.name,
+                                         project.image, False)
+            project_clone.created = datetime.now()
+            project_clone.is_draft = True
+            project_clone.save()
+
+            for module in project.module_set.all():
+                module_clone = deepcopy(module)
+                module_clone.project = project_clone
+                module_clone.pk = None
+                module_clone.save()
+
+                for phase in module.phase_set.all():
+                    phase_clone = deepcopy(phase)
+                    phase_clone.module = module_clone
+                    phase_clone.pk = None
+                    phase_clone.save()
+
+                settings_instance = module.settings_instance
+                if settings_instance:
+                    settings_instance_clone = deepcopy(settings_instance)
+                    settings_instance_clone.pk = None
+                    settings_instance_clone.module = module_clone
+                    settings_instance_clone.save()
+
+            messages.success(request,
+                             _('Project successfully duplicated.'))
+            return redirect('a4dashboard:project-edit',
+                            project_slug=project_clone.slug)
+        else:
+            return super().post(request, *args, **kwargs)
diff --git a/meinberlin/apps/dashboard2/views.py b/meinberlin/apps/dashboard2/views.py
--- a/meinberlin/apps/dashboard2/views.py
+++ b/meinberlin/apps/dashboard2/views.py
@@ -36,6 +36,7 @@ def get_object_or_none(*args, **kwargs):
 
 
 class ProjectListView(mixins.DashboardBaseMixin,
+                      mixins.DashboardProjectDuplicateMixin,
                       generic.ListView):
     model = project_models.Project
     paginate_by = 12


Test patch: