[damned-lies] Moved the release copy functionality from management command to admin action



commit 6344505dcd6c7401992655419e492aee0c39a40b
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Oct 9 09:12:49 2019 +0200

    Moved the release copy functionality from management command to admin action

 stats/admin.py                            | 37 +++++++++++++++++++++++++++--
 stats/management/commands/copy-release.py | 39 -------------------------------
 2 files changed, 35 insertions(+), 41 deletions(-)
---
diff --git a/stats/admin.py b/stats/admin.py
index 83e469a0..084c41d4 100644
--- a/stats/admin.py
+++ b/stats/admin.py
@@ -1,7 +1,9 @@
 from django import forms
 from django.core.exceptions import PermissionDenied
-from django.contrib import admin
+from django.contrib import admin, messages
 from django.contrib.admin import helpers
+from django.db import transaction
+from django.http import HttpResponseRedirect
 from django.shortcuts import render
 from django.utils.encoding import force_text
 
@@ -139,7 +141,38 @@ class ReleaseAdmin(admin.ModelAdmin):
     list_display = ('name', 'status', 'weight', 'string_frozen')
     list_editable = ('weight',)
     inlines = [ CategoryInline ]
-    actions = ['delete_release']
+    actions = ['copy_release', 'delete_release']
+
+    def copy_release(self, request, queryset):
+        """Copy an existing release and use master branches"""
+        if not self.has_add_permission(request):
+            raise PermissionDenied
+        if queryset.count() > 1:
+            messages.error(request, 'Please copy only one release at a time')
+            return HttpResponseRedirect(request.path)
+
+        base_rel = queryset.first()
+        with transaction.atomic():
+            new_rel = Release.objects.create(
+                name=base_rel.name + '-copy', description=base_rel.description + '-copy',
+                string_frozen=False, status=base_rel.status
+            )
+
+            branch_seen = set()
+            for cat in base_rel.category_set.all():
+                if not cat.branch.is_head():
+                    mod = Module.objects.get(pk=cat.branch.module.id)
+                    branch = mod.get_head_branch()
+                else:
+                    branch = cat.branch
+                if branch in branch_seen:
+                    continue
+                else:
+                    Category.objects.create(release=new_rel, branch=branch, name=cat.name)
+                    branch_seen.add(branch)
+        messages.success(request, "New release '%s' created" % new_rel.name)
+        return HttpResponseRedirect(request.path)
+    copy_release.short_description = "Copy release (and associated branches)"
 
     def delete_release(self, request, queryset):
         """ Admin action to delete releases *with* branches which are not linked to another release """


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]