Skip to content

Commit

Permalink
add delete view (#1961)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jul 25, 2024
1 parent bc01ee7 commit e21f890
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
3 changes: 2 additions & 1 deletion isatemplates/templates/isatemplates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ <h4>
<i class="iconify" data-icon="mdi:download"></i>
Export Template
</a>
<a class="dropdown-item text-danger" href="#"> {# TODO #}
<a class="dropdown-item text-danger"
href="{% url 'isatemplates:delete' cookiecutterisatemplate=t.sodar_uuid %}">
<i class="iconify" data-icon="mdi:close-thick"></i>
Delete Template
</a>
Expand Down
30 changes: 30 additions & 0 deletions isatemplates/templates/isatemplates/template_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends 'projectroles/base.html' %}

{% block title %}Confirm ISA-Tab Template Deletion{% endblock %}

{% block projectroles %}

<div class="container-fluid sodar-subtitle-container">
<h2>Confirm ISA-Tab Template Deletion</h2>
</div>

<div class="container-fluid sodar-page-container">
<div class="alert alert-warning" role="alert">
Are you sure you want to delete the ISA-Tab template
"{{ object.description }}"? This can <strong>not</strong> be undone!
</div>
<form method="post">
{% csrf_token %}
<div class="btn-group pull-right" role="group">
<a role="button" class="btn btn-secondary"
href="{% url 'isatemplates:list' %}">
<i class="iconify" data-icon="mdi:arrow-left-circle"></i> Cancel
</a>
<button type="submit" class="btn btn-danger sodar-btn-submit-once">
<i class="iconify" data-icon="mdi:close-thick"></i> Delete
</button>
</div>
</form>
</div>

{% endblock projectroles %}
12 changes: 12 additions & 0 deletions isatemplates/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ def test_get_update(self):
bad_users = [self.anonymous, self.regular_user]
self.assert_response(url, good_users, 200)
self.assert_response(url, bad_users, 302)

def test_get_delete(self):
"""Test ISATemplateDeleteView GET"""
template = self.make_isa_template(TEMPLATE_NAME, TEMPLATE_DESC, {})
url = reverse(
'isatemplates:delete',
kwargs={'cookiecutterisatemplate': template.sodar_uuid},
)
good_users = [self.superuser]
bad_users = [self.anonymous, self.regular_user]
self.assert_response(url, good_users, 200)
self.assert_response(url, bad_users, 302)
49 changes: 49 additions & 0 deletions isatemplates/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ def test_post(self):
self.assertEqual(self.template.description, TEMPLATE_DESC)
self.assertEqual(self.template.name, TEMPLATE_NAME)
self.assertEqual(self.template.json_data, {})
self.assertEqual(self.template.active, True)
for f in self._get_files():
self.assertEqual(f.content, '')
self.assertEqual(
Expand All @@ -488,6 +489,7 @@ def test_post(self):
'file_upload': self.get_zip(TEMPLATE_PATH, ARCHIVE_NAME),
'description': TEMPLATE_DESC_UPDATE,
'name': TEMPLATE_NAME_UPDATE,
'active': False,
}
with self.login(self.user):
response = self.client.post(self.url, data)
Expand All @@ -498,6 +500,7 @@ def test_post(self):
self.template.refresh_from_db()
self.assertEqual(self.template.description, TEMPLATE_DESC_UPDATE)
self.assertEqual(self.template.name, TEMPLATE_NAME_UPDATE)
self.assertEqual(self.template.active, False)
with open(TEMPLATE_JSON_PATH, 'rb') as f:
json_data = json.load(f)
self.assertEqual(self.template.json_data, json_data)
Expand Down Expand Up @@ -647,3 +650,49 @@ def test_post_cubi_template_exists(self):
self.assertEqual(CookiecutterISAFile.objects.count(), 3)
for f in self._get_files():
self.assertEqual(f.content, '')


class TestISATemplateDeleteView(ISATemplateViewTestBase):
"""Tests for ISATemplateDeleteView"""

def _get_files(self):
return CookiecutterISAFile.objects.filter(template=self.template)

def setUp(self):
super().setUp()
# Set up template with empty files
self.template = self.make_isa_template(
name=TEMPLATE_NAME,
description=TEMPLATE_DESC,
json_data={},
user=self.user,
)
for fn in ISA_FILE_NAMES:
self.make_isa_file(template=self.template, file_name=fn, content='')
self.url = reverse(
'isatemplates:delete',
kwargs={'cookiecutterisatemplate': self.template.sodar_uuid},
)

def test_get(self):
"""Test ISATemplateUpdateView GET"""
with self.login(self.user):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['object'], self.template)

def test_post(self):
"""Test POST"""
self.assertEqual(
ProjectEvent.objects.filter(event_name='template_delete').count(), 0
)
self.assertEqual(CookiecutterISATemplate.objects.count(), 1)
self.assertEqual(CookiecutterISAFile.objects.count(), 3)
with self.login(self.user):
response = self.client.post(self.url)
self.assertEqual(response.status_code, 302)
self.assertEqual(CookiecutterISATemplate.objects.count(), 0)
self.assertEqual(CookiecutterISAFile.objects.count(), 0)
self.assertEqual(
ProjectEvent.objects.filter(event_name='template_delete').count(), 1
)
5 changes: 5 additions & 0 deletions isatemplates/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
view=views.ISATemplateUpdateView.as_view(),
name='update',
),
path(
route='delete/<uuid:cookiecutterisatemplate>',
view=views.ISATemplateDeleteView.as_view(),
name='delete',
),
]
30 changes: 26 additions & 4 deletions isatemplates/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import CreateView, UpdateView, TemplateView
from django.views.generic import (
CreateView,
UpdateView,
DeleteView,
TemplateView,
)

# Projectroles dependency
from projectroles.plugins import get_backend_api
Expand Down Expand Up @@ -72,7 +77,7 @@ def handle_modify(self, obj, action):
self.request,
'ISA-Tab template "{}" {}d.'.format(obj.description, action),
)
return redirect(reverse('isatemplates:list'))
return reverse('isatemplates:list')


class ISATemplateCreateView(
Expand All @@ -89,7 +94,7 @@ class ISATemplateCreateView(

def form_valid(self, form):
obj = form.save()
return self.handle_modify(obj, 'create')
return redirect(self.handle_modify(obj, 'create'))


class ISATemplateUpdateView(
Expand All @@ -109,4 +114,21 @@ class ISATemplateUpdateView(

def form_valid(self, form):
obj = form.save()
return self.handle_modify(obj, 'update')
return redirect(self.handle_modify(obj, 'update'))


class ISATemplateDeleteView(
LoggedInPermissionMixin,
ISATemplateModifyMixin,
DeleteView,
):
"""CookiecutterISATemplate update view"""

model = CookiecutterISATemplate
permission_required = 'isatemplates.delete_template'
slug_url_kwarg = 'cookiecutterisatemplate'
slug_field = 'sodar_uuid'
template_name = 'isatemplates/template_confirm_delete.html'

def get_success_url(self):
return self.handle_modify(self.object, 'delete')

0 comments on commit e21f890

Please sign in to comment.