From 488ab2cce7f92c0c8e7cb064607a86cea88df627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 18 Oct 2019 12:03:46 +0300 Subject: [PATCH] Add a "Create a Copy" function for cloning checks Fixes #288 --- CHANGELOG.md | 1 + hc/front/tests/test_copy.py | 18 ++++++++++++++ hc/front/urls.py | 1 + hc/front/views.py | 22 +++++++++++++++++ static/css/details.css | 22 +++++++++++++++++ templates/front/copy_modal.html | 32 +++++++++++++++++++++++++ templates/front/details.html | 32 +++++++++++++++++++------ templates/front/remove_check_modal.html | 6 +---- 8 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 hc/front/tests/test_copy.py create mode 100644 templates/front/copy_modal.html diff --git a/CHANGELOG.md b/CHANGELOG.md index bc543051..bdb0954d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. - Autofocus the email field in the signup form, and submit on enter key - Add support for OpsGenie EU region (#294) - Update OpsGenie logo and setup illustrations +- Add a "Create a Copy" function for cloning checks (#288) ### Bug Fixes - Prevent double-clicking the submit button in signup form diff --git a/hc/front/tests/test_copy.py b/hc/front/tests/test_copy.py new file mode 100644 index 00000000..da8ff8d8 --- /dev/null +++ b/hc/front/tests/test_copy.py @@ -0,0 +1,18 @@ +from hc.api.models import Channel, Check +from hc.test import BaseTestCase + + +class CopyCheckTestCase(BaseTestCase): + def setUp(self): + super(CopyCheckTestCase, self).setUp() + self.check = Check(project=self.project) + self.check.name = "Foo" + self.check.save() + + self.copy_url = "/checks/%s/copy/" % self.check.code + + def test_it_works(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.post(self.copy_url, follow=True) + self.assertContains(r, "This is a brand new check") + self.assertContains(r, "Foo (copy)") diff --git a/hc/front/urls.py b/hc/front/urls.py index f0e3fc68..a74f3183 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -13,6 +13,7 @@ check_urls = [ path("status/", views.status_single, name="hc-status-single"), path("last_ping/", views.ping_details, name="hc-last-ping"), path("transfer/", views.transfer, name="hc-transfer"), + path("copy/", views.copy, name="hc-copy"), path( "channels//enabled", views.switch_channel, diff --git a/hc/front/views.py b/hc/front/views.py index 07bcf56e..5dc2184c 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -483,6 +483,7 @@ def details(request, code): "timezones": pytz.all_timezones, "downtimes": check.downtimes(months=3), "is_new": "new" in request.GET, + "is_copied": "copied" in request.GET, } return render(request, "front/details.html", ctx) @@ -513,6 +514,27 @@ def transfer(request, code): return render(request, "front/transfer_modal.html", ctx) +@require_POST +@login_required +def copy(request, code): + check = _get_check_for_user(request, code) + + copied = Check(project=check.project) + copied.name = check.name + " (copy)" + copied.desc, copied.tags = check.desc, check.tags + copied.subject = check.subject + + copied.kind = check.kind + copied.timeout, copied.grace = check.timeout, check.grace + copied.schedule, copied.tz = check.schedule, check.tz + copied.save() + + copied.channel_set.add(*check.channel_set.all()) + + url = reverse("hc-details", args=[copied.code]) + return redirect(url + "?copied") + + @login_required def status_single(request, code): check = _get_check_for_user(request, code) diff --git a/static/css/details.css b/static/css/details.css index 92066b0d..218112bf 100644 --- a/static/css/details.css +++ b/static/css/details.css @@ -103,3 +103,25 @@ text-align: center; padding: 32px; } + + +ul.checkmarks { + padding-left: 20px; + list-style: none; + color: #117a3f; +} + +ul.checkmarks li:before { + content: '✔ '; +} + + +ul.crosses { + padding-left: 20px; + list-style: none; + color: #aa413e; +} + +ul.crosses li:before { + content: '✘ '; +} \ No newline at end of file diff --git a/templates/front/copy_modal.html b/templates/front/copy_modal.html new file mode 100644 index 00000000..91c7924c --- /dev/null +++ b/templates/front/copy_modal.html @@ -0,0 +1,32 @@ + \ No newline at end of file diff --git a/templates/front/details.html b/templates/front/details.html index ffa13f90..17c26238 100644 --- a/templates/front/details.html +++ b/templates/front/details.html @@ -11,12 +11,23 @@

Your new check is ready! - You can now - give it a name - or + You can now + give it a name + or set its schedule.

-
+ + {% endif %} + + {% if is_copied %} +
+

+ Copy created! + This is a brand new check, with details copied over from your existing check. + You might now want to + update its name and tags. +

+
{% endif %} {% if messages %} @@ -196,15 +207,21 @@

Danger Zone

-

Transfer to a different project, or permanently remove this check.

+

Copy, Transfer, or permanently remove this check.

+ +